All user manangment commands need to be executed with sudo, so the user who is creating/modifying users should be part of sudoers file.
useradd is the command that let's you add users. However, this is a very basic command with limited options.
adduser is an enhanced command and generally used.
sudo add user <user_name>Example
sudo add user wjohnsonwhen you create a user in Ubuntu with adduser command it will prompt you to enter password for the user.
However in some distributinos like Centos, you may have to update password manually using the below commands
sudo passwd <user_name>Example
sudo passwd wjohnsonEverytime a user account is created a group with the same name is also created by default.
For example, if a user called testuser is created and a group with the name testuser is also created.
To create a user in a different group other than the one with his user name, use --ingroup option.
sudo adduser <newusername> --ingroup <groupname>For instance if you would like to create a user with the username tester but not create a group with the same name and want him to be part of the group qa
then use the below command:
sudo adduser tester --ingroup qaTo delete a user use the below commands
sudo userdel <user-name>All the user details are stored in the file /etc/passwd. It is not recommended to edit these files, use usermod instead
Groups are a way to organize users. Everyuser is part of a default group.
To create a group use the below command
sudo groupadd <group_name>Example:
sudo groupadd tesersTo delete a user, use the below command
sudo groupdel <group_name>Example:
sudo groupdel tesersTo see the groups to which a user belongs to use the groups command
The below command gives the list of groups to which the current user is part of
groupsTo see the list of groups for another user, use the below command
groups <username>For example if you're logged in as john but would like to see the groups of alice, use the below command:
groups aliceInformation about all the groups is stored in the file /etc/group
su -- Switch user ; to login as a different user
su - <username>For example if you're logged in as john but would like to switch to tu user alice, use the below command:
[this assumes you know the password for the user alice]
su - aliceIf you're a superuser, you need not know the password for the user you can sudo su
sudo su - aliceTo logout from the session:
exitThe command usermod is used to change the attributes of a user
For instance, to add a user to an existing group we use usermod
sudo usermod -aG <groupname> <username>Exmple: Lets add a user tester4 to the group qa
sudo usermod -aG qa tester4