User management in Unix involves creating, modifying, and managing user accounts, as well as configuring user permissions and access. Here, we'll cover key user management tasks with detailed explanations and examples.
1. Creating a User:
To create a new user in Unix, you can use the useradd
command. You'll also want to set a password for the new user with the passwd
command.
# Create a new user named "john"
sudo useradd john
# Set a password for the new user
sudo passwd john
2. Modifying User Attributes:
You can modify user attributes, such as the user's full name, home directory, or shell, using the usermod
command.
# Change the user's full name
sudo usermod -c "John Doe" john
# Change the user's home directory
sudo usermod -d /home/johndoe john
# Change the user's default shell
sudo usermod -s /bin/bash john
3. Deleting a User:
To delete a user account, use the userdel
command. Be cautious when deleting users, as their files and home directories may be removed as well.
# Delete the user account (without removing the home directory)
sudo userdel john
# Delete the user account and remove the home directory
sudo userdel -r johndoe
4. Viewing User Information:
You can view user account information using the id
or finger
command. The id
command provides basic user information, while finger
gives more detailed information if available.
# View basic user information
id john
# View detailed user information (if available)
finger john
5. Changing User Password:
Users can change their passwords using the passwd
command followed by their username. The sudo
command is not required for users to change their own passwords.
# Change your own password
passwd
# Change another user's password (requires sudo)
sudo passwd john
6. User Groups:
Unix uses groups to manage user permissions. You can create a new group with the groupadd
command and add users to groups with the usermod
command.
# Create a new group
sudo groupadd mygroup
# Add a user to a group
sudo usermod -aG mygroup john
7. Switching Users:
To switch to another user's environment temporarily, you can use the su
(substitute user) command.
# Switch to the "john" user
su - john
8. User Privileges:
To grant administrative privileges to a user, you can add them to the sudo
group. Users in the sudo
group can execute commands with superuser privileges using the sudo
command.
# Add a user to the sudo group (to grant sudo privileges)
sudo usermod -aG sudo john
9. Locking and Unlocking User Accounts:
You can lock and unlock user accounts using the passwd
command. A locked account prevents login.
# Lock a user account
sudo passwd -l john
# Unlock a user account
sudo passwd -u john
10. Viewing Logged-In Users:
The who
and w
commands can be used to see who is currently logged into the system and what they are doing.
# Show currently logged-in users
who
# Show currently logged-in users and their activities
w
User management is an essential part of Unix administration, allowing you to control access and permissions on your system. These commands and techniques help you create, modify, and manage user accounts and their associated attributes in a Unix-like operating system.