This article is going to cover some basic commands that can be used to manage users and groups in Ubuntu Linux including adding users and giving admin permissions. The screenshots used in this article were taken on an Ubuntu machine but the commands should work in most of the different Linux distros.
When it comes to adding a new user in Linux, the below command can be used.
useradd joe (creates a user, joe, together with the primary group with the same name)
If you are not logged in as a root user but have admin permissions, you will need to use the sudo command. Assuming the user is already in the sudo group. Use the below command:
sudo useradd joe
To check whether user joe shows up in the user accounts file located in /etc/passwd:
grep joe /etc/passwd (use grep to filter the contents of the file that lists all user accounts)
To check whether a user has sudo/admin privileges:
sudo whoami (The output should say ‘root’ if the current user has admin privileges. Otherwise, it will say “user is not in the sudoers file”)
To give administrative permissions/privileges, add the user to the sudo group. The sudo command allows users to perform admin tasks such as installing software.
To list all the groups that a user belongs to, use the below commands:
groups joe (lists all groups that user joe is a member of) OR
id joe (lists user joe’s id, as well as group id and all the groups user joe is a member of)
To change a user’s password use:
sudo passwd joe
All users have a default primary group that is created whenever a user is added. All files created by the user will be placed in this group. A user can be added to a secondary group which usually gives the user certain privileges.
To add the user to the sudo group, run the below command either as a root user or as a regular user who has sudo permissions.
usermod -h (use this command to see what options are available with the usermod command)
sudo usermod -aG sudo joe (adds joe to the sudo group)
To switch user to user joe use the below command:
su joe (use su for switch users then enter joe’s password)
Then check whether the user has sudo permissions:
sudo whoami
To delete a user, use the below command:
deluser joe or sudo deluser joe (deletes user joe, but does not delete his home directory and files)
deluser –remove-home joe (deletes user joe plus his home directory)
To remove sudo privileges, edit the /etc/sudoers file using the below commands:
sudo visudo (open the sudoers file)
Then delete the following line in the file:
joe ALL=(ALL:ALL) ALL (Delete this line from the file and then save the file)
Conclusion
In this article, we went over some of the aspects to do with managing users and groups in Linux. We saw how to use various commands to perform tasks such as adding users and giving permissions. We also highlighted the importance of the admin group or the sudo group that is used to give users administrative privileges.