How to create a user in Linux[User management]
How to create a user in Linux
Knowing how to create a user in Linux is one of the basic skills a Linux user(admin) should know and still one of the most important skills as well to have(user management). Reason behind this is, because in every multi user operating system(Linux as well) there are and there should be a sets of different user permissions and different user groups. This is of course because of security reasons.
You don’t want to have one user managing the entire system which runs on a production environment(meaning on servers). It’s always advisable to have multiple users on your system. Sometimes you might need to have a user will act only as a service account(not having a home folder, no access to the shell and only be able to run specific apps and tools).
In this article we will cover how to create a user in Linux via terminal and how to delete a user in Linux as well. The commands we’ll be using to run these actions are – “adduser” and “deluser”. You can also create a new user with the command – “useradd”. There are differences between these two commands. They behave differently and will cover both of them.
To be able to create and remove users, you need to be logged in as root or as a user with sudo privileges.
This post is a starter of our user management in Linux post series. Next line of actions in user management is managing user groups. That process is covered in this post and can be checked were we covered steps how to create user groups and add and remove user to a group/from a group.
How to create a user in Linux
How to create a user with adduser command
We’ll first create a new user with the “adduser” command. The “adduser” command will create a new user with a home directory automatically, and ask to set the password for the new user as well, create a default user settings(adding user details in /etc/passwd file, add a unique user ID, etc). Once you are in a terminal to create a new user you will type “adduser” and it’s “username”. You will be asked for your sudo password.
sudo adduser newuser
Once you type your password , you will be asked series of questions , from which password is needed for the new user ,and all other questions are optional.
After you will just need to confirm the information with “y”. And you have successfully created a new user. This command will also create a new users home directory.
Also to put in your mind that in Ubuntu by default members of the sudo group are granted with sudo access. Meaning, the newly created user will not be in the sudo user group by default and you’ll need to add it.
How to create a user with useradd command
When you create a new user with the command “useradd”, it will just create an “empty” user. It will not have a home directory, it will not have a password set and not be a part of any user groups. That means, after the user creation with the “useradd” command you’ll need to set everything separately.
sudo useradd newuser2
There are of course options for this command which can add and set other user settings that are needed. For an example, if you need to a have a home directory for a new user, you then need to add the option “-m” on the “useradd” command. The command looks like this:
sudo useradd -m newuser2
After the user creation with the “useradd” command, the user won’t be able to login since it doesn’t have a password set(the command doesn’t set it). The password can easily set with the command “passwd”.
sudo passwd newuser2
We can say, essentially if you need a to create a user in Linux quickly with one command, with a home directory, password and everything else desktop user has, use the – adduser command. For more control and user management on servers, better to go with useradd command.
Add user sudo privileges to a new user
To add the sudo permission to the new user, we use the “usermod” tool to modify the user. The tool needs to be run with sudo. Run the command like this:
For Debian/Ubuntu based distros
sudo usermod -aG sudo newuser
sudo usermod -aG wheel newuser
How to delete a user in Linux
For this action you will use command “deluser” and type it’s “username”. For this command as well you need to run it with sudo or either run as root.
sudo userdel newuser
After you type in the sudo/root password – the user and it’s settings will be deleted, but it’s home directory and files won’t be deleted. To delete a user with it’s home directory all together at once, add the “-r” option in the command. The command will look like this:
sudo userdel -r newuser
Now you have mastered how to create a user in Linux and how to delete a user in Linux. Also to point out that all commands that we have used in this article, can of course apply in any Ubuntu-based distribution, and other distributions as well such as Debian, Kubuntu, Pop OS and Linux Mint, CentOS, Red Hat etc.
Thank you for your time…