The useradd
command is used to create a new user. Usually, the useradd
command comes with the -m
option, which can create the home directory for the new user under the path of /home
with the user name. To run this command, you need to run it with superuser privileges.
Adding a user is a privilege of the superuser. When you run the command, you need to run it as root
or with the sudo
command. If you run the command as a normal user without sudo, you'll see a permission error like the one below.
useradd user_a
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
To create a new user, switch to the superuser and run the useradd
command. Use the -m
option to create the home directory for the new user.
sudo su -
useradd -m user_a
The user data is recorded under the /etc/passwd file. To check the file, run the following command.
cat /etc/passwd
You can see that a new user is added at the bottom of the file.
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
:
:
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
lxd:x:998:100::/var/snap/lxd/common/lxd:/bin/false
user_a:x:1001:1001::/home/user_a:/bin/sh
In this file, the following information is covered (left to right):
You can also use the grep
command to see only the necessary part of the user data. For example, what we need to know is the user data which has a string with "user" in it. Run the following command to get the data.
grep user /etc/passwd
You'll get the result like the one below.
user_a:x:1001:1001::/home/user_a:/bin/sh
There are several options for the useradd
command. You can use these options to customize user data.
-u
: specify user ID-g
: specify primary group ID-c
: specify user’s comment-s
: specify login shell-d
: specify home directory pathFor practice purposes, let's create three users: user_a, user_b, and user_c. First, switch to the superuser.
sudo su
Run the useradd commands three times.
useradd -m user_a
useradd -m user_b
useradd -m user_c
Check the user data with the grep
command.
grep user /etc/passwd
You'll see that three user accounts have been created.
user_a:x:1001:1001::/home/user_a:/bin/sh
user_b:x:1002:1002::/home/user_b:/bin/sh
user_c:x:1003:1003::/home/user_c:/bin/sh
You can further switch to another user by running the su
command. To switch to user_a, run the command below.
su - user_a
You can see that #
changed to $
as you are back to the normal user. Also, you can see that the home directory has changed to the user_a directory by running the pwd
command.
pwd
/home/user_a
To go back to the ubuntu user, use the exit
command twice as shown below.
exit
exit
exit
ubuntu:~$
The useradd
command is used to create a new user. Usually, the useradd
command comes with the -m
option, which can create the home directory for the new user under the path of /home
with the user name. To run this command, you need to run it with superuser privileges.
Adding a user is a privilege of the superuser. When you run the command, you need to run it as root
or with the sudo
command. If you run the command as a normal user without sudo, you'll see a permission error like the one below.
useradd user_a
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
To create a new user, switch to the superuser and run the useradd
command. Use the -m
option to create the home directory for the new user.
sudo su -
useradd -m user_a
The user data is recorded under the /etc/passwd file. To check the file, run the following command.
cat /etc/passwd
You can see that a new user is added at the bottom of the file.
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
:
:
systemd-coredump:x:999:999:systemd Core Dumper:/:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
lxd:x:998:100::/var/snap/lxd/common/lxd:/bin/false
user_a:x:1001:1001::/home/user_a:/bin/sh
In this file, the following information is covered (left to right):
You can also use the grep
command to see only the necessary part of the user data. For example, what we need to know is the user data which has a string with "user" in it. Run the following command to get the data.
grep user /etc/passwd
You'll get the result like the one below.
user_a:x:1001:1001::/home/user_a:/bin/sh
There are several options for the useradd
command. You can use these options to customize user data.
-u
: specify user ID-g
: specify primary group ID-c
: specify user’s comment-s
: specify login shell-d
: specify home directory pathFor practice purposes, let's create three users: user_a, user_b, and user_c. First, switch to the superuser.
sudo su
Run the useradd commands three times.
useradd -m user_a
useradd -m user_b
useradd -m user_c
Check the user data with the grep
command.
grep user /etc/passwd
You'll see that three user accounts have been created.
user_a:x:1001:1001::/home/user_a:/bin/sh
user_b:x:1002:1002::/home/user_b:/bin/sh
user_c:x:1003:1003::/home/user_c:/bin/sh
You can further switch to another user by running the su
command. To switch to user_a, run the command below.
su - user_a
You can see that #
changed to $
as you are back to the normal user. Also, you can see that the home directory has changed to the user_a directory by running the pwd
command.
pwd
/home/user_a
To go back to the ubuntu user, use the exit
command twice as shown below.
exit
exit
exit
ubuntu:~$