JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Detailed introduction on how to add and delete users on Ubuntu 18.04 system

Author:JIYIK Last Updated:2025/04/07 Views:

Adding and removing users on a Linux system is one of the most important system administration tasks we need to be familiar with. When a new system is created, usually only the root account is accessible by default.

While running as the root user gives us complete control over the system and its users, it is also dangerous and potentially destructive. For common system administration tasks, it is better to add a non-privileged user and perform those tasks without root privileges. You can also create additional non-privileged accounts for any other users you may have on the system. Each user on the system should have their own separate account.

For tasks that require administrator privileges, a tool called sudo is installed on Ubuntu systems. In short, sudo allows us to run commands as other users, including users with administrative privileges. In this article, you will learn how to create user accounts, assign sudo privileges, and delete users.

Adding Users

If you are logged in as root, you can create a new user at any time by running:

$ adduser newuser

If you are logged in as a non-root user with sudo privileges, you can add a new user with the following command:

$ sudo adduser newuser

Either way, we need to answer a series of questions:

  • Assign and confirm a password for the new user.
  • Enter any additional information about the new user. This is optional and you can press ENTER to skip if you do not want to use these fields.
  • Finally, you will be asked to confirm that the information we provided is correct. Press Y to continue.
  • The new user is now ready to use and can log in using the password we entered.

If you need the new user to have administrative privileges, continue to the section below.

Granting Sudo Privileges to Users

If the new user should be able to execute commands with root (administrative) privileges, you will need to grant the new user sudo access. Let's look at two ways to accomplish this task: first, adding the user to the predefined sudo user group, and second, specifying permissions for each user in sudo's configuration.

Add the new user to the sudo group

By default, sudo on Ubuntu 18.04 systems is configured to extend full permissions to any user in the sudo group.

You can use the groups command to view the groups to which the new user belongs:

$ groups newuser
newuser : newuser

By default, new users are only in their own group because adduser creates it outside of the user profile. A user and its own group share the same name. In order to add a user to a new group, the usermod command can be used:

$ usermod -aG sudo newuser

-aGThe -p option instructs usermod to add the user to the listed groups.

Note that the usermod command itself requires sudo privileges. This means that you can only add a user to the sudo group if you are logged in as root or another user that has been added as a member of the sudo group. In the latter case, we must prefix this command with sudo, as in this example:

$ sudo usermod -aG sudo newuser

Specify explicit user permissions in /etc/sudoers

As an alternative to putting a user into the sudo group, you can use the sudo command, which opens a configuration file called /etc/sudoersvisudo in your system's default editor and explicitly specifies the permissions for each user.

Using visudo is the only recommended way to make changes to /etc/sudoers because it locks the file to prevent multiple simultaneous edits and performs validation checks on its contents before overwriting the file. This helps prevent situations where you misconfigure sudo and are unable to resolve the problem due to loss of sudo privileges.

If you are currently logged in as root, run the following command:

$ visudo

If you are logged in as a non-root user with sudo privileges, run the same command with the sudo prefix:

$ sudo visudo

Traditionally, visudo opens /etc/sudoers in the vi editor, which can be confusing to inexperienced users. By default, on a new Ubuntu installation, visudo will use the nano text editor, which provides a more convenient and accessible text editing experience. Use the arrow keys to move the cursor, and search for a line that looks like this:

/etc/sudoers

root    ALL=(ALL:ALL) ALL

Below this line, add the following highlighted line. Be sure to change newuser to the name of the user profile you want to grant sudo privileges to:

/etc/sudoers

root    ALL=(ALL:ALL) ALL
newuser ALL=(ALL:ALL) ALL

Add a new line like this for each user that should be given full sudo privileges. When you are finished, CTRL + Xsave and close the file by pressing Y and then ENTER to confirm.

Test the user's sudo privileges

Now the new user can execute commands with administrative privileges.

Once you are logged in as the new user, you can execute commands as a normal user by typing commands normally:

$ some_command

The same command can be executed with administrative privileges by typing sudo before the command:

$ sudo some_command

When doing this, we will be prompted to enter the password of the regular user account we are logged in as.

Deleting a User

If a user is no longer needed, it is best to delete the old account.

We can remove the user itself, without deleting any of their files, by running the following command as root:

$ deluser newuser

If you are logged in as another non-root user with sudo privileges, you would use the following:

$ sudo deluser newuser

If, instead, you want to delete the user's home directory when you delete the user, you can execute the following command as root:

$ deluser --remove-home newuser

If you run this command as a non-root user with sudo privileges, you will run the same command with the sudo prefix:

$ sudo deluser --remove-home newuser

If we previously configured sudo permissions for the deleted user, we may need to delete the relevant lines again:

$ visudo

Alternatively, as a non-root user with sudo privileges, use the following command:

$ sudo visudo

/etc/sudoers

root    ALL=(ALL:ALL) ALL
newuser ALL=(ALL:ALL) ALL   # 删除这一行

This will prevent new users created with the same name from being accidentally granted sudo privileges.

Summarize

By now we should have a fairly good understanding of how to add and remove users from our Ubuntu 18.04 system. Effective user management will allow us to separate users and grant them only the access they need to do their job.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

How to Install Nginx on Ubuntu 20.04?

Publish Date:2025/04/07 Views:157 Category:OPERATING SYSTEM

Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.

How to Update Ubuntu Scrolling Speed

Publish Date:2025/04/07 Views:77 Category:OPERATING SYSTEM

It's strange that there's no default GUI provided for us to update the scroll speed on Ubuntu, this is one of the more obvious configuration options missing. We will use imwheel to adjust the scroll speed of different applications. First cr

Install WordPress with Nginx on Ubuntu 18.04

Publish Date:2025/04/07 Views:86 Category:OPERATING SYSTEM

WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi

Linux: How to install htop on Ubuntu and Mint

Publish Date:2025/04/06 Views:129 Category:OPERATING SYSTEM

While top the htop utility provides us with basic information about the processes running on our Linux system, the htop utility goes a step further and provides us with more information about the processes and also allows for several operat

How to Install GCC Compiler on Ubuntu 18.04

Publish Date:2025/04/06 Views:165 Category:OPERATING SYSTEM

GCC It is the abbreviation of GNU Compiler Collection , which is applicable to different programming languages ​​such as R, C, C++, Objective-C, Fortran, Ada, Go, D, etc. We can apt install the compiler on Ubuntu using the command line

Configuring Apache Web Server on Ubuntu and Debian

Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM

This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial