How can I add a user as a new sudoer using the command line?

To add a new user as a sudoer in Ubuntu, follow these steps:

  1. Open a terminal and log in as the root user. You can do this by running the su command and then entering the root password.
  2. Use the adduser command to add a new user. Replace username with the desired username:
adduser username
  1. Add the new user to the sudo group by running the following command:
usermod -aG sudo username
  1. Verify that the user has been added to the sudo group by running the groups command:
groups username
  1. Log out of the root user account by running the exit command.
  2. Log in as the new user and verify that you can use sudo by running a command that requires superuser privileges, such as sudo apt update.

Note that you must have root privileges to add a user to the sudo group. If you do not have root privileges, you will need to ask your system administrator to add the user for you.

Here are a few additional details that may be helpful:

  • By default, members of the sudo group are allowed to use the sudo command to run any command as the root user. If you want to restrict the user’s sudo privileges, you can do so by editing the /etc/sudoers file. This file specifies which users are allowed to use sudo and which commands they are allowed to run.
  • To edit the /etc/sudoers file, you can use the visudo command. This command opens the file in a text editor and checks the syntax of the file before saving any changes.
  • You can also use the visudo command to add specific sudo privileges for a user. For example, to allow the user username to run the apt command without a password, you can add the following line to the /etc/sudoers file:
username ALL=(ALL) NOPASSWD: /usr/bin/apt

This will allow the user to run the apt command using sudo without being prompted for a password.

  • Be careful when editing the /etc/sudoers file, as incorrect changes can cause problems with your system. If you are not familiar with the /etc/sudoers file, it is generally best to ask your system administrator for assistance.

Related Solutions