I am trying to add a new user to the system. I am confused though which command to run: useradd
or adduser
?
Debian – Should I use useradd or adduser
administrationdebianuseraddusers
Related Solutions
First off, the respective man page snippets highlight the differences between the two commands and give some indication of what is going on. For adduser
:
adduser and addgroup add users and groups to the system according to command line options and configuration information in /etc/adduser.conf. They are friendlier front ends to the low level tools like useradd, groupadd and usermod programs, by default choosing Debian policy conformant UID and GID values, creating a home directory with skeletal configuration, running a custom script, and other features.
Then for useradd
:
useradd is a low level utility for adding users. On Debian, administrators should usually use adduser(8) instead.
Further investigation of adduser
reveals that it is a perl script providing a high level interface to, and thus offering some of the functionality of, the following commands:
useradd
groupadd
passwd
- used to add/change users passwords.gpasswd
- used to add/change group passwords.usermod
- used to change various user associated parameters.chfn
- used to add/change additional information held on a user.chage
- used to change password expiry information.edquota
- used to change disk usage quotas.
A basic run of the adduser
command is as follows:
adduser username
This simple command will do a number of things:
- Create the user named
username
. - Create the user's home directory (default is
/home/username
and copy the files from/etc/skel
into it. - Create a group with the same name as the user and place the user in it.
- Prompt for a password for the user.
- Prompt for additional information on the user.
The useradd
program can most of accomplish most of this, however it does not do so by default and needs additional options. Some of the information requires more commands:
useradd -m -U username
passwd username
chfn username
Note that adduser
ensures that created UIDs and GIDs conform with the Debian policy. Creating normal users with useradd
seems to be ok, provided UID_MIN
/UID_MAX
in /etc/login.defs
matches the Debian policy. What is a problem though is that Debian specifies a particular range for system user UIDs which only seems to be supported in /etc/adduser.conf
, so naively adding a system user with useradd
and not specifying a UID/GUID in the correct range leaves the potential for serious problems.
Another common use for adduser
is to simplify the process of adding a user to a group. Here, the following command:
adduser username newgroup
is equivalent to the following usermod
command:
usermod -a -G newgroup username
The main drawback from usermod
in this case is that forgetting to pass the
append option (i.e.: -a
) would end up removing the user from all groups
before adding them to "newgroup" (i.e.: -G
alone means "replace with").
One downside to using adduser
here though is that you can only specify one group at a time.
The possible way to add an user is more or less similar to what I had put in the question. I got this approach from here.
To create a new account manually, follow these steps:
Edit /etc/passwd
with vipw
and add a new line for the new account. Be careful with the syntax. Do not edit directly with an editor. vipw
locks the file, so that other commands won't try to update it at the same time. You should make the password field be `*', so that it is impossible to log in.
Similarly, edit /etc/group
with vigr
, if you need to create a new group as well.
Create the home directory of the user with mkdir
.
Copy the files from /etc/skel
to the new home directory.
Fix ownerships and permissions with chown
and chmod
. The -R
option is most useful. The correct permissions vary a little from one site to another, but usually the following commands do the right thing:
cd /home/newusername
-
chown -R username.group .
-
chmod -R go=u,go-w .
-
chmod go= .
Set the password with passwd
.
After you set the password in the last step, the account will work. You shouldn't set it until everything else has been done, otherwise the user may inadvertently log in while you're still copying the files.
Best Answer
According to the manual you should use
adduser
: