Creating a user account
While installing Ubuntu, we add a primary user account on the server; if you are using the cloud image, it comes preinstalled with the default user. This single user is enough to get all tasks done in Ubuntu. There are times when you need to create more restrictive user accounts. This recipe shows how to add a new user to the Ubuntu server.
Getting ready
You will need super user or root privileges to add a new user to the Ubuntu server.
How to do it…
Follow these steps to create the new user account:
- To add a new user in Ubuntu, enter following command in your shell:
$ sudo adduser bob
- Enter your password to complete the command with
sudo
privileges: - Now enter a password for the new user:
- Confirm the password for the new user:
- Enter the full name and other information about the new user; you can skip this part by pressing the Enter key.
- Enter
Y
to confirm that information is correct: - This should have added new user to the system. You can confirm this by viewing the file
/etc/passwd
:
How it works…
In Linux systems, the adduser
command is higher level command to quickly add a new user to the system. Since adduser
requires root privileges, we need to use sudo
along with the command, adduser
completes following operations:
- Adds a new user.
- Adds a new default group with the same name as the user.
- Chooses UID (user ID) and GID (group ID) conforming to the Debian policy.
- Creates a
home
directory with skeletal configuration (template) from/etc/skel
. - Creates a password for the new user.
- Runs the user script, if any.
If you want to skip the password prompt and finger information while adding the new user, use the following command:
$ sudo adduser --disabled-password --gecos "" username
Alternatively, you can use the useradd
command as follows:
$ sudo useradd -s <SHELL> -m -d <HomeDir> -g <Group> UserName
Where:
-s
specifies default login shell for the user-d
sets thehome
directory for the user-m
creates ahome
directory if one does not already exist-g
specifies the default group name for the user
Creating a user with the command useradd
does not set password for the user account. You can set or change the user password with the following command:
$sudo passwd bob
This will change the password for the user account bob
.
Note
Note that if you skip the username part from the above command you will end up changing the password of the root account.
There's more…
With adduser
, you can do five different tasks:
- Add a normal user
- Add a system user with system option
- Add user group with the
--group
option and without the--system
option - Add a system group when called with the
--system
option - Add an existing user to existing group when called with two non-option arguments
Check out the manual page man adduser
to get more details.
You can also configure various default settings for the adduser
command. A configuration file /etc/adduser.conf
can be used to set the default values to be used by the adduser
, addgroup
, and deluser
commands. A key value pair of configuration can set various default values, including the home
directory location, directory structure skel
to be used, default groups for new users, and so on. Check the manual page for more details on adduser.conf
with following command:
$ man adduser.conf
See also
- Check out the command
useradd
, a low level command to add new user to system - Check out the command
usermod
, a command to modify a user account - See why every user has his own group at http://unix.stackexchange.com/questions/153390/why-does-every-user-have-his-own-group