All Posts

How to create and remove users in Linux

Posted by Mike on May 9th, 2022
server-users.png

Having multiple users on your server is a good way to help organise your server and can also aid with security. Users can be given different permissions which in turn means they can only access certain folders or run certain programs, so if a user becomes compromised, your whole server is not at risk!

Let's get started and create a user!

How to create a user

To create a user, you'll first need to SSH on to your server as root or a user with sudo access.

ssh your-user@12.23.123.12

Once you are logged in, simply run the following command, swapping out for your desired username.

sudo -S useradd -m YOURUSERNAME

This will create a user on your system with the specified username. It will also create a "home directory" for the user, within /home/, the home directory will be the username you specified in the command above.

The command above will not create a password for the user. If you have password authentication disabled for SSH, (which we recommend), you will need to create a .ssh folder and an authorized_keys file within the users home directory and drop your SSH keys into the file to allow SSH login.

sudo mkdir /home/YOURUSERNAME/.ssh && \
sudo touch /home/YOURUSERNAME/.ssh/authorized_keys && \
sudo chown -R YOURUSERNAME:YOURUSERNAME /home/YOURUSERNAME/.ssh

Once you've swapped out the username for your username, the command above will create the .ssh directory, create the authorized_keys file and then change the owner of the folder and files to ensure it's owned by that user.

Now grab your SSH key and drop it into the authorized_keys file we just created, you will then be able to login as that user via SSH.

How to remove a user

To remove a user, as with creating a user, you'll need to login to your server as root or a user with sudo access.

ssh your-user@12.23.123.12

Once you are logged in, simply run the following command, swapping out for your desired username.

sudo -S deluser YOURUSERNAME

This will remove the user from the server, stop programs from being able to use it during runtime and also stop you from being able to SSH into the server via that user.

That command however, will not fully remove all the users files. The users home directory located at /home/YOURUSERNAME will remain on the server. This means if you create a user with the same username then that home directory, will become the home directory for this user. If a .ssh folder is present, then anyone that previously had access to that user, will again have access to the new user.

By adding an option flag to that command above, you can also remove the home directory from the server and completely wipe the user.

sudo -S deluser --remove-home YOURUSERNAME

With that flag added, your /home/YOURUSERNAME directory will also be deleted keeping your server tidy!

Simplifying the process

If your server has been integrated with ServerAuth, you can simplify all this even more! With the ServerAuth platform you can simply enter a username into the Server Users management section to easily add a user to your server without ever logging in.

The following clips show you just how easy it is to create and remove a user from your server with ServerAuth.

As an extra bonus, the follow clip shows how simply it is to grant a team member access to the newly created user on your server!

Server Management & Security doesn't have to be a full time job.

ServerAuth provides a whole host of management tools, from controlling who can access your server, to adding cron jobs, securing your servers and installing packages. And with an ever growing suite of tools you'll always be one step ahead!

Start for free
Server Management Software Screenshot
ServerAuth
Server Management & SSH Security Software
 on Twitter
Copyright © ServerAuth Ltd
Registered in England No. 13996293
All Rights Reserved.
Solutions
Support
Customers
ServerAuth
The Legal Bits