This is the first part in a series on LInux Basics. Today, I’m going to give you a brief run-down on creating user accounts and creating groups from the linux command line. These aren’t difficult tasks, but often times it’s good to refresh the basics and have a reference to go back to. So, here we go.
How to add a user account
Creating a user account is a very straight forward process and nearly identical for all Linux distributions.
# useradd username
Some distributions, such as Ubuntu, might have the root account disabled. If so, you will need to use sudo to gain root privileges to run a specific command. If you get a permission denied error, simply run this command instead.
# sudo useradd username
How to set or change a password
Once you’ve created a user account, you will need to set a password. To do so, use the passwd command.
# passwd username Enter new UNIX password: Retype new UNIX password: or # sudo passwd username Enter new UNIX password: Retype new UNIX password:
How to create a new group
Creating a group is very simple. To do so, use the groupadd command.
# groupadd groupname or # sudo groupadd groupname
Add a user to group
To add an existing user to an additional (supplemental) existing group, use the usermod command.
# usermod -a -G groupname username or # sudo usermod -a -G groupname username
In the above command, the -a option tells usermod you want to add a user and the -G options specifies what you want to add the user to, i.e. a group.
Change a user’s primary group
Changing a user’s primary group is also done with the usermod command. To do so, run the command below.
# usermod -g groupname username or # sudo usermod -g groupname username
The -g options tells usermod to change the user’s primary group.
To view what groups a user belongs to
If you need to see what groups an existing user belongs to, and what their UID is, you would use the id command.
# id username
To delete a user account
If you need to delete a user’s account, you will use the userdel command.
# userdel username or # sudo userdel username
If you want to delete the users home directory at the same time, you can use the -r option.
# userdel -r username or # sudo userdel -r username
The -r option tells userdel to delete the user’s home directory, and the user’s account.
To remove a group
Removing a group is simple process. To do so you would use the groupdel command.
# groupdel groupname or # sudo groupdel groupname
There are many graphical utilities to perform user and group management related tasks and these vary from distribution to distribution. To obtain more information on the commands we used in this article simply load the corresponding man pages for each command. You can do so using the following commands.
# man useradd # man usermod # man groupadd # man userdel # man groupdel
You can also use the -help option on most commands. This gives you a more consolidated, quicker way to get help.
Working with users and groups from the command line is a very straight forward process. It is mostly the same on all Linux and BSD operating systems. If you need help or have any questions, please feel free to ask in the comments below. Thanks!