When managing a server, you’ll sometimes want to allow users to execute commands as “root,” the administrator-level user. The sudo
command provides system administrators with a way to grant administrator privileges — ordinarily only available to the root user — to normal users.
In this tutorial, you’ll learn how to create a new user with sudo
access on Ubuntu without having to modify your server’s /etc/sudoers
file.
Note: If you want to configure sudo
for an existing user, skip to step 3.
Also, if you are using an older version of Ubuntu, we recommend you upgrade to a more latest version since Ubuntu no longer provides support for these versions. This collection of guides will help you in upgrading your Ubuntu version.
SSH in to your server as the root user:
- ssh root@your_server_ip_address
Use the adduser
command to add a new user to your system:
- adduser sammy
Be sure to replace sammy
with the username that you want to create. You will be prompted to create and verify a password for the user:
- OutputEnter new UNIX password:
- Retype new UNIX password:
- passwd: password updated successfully
Next, you’ll be asked to fill in some information about the new user. It is fine to accept the defaults and leave this information blank:
- OutputChanging the user information for sammy
- 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]
Use the usermod
command to add the user to the sudo group:
- usermod -aG sudo sammy
Again, be sure to replace sammy
with the username you just added. By default on Ubuntu, all members of the sudo group have full sudo
privileges.
sudo
AccessTo test that the new sudo
permissions are working, first use the su
command to switch to the new user account:
- su - sammy
As the new user, verify that you can use sudo
by prepending sudo
to the command that you want to run with superuser privileges:
- sudo command_to_run
For example, you can list the contents of the /root
directory, which is normally only accessible to the root user:
- sudo ls -la /root
The first time you use sudo
in a session, you will be prompted for the password of that user’s account. Enter the password to proceed:
- Output:[sudo] password for sammy:
Note: This is not asking for the root password! Enter the password of the sudo-enabled user you just created.
If your user is in the proper group and you entered the password correctly, the command that you issued with sudo
will run with root privileges.
sudo
and su
Both sudo
and su
are Linux commands that allow you to execute commands with elevated privileges, but they approach this task differently.
sudo
(SuperUser Do _or Substitute User Do) lets a permitted user run a command as another user (by default, the root
user) by authenticating with their own password. It’s designed for executing a single command or a series of commands for a limited time, and its access is highly configurable through the /etc/sudoers
file. A key benefit of sudo
is that it provides a robust audit trail, logging each command executed with escalated privileges, making it a generally more secure practice for daily administrative tasks as it avoids sharing the root password directly.
On the other hand, su
(Substitute User or Switch User) allows you to completely switch to another user’s account and requires the password of the target account. When you use su
(especially with the - or --login option, like su -), you enter a full login session as that user, inheriting their environment variables and home
directory. This grants full access to the target user’s environment and privileges for the entire session until you explicitly exit. While simple for switching users, su
doesn’t inherently log individual commands executed within the session, and it can be less secure if the root
password is widely known.
In many system administration scenarios, you might need to create user accounts that are not intended for interactive login by a human user but used by applications, scripts, or specific services (like an FTP server, web server, or database) to manage files, run processes, or interact with other system components. For security reasons, it’s vital to prevent these accounts from being used for direct shell access.
You can create such users by specifying a “no-login” shell during the account creation process. The two most common no-login shells are /usr/sbin/nologin
and /bin/false
. Both effectively prevent a user from obtaining an interactive shell session upon login. Choosing either one effectively locks out interactive shell access for the user.
adduser
to Create a No-Login UserWhen using adduser
, you can specify the shell with the --shell
option:
- sudo adduser --system --no-create-home --shell /usr/sbin/nologin serviceuser
Let’s break down the different flags:
--system
: This option creates a “system user.” System users typically have UIDs (User IDs) below 1000 (though this range can vary), distinguishing them from regular interactive users. They are also often excluded from password expiration policies and certain system reports.--no-create-home
: For service accounts, a separate home directory might not be necessary, especially if the account only needs to access specific predefined directories. This option prevents adduser
from creating a home
directory. If the service does require a directory for its own files, omit this and specify a different home path.--shell /usr/sbin/nologin
: This is the crucial part. It assigns /usr/sbin/nologin
as the user’s default shell. When this user attempts to log in, they will typically receive a message like “This account is currently not available” and the connection will be closed.For example, to create a system user named ftpuser
without a home
directory and no shell access, suitable for an FTP daemon, we use the following command:
- sudo adduser --system --no-create-home --shell /usr/sbin/nologin ftpuser
useradd
to Create a No-Login UserIf you prefer useradd
for its more direct control, you can use it to achieve the same result:
- sudo useradd -r -s /usr/sbin/nologin serviceuser
Let’s understand the flags:
-r
: This option creates a system account (similar to adduser --system
).-s /usr/sbin/nologin
: Explicitly sets /usr/sbin/nologin
as the user’s login shell.By default, useradd
does not create a home
directory unless the -m
option is used. So, omitting -m
will result in no home directory, which is often desired for these types of accounts.
To create a system user named db_reader
with no shell access and no home
directory, we can use the following command:
- sudo useradd -r -s /usr/sbin/nologin db_reader
sudo -l
to List Allowed CommandsAs an administrator or a regular user, it’s often useful to quickly check what sudo
privileges you or another user possess on a system. The sudo -l
command provides this capability by listing the commands a user is allowed to execute via sudo
.
To see your own sudo privileges, simply run:
- sudo -l
You will typically be prompted for your password. After successful authentication, sudo
will display a list of rules that apply to your user account in the /etc/sudoers
file or its included directories.
If you have sudo
access yourself, you can use sudo -l
to check the privileges of another user by specifying their username with the -U
option:
- sudo -U another_username -l
Replace another_username
with the actual username you want to check. This is an invaluable tool for administrators to audit and verify user permissions quickly without having to directly inspect the /etc/sudoers
file.
sudo
CommandsLet’s discuss some of the best practices for managing user permissions and limiting sudo
commands, crucial for maintaining a secure and robust Ubuntu system:
User permissions dictate who can read, write, or execute files and directories on your system. A strong permission strategy is built on the Principle of Least Privilege (PoLP), meaning users and processes should only have the minimum necessary access to perform their intended functions.
chmod
command.umask
setting (usually in .bashrc
or system-wide configuration) determines the default permissions for newly created files and directories. Ensure your umask is appropriately restrictive.sudo
CommandsGranting full sudo access (i.e., allowing a user to run ALL=(ALL:ALL) ALL in /etc/sudoers) is the same as giving them the root
password. While convenient, it poses a significant security risk. Best practices dictate limiting sudo privileges to only what is absolutely necessary.
"ALL"
commands, specify the exact command(s) they need./usr/sbin/service
instead of just service). This prevents a malicious user from creating a custom executable with the same name elsewhere in the system’s PATH
and tricking sudo
into running their malicious version.NOPASSWD
tag allows a user to execute a sudo
command without being prompted for their password. Only use NOPASSWD
for commands that are genuinely harmless, non-system-altering, and frequently executed, such as status checks. For any command that involves system modification or access to sensitive data, a password should always be required.sudo
cleans a user’s environment before executing commands to prevent privilege escalation via manipulated environment variables (like PATH
). Avoid using the SETENV
tag in your sudoers rules unless it is absolutely essential for a specific application’s functionality, as it can introduce security vulnerabilities.adduser
and useradd
?adduser
is a high-level, and user-friendly script that simplifies the process of creating a new user account. It interactively prompts for information, automatically creates a home directory, copies skeleton files (like .bashrc
, .profile
), sets appropriate permissions, and assigns a default shell. In most cases, it’s the recommended command for creating users on Debian-based systems like Ubuntu.
useradd
is a lower-level binary that provides fine-grained control over user creation. It does not create a home directory or copy skeleton files by default, requiring these to be specified with options or handled manually. It’s often used in scripts or for automated system management where precise control and non-interactive operation are necessary.
The simplest way to check if a user has sudo
privileges is to use the getent
command to inspect the /etc/group
file:
- getent group sudo
This command will list the members of the sudo
group. If the username you’re checking is listed, they have sudo privileges.
Alternatively, the user can try running a command with sudo
, such as:
- sudo ls /root
If they are able to list the contents of /root
(which requires root privileges), they have sudo
access. If they are not, they will be prompted for a password, and if they are not in the sudo
group, the command will fail.
Yes, it is generally considered safe and even a good security practice to disable the root
account after creating a user with sudo
privileges. Disabling direct root
login reduces the attack surface of your system. Instead of logging in as root
, administrators can log in with their regular user accounts and use sudo
to perform administrative tasks.
You can disable the root
account by locking its password:
- sudo passwd -l root
This prevents direct login as root
. To re-enable the root
account, you can unlock it:
- sudo passwd -u root
Instead of granting full sudo
access (allowing a user to run any command as root), you can configure limited sudo access, allowing a user to run only specific commands as root. This is done by editing the /etc/sudoers
file using the visudo
command.
For example, to allow the user webadmin
to restart the Apache web server:
- sudo visudo
Then, add a line like this:
- webadmin ALL=(ALL:ALL) /usr/sbin/service apache2 restart
This line specifies that the user webadmin
can run the command /usr/sbin/service apache2 restart
as root
.
Caution: Incorrect modifications to the /etc/sudoers
file can severely impact system administration. Always be careful when using visudo
.
In this quickstart tutorial, we created a new user account and added it to the sudo group to enable sudo
access. We covered the differences between sudo
and su
, and learned how to create an user without shell access for service accounts. We also discussed the best practices to be followed while granting user permissions and limiting sudo
commands.
For your new user to be granted external access, please follow our section on Enabling External Access for Your Regular User.
If you need more detailed information on setting up an Ubuntu server, please choose your distribution from this list to follow your specific Initial Server Setup Guide.
For more Ubuntu-related topics, you can refer to the following articles:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya all,
For anyone stumbling on this tutorial, I think you might find this interesting:
https://www.digitalocean.com/community/questions/a-deep-dive-into-the-sudoers-file
In this mini-tutorial, we do a deep dive into the sudoers files and how you can work wonders with it.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.