Useradd vs Adduser: What's the Difference?
What is the difference between useradd and adduser commands in Linux? Which one is better and which one should you be using. This article answers all these questions.
When it comes to creating a new user in Linux, you have the option to use either adduser or useradd command.
So what’s the difference between the useradd and adduser commands? Is one of them better than the other? Which one should you use? useradd or adduser?
Difference between adduser and useradd commands
Useradd is built-in Linux command that can be found on any Linux system. However, creating new users with this low-level is a tedious task because it doesn't create the home directory and user password by default.
Adduser is not a standard Linux command. It’s essentially a Perl script that uses the useradd command in the background. This high-level utility is more efficient in properly creating new users on Linux. It gives you the option to create the home directory, and set password along with a few more parameters.
Let’s take a look at both these commands and how they create users in Linux.
Useradd command
First, let’s have a look at the default behavior of the useradd command. Have you ever run the following command?
useradd new_user
I am yet to see a distribution where this command has created a complete user account. Even after setting a password for the new user, you would need to use additional options with it.
For example, on my system, the above command added the following entry in the /etc/passwd file.
If you read the article on listing users on Linux, you probably already are familiar with the content of the /etc/passwd file. It looks good at first, but if you start digging, you will soon notice a few things are not quite right.
First, the second field contains ‘x’. This means that the placeholder for password information is in /etc/shadow. But I have not set a password. This means test user will not be able to log in.
Second, the home directory. The thing is that /home/test does not exist. Even after you set a password, an error will occur if the user logs in. Depending on other configurations, your Linux system may prevent a user without a valid home directory to login in. Most probably, user test would be allowed to log in on your system, at least in text mode. But user test will not be allowed to create his home directory.
Third, the default login shell is sh, not bash. I don’t use sh as a shell. You probably don’t either because bash is the default shell on most Linux distributions.
While useradd’s behavior can be slightly modified by editing /etc/login.defs, there is little to no chance that the above command will create a complete user account. Useradd is a low-level utility. Still, looking through the settings in /etc/login.defs can be useful.
Adduser command
Adduser command is not available on some Linux distributions. On others, it is a soft link to useradd. While on some others, it is a Perl script.
Adduser is an interactive high-level utility. It uses the low-level utility useradd as a backend. Settings in /etc/login.defs will be used while using the adduser command. On Debian-based distros, even the man page recommends its use over the useradd command.
Calling adduser with just a username will walk you through a series of questions.
After answering a few questions, a complete user account with a home directory will be created. The new user is ready to log in through your login manager or in text mode.
To accomplish almost the same result using the low-level utility useradd, the command would look similar to this:
sudo useradd -d /home/test -m -s/bin/bash \ -c FullName,Phone,OtherInfo test && passwd test
The -c options can be removed without problems, the comma-separated list that follows are comments added to /etc/passwd. But even then, adduser saves you two additional commands.
Adduser command has a list of options available to you. Here is a short list I think will be most useful to know. Refer to the help or man page for more details.
- system: Add a system user. By default, system users are placed in nogroup group. To add a system user to an existing group, provide –gid or –ingroup option.
- home DIR: Use DIR as home directory instead of the default. Directory will be created if needed and skeleton files will be copied.
- shell SHELL: Use SHELL instead of the default.
- ingroup GROUP: Set user’s primary group to GROUP
- add_extra_groups: Add new user to an extra group defined in the configuration file.
Adduser’s configuration file lets you set the default values to be used when creating the account. The file is well documented and lets you set default values such as:
- Default shell
- Home directory
- Extra groups
- Add extra groups
Conclusion
In my personal experience as a system administrator, adduser is a lot better at creating new users in Linux. I prefer adduser over useradd any given day and I recommend the same.
What about you? Which one do you prefer between adduser and useradd?
Linux has been my passion for around 2 decades. I enjoy compiling 'Linux from Scratch'.