In this article, we will cover the following recipes:
In this article by Uday Sawant, the author of the book Ubuntu Server Cookbook, you will see how to add new users to the Ubuntu server, update existing users. You will get to know the default setting for new users and how to change them.
(For more resources related to this topic, see here.)
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.
You will need super user or root privileges to add a new user to the Ubuntu server.
Follow these steps to create the new user account:
$ sudo adduser bob
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:
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:
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 that if you skip the username part from the preceding command you will end up changing the password of root account.
With adduser, you can do five different tasks:
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
In this recipe, we will see how to create multiple user accounts in batch mode without using any external tool.
You will need a user account with root or root privileges.
Follow these steps to create a user account in batch mode:
$ touch users.txt
$ chmod 600 users.txt
$ nano users.txt
We created a database of user details listed in format as the passwd file. The default format for each row is as follows:
username:passwd:uid:gid:full name:home_dir:shell
Where:
The new user command reads each row and updates the user information if user already exists, or it creates a new user.
We made the users.txt file accessible to owner only. This is to protect this file, as it contains the user's login name and password in unencrypted format.
Group is a way to organize and administer user accounts in Linux. Groups are used to collectively assign rights and permissions to multiple user accounts.
You will need super user or root privileges to add a group to the Ubuntu server.
$ sudo addgroup guest
Here, we are simply adding a new group guest to the server. As addgroup needs root privileges, we need to use sudo along with the command. After creating a new group, addgroup displays the GID of the new group.
Similar to adduser, you can use addgroup in different modes:
In this article, we have discussed how to create user account, how to create a group and also about how to create user accounts in batch mode.
Further resources on this subject: