How to create a user account in Ubuntu
Are you still using the root user to host your applications on your VPS? Stop doing that! It’s not a good idea to host your applications on your VPS with the root user. Therefore, you must create a separate, non-root user account to run your applications. This simple guide will help you create a user account in linux, and also switch between users, and deleting them.
Prerequisites
- Basic knowledge of Linux commands
- Ubuntu machine with root access
If you don’t know how to set up a VPS, you can refer to this tutorial here.
Creating a user account
You can use this command to create a user account on linux. If you are not logged in as the root user, you have to use the sudo
prefix. sudo
means Super User Do, which you are exercising root
privileges while being logged in as a non-root user. However, this non-root user must have sudo
privileges.
To create a user, you can use the following command.
sudo adduser <username>
This will prompt you to set a password for the user, and you will have to confirm the password as well. This will also prompt for more information about the user such as the Full Name
. For any detail you don’t want to provide, just hit enter.
Adding user `test' ...
Adding new group `test' (1001) ...
Adding new user `test' (1001) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []: Test User
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
Granting sudo privileges to the user
If you want to perform root
activities from the logged in non-root user, you need to add that user to the Super User Doers
group. For that, enter the following command with root privileges (either use sudo
prefix or run it while logged in as root).
usermod -aG sudo <username>
Now your newly created user can run sudo
commands.
Switching between users
If you want to switch from the current logged in user, you can use the su
command. su
command stands for Switch User.
To view the currently logged in user, you can type,
whoami
To switch to the root user, you can type,
sudo su
To switch to another non-root user, you can use
su <username>
Note: Always be mindful about the currently logged in user, as you don’t want to do anything dangerous.
Deleting users
To delete a user, you should have root privileges, and the user you are going to delete must not be active. You can log out of the current terminal session to make all users log out. (Given only you are using the machine at the given time).
Then you can use the following command to delete a user.
deluser <username>
However, this does not delete the home directory and other files that are owned by the user. To remove the home directory, you can use the same command with the following flag.
deluser <username> --remove-home
To remove all files owned by the user, you can use the same command with the following flag.
deluser <username> --remove-all-files
The --remove-all-files
flag will also delete the home directory as well.
Conclusion
I hope you gained some knowledge on how to manage users in an Ubuntu environment. Thank you for reading 🥳.