How to Create and Delete a User on Debian 9

 

 

In this tutorial, we will show you how to create and delete a user on Debian 9. In the initial server setup, there is only one user account created and that is “root” user account. This root user has the highest privileges and has access to all files and commands on the system. But having too many privileges and running as root user is not always recommended and if you are not careful enough it can have destructive consequences for your system.

That’s why it is recommended to create additional users with limited privileges for your most common tasks. A new user account should also be created for any other user that will be using your server. Additionally, we will also show you how to give those users root privileges when necessary through the sudo command.

Create a new User on Debian 9

First, you need to establish a connection to your server as user root via SSH.

Once logged, you can create a new user with the adduser command. In our example, we will create a user called “newuser”. To do this, simply type in the following in your command line:

adduser newuser

You will be asked some additional questions.
First, you will need to enter and confirm a password for this user.
Then you will be asked for some additional information about the user, such as full name, room number, work phone, home phone and other. This information is optional and you can just press ENTER on each question to skip it.
In the end, you will be asked for confirmation of all the information you have entered so far. If everything is correct just press Y and then Enter.

This is the output that you should get, for our new user called “newuser”:

Adding user `newuser' ...
Adding new group `newuser' (1000) ...
Adding new user `newuser' (1000) with group `newuser' ...
Creating home directory `/home/newuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for newuser
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y

With this, a new user and group named “newuser” have been successfully created. Its home directory has also been created at the following location on your server: /home/newuser

You can now log in as the “newuser” user to your server using the password you have set up.

Add User to the sudo Group on Debian 9

We will now show you ho to allow this new user you have just created, to execute commands as the superuser or another system user. To achieve this, you will need to add the user to the sudo group. All members of this group have sudo privileges.
By default, all new users are only added to their own group during the initial user creating. To add our “newuser” user to the sudo group, you need to run the following command:

usermod -aG sudo newuser

You can also verify if our user is now a member of the sudo group , with the groups commands:

groups newuser

You should get the following output, showing that “newuser” is a member of both newuser and the sudo group:

newuser : newuser sudo

You can now switch to this user with the su command:

su newuser

and try to run commands with administrative privileges by using the sudo command. For example, a normal user does not have the privileges to run the commands necessary for upgrading all the system software on the server to the latest version:

apt-get update && sudo apt-get upgrade

If you try to run these commands normally, while logged in as the “newuser” user you will get the following output:

Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/

But since our user is added to the sudo group, we can use the sudo command to successfully update our system:

sudo apt-get update && sudo apt-get upgrade

Delete a User on Debian 9

Once you no longer need the user we have created, we can delete it with the deluser command.

Let’s say we want to delete the user “newuser”. You can run the following command to do this:

deluser newuser

However, this will not delete the user’s home directory. If you want to delete the user’s home directory as well, you should run the following command:

deluser --remove-home newuser

The user is now completely removed from our system.

This is all you need to know when it comes to creating and deleting a user on Debian 9.

Original Article

Tags: