The essential commands for creating, modifying, and deleting user accounts, as well as managing user privileges and groups.
User Management:
Creating a new user:
sudo useradd username
Set the user’s home directory and shell:
sudo useradd -d /home/username -s /bin/bash username
Setting a password for the user:
sudo passwd username
Deleting a user:
sudo userdel -r username
Modifying user properties:
sudo usermod -g newgroup -aG group1,group2 -d /new/home/directory -s /bin/newshell username
Listing all users:
getent passwd
Changing to another user:
su - username
Granting sudo privileges:
sudo visudo
Add the following line:
username ALL=(ALL:ALL) ALL
Locking and unlocking user accounts:
sudo passwd -l username # lock sudo passwd -u username # unlock
Group Management:
Creating a new group:
sudo groupadd groupname
Adding a user to a group:
sudo usermod -aG groupname username
Removing a user from a group:
sudo deluser username groupname
Listing all groups:
getent group
Modifying group properties:
sudo groupmod -n newgroupname oldgroupname
Add or remove a user from a group:
sudo gpasswd -a username groupname # Add user to group sudo gpasswd -d username groupname # Remove user from group
Deleting a group:
sudo groupdel groupname
Checking a user’s groups:
groups username
Changing the primary group of a user:
sudo usermod -g newprimarygroup username
These commands provide a comprehensive overview of user and group management on a Linux system. Whether you’re creating new users, modifying user properties, managing groups, or adjusting user privileges, these commands will help you effectively administer your Linux environment.