How to Add a User on Linux and Give Sudo Rights
Add a user on Linux with adduser or useradd, give sudo rights, copy your SSH key, turn off root login, and lock, expire or delete users safely.

Contents
Adding a user on Linux is one of the first things to do on a new server. Working as root all the time means a single typo or a stolen password puts the whole server at risk, and root is the first user name automated attacks try. A regular user that runs admin commands with sudo only when needed is safer and leaves a record of who did what.
This guide covers creating a user with adduser and useradd, giving it sudo rights, moving your SSH key over, turning off root login safely, limiting sudo with visudo, and listing, locking and deleting users.
Creating a user: adduser or useradd
After connecting to your server over SSH as root, on Debian and Ubuntu the easiest way is adduser:
adduser alice
The command creates the /home/alice folder, sets the shell to bash, asks for a password and then for optional details such as the full name; you can skip those with Enter.
useradd is the low-level tool and works on every distribution. On Debian and Ubuntu it only does what you tell it, so add the home folder and shell yourself, then set the password:
useradd -m -s /bin/bash alice
passwd alice
On AlmaLinux, adduser is just another name for useradd, and it creates the home folder and uses bash by default. useradd alice followed by passwd alice is enough there.
Choose a strong password even if you'll log in with an SSH key: sudo asks for this password.
Giving sudo rights
Admin rights are given through a group. The group name depends on the distribution:
| Task | Debian / Ubuntu | AlmaLinux |
|---|---|---|
| Create a user | adduser alice | useradd alice |
| Give sudo rights | usermod -aG sudo alice | usermod -aG wheel alice |
| Delete with home | deluser --remove-home alice | userdel -r alice |
| Restart the SSH service | systemctl restart ssh | systemctl restart sshd |
usermod -aG sudo alice
The -a (append) part matters: -G without -a replaces the user's extra groups with only the ones you list. On minimal Debian installs sudo may be missing; install it first with apt install sudo.
Testing sudo
Switch to the new user and check that sudo works:
su - alice
sudo whoami
After you enter alice's password, the output should be root. If you get the alice is not in the sudoers file error, check the group with groups; group changes only apply to new sessions. Type exit to return to root.
Copying your SSH key to the new user
If you have an SSH key on your own computer and password login is still on, the easiest way is to copy it from your computer:
ssh-copy-id alice@203.0.113.10
If your key is already in root's authorized_keys (for example, added when the server was set up), copy it on the server and fix the owner and permissions:
mkdir -p /home/alice/.ssh
cp /root/.ssh/authorized_keys /home/alice/.ssh/
chown -R alice:alice /home/alice/.ssh
chmod 700 /home/alice/.ssh
chmod 600 /home/alice/.ssh/authorized_keys
SSH ignores the key if the folder or file is writable by others, so the 700/600 permissions and the owner are important. We explain these values in our Linux file permissions guide.
Now, without closing the current session, open a new terminal and test:
ssh alice@203.0.113.10
Turning off root and password login
Only after you've logged in as the new user with a key and confirmed that sudo whoami returns root, change these settings:
PermitRootLogin no
PasswordAuthentication no
On newer Ubuntu and AlmaLinux versions, /etc/ssh/sshd_config reads the files in /etc/ssh/sshd_config.d/ first, and for SSH the first value found wins. A file left by the installation, such as 50-cloud-init.conf with PasswordAuthentication yes, can override what you write in the main file. Find where these settings are set:
sudo grep -ri -E "PermitRootLogin|PasswordAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
Fix the value in every file you find, then check the syntax and the values that will actually apply:
sudo sshd -t
sudo sshd -T | grep -E "permitrootlogin|passwordauthentication"
If there's no error, restart the service: sudo systemctl restart ssh on Debian and Ubuntu, sudo systemctl restart sshd on AlmaLinux. Keep the old session open and test once more from a second session. If you're locked out, the web console in the Nodesty customer panel works independently of SSH. Fail2ban, covered in our How to install Fail2ban guide, is a good next layer.
visudo and the /etc/sudoers.d folder
Never edit the sudo rules with a regular editor: a single syntax error can break sudo entirely. visudo checks the syntax before saving. Instead of changing the main file, add your own rules as separate files:
sudo visudo -f /etc/sudoers.d/deploy
For example, to let a deploy user restart only one service without a password:
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart myapp
Write commands with their full path (which systemctl shows it). See which rights a user has with sudo -l -U deploy. Note that files in /etc/sudoers.d/ whose name contains a dot or ends with ~ are ignored.
Be careful with NOPASSWD. NOPASSWD: ALL means anyone who takes over that user is root without a password. Also, allowing commands such as vim, less, bash or tee with sudo is the same as giving full root rights, because they can start a shell or write to any file.
Listing users and groups
getent passwd
getent passwd | awk -F: '$3 >= 1000 {print $1}'
getent group sudo
id alice
groups alice
The first command lists every account, including system users. The second shows only regular users (UID 1000 and above; nobody also appears). getent group sudo (or wheel) shows who has admin rights, and id shows a user's UID and all groups. Check this list from time to time; a user you don't recognize is a warning sign. More everyday commands are in our Basic Linux commands guide.
Locking, expiring and deleting users
To lock a user's password temporarily:
sudo usermod -L alice
passwd -l alice does the same. Note that this only locks the password; the user can still log in with an SSH key. To block the account completely, also expire it, and undo both when needed:
sudo usermod -L -e 1 alice
sudo usermod -U -e "" alice
For a user who needs access only until a certain date, such as a contractor, set an expiry date and check it:
sudo chage -E 2026-12-31 alice
sudo chage -l alice
To delete a user, back up anything important in their home folder first, then:
sudo deluser --remove-home alice
On AlmaLinux, use sudo userdel -r alice (which also works on Debian and Ubuntu). If the user still has running processes, deletion fails; stop them with sudo pkill -u alice first. Don't forget the user's files in /etc/sudoers.d/ either.
Service users without a login shell
Applications such as bots, game servers and web apps should run as a separate user without a login shell, not as root or your own user:
sudo useradd --system --no-create-home --shell /usr/sbin/nologin myapp
This user can't log in, but can own files and run a service. Give it ownership of the application folder with sudo chown -R myapp:myapp /opt/myapp and set User=myapp in the systemd unit; to run a single command as this user, use sudo -u myapp command. If the application is ever compromised, the attacker can only reach this user's files. Other measures are listed in our Linux server security checklist.
User management at Nodesty
Nodesty AMD Ryzen 9 9950X VPS and AMD EPYC 7763 VPS plans come with NVMe disks and full root access, so you can create users and set up sudo rules as you like. If you lock yourself out while changing SSH settings, you can always reach your server through the web console in your panel, and free daily backups kept for 7 days give you a safety net.
Frequently Asked Questions
What is the difference between adduser and useradd?
On Debian and Ubuntu, adduser is a friendly script that creates the home folder, sets the shell to bash and asks for a password in one go. useradd is the low-level tool available on every distribution; on Debian and Ubuntu it does nothing extra unless you ask, so you add -m for the home folder and -s /bin/bash for the shell and then set the password with passwd. On AlmaLinux, adduser is simply another name for useradd.
Why do I still need a password if I log in with an SSH key?
The SSH key only replaces the password for logging in. When you run a command with sudo, the user's own password is asked to confirm it's really you. Give the user a strong password even if password login over SSH is turned off.
I added the user to the sudo group but sudo still doesn't work. Why?
Group changes only apply to new sessions. Log out and back in, or switch to the user again with su - username, then check with groups that sudo (or wheel on AlmaLinux) is listed. Also make sure you used usermod -aG; without -a, the user is removed from all other extra groups.
Does usermod -L completely block a user?
No. usermod -L and passwd -l only lock the password, so the user can still log in over SSH with a key. To block the account fully, also expire it with sudo usermod -e 1 username, or remove the user's authorized_keys file.
Is NOPASSWD in sudoers safe?
It's acceptable for an automation user that may run only one or two specific commands, such as restarting a service. Giving NOPASSWD for ALL commands means anyone who takes over that user immediately becomes root, so avoid it for everyday admin accounts.
I locked myself out after turning off root login. What now?
Don't panic. Connect through the web console (VNC) in the Nodesty customer panel, which works independently of SSH, log in there and fix the setting in the SSH configuration. This is why you should always test a second session before closing the first one.
About the author
Nodesty Team · Infrastructure & Support
The Nodesty team runs our servers, network and DDoS protection day to day and answers customer support requests around the clock. These guides come from that work.
Related articles

WordPress Security: A Hardening Checklist for Your Site
WordPress security checklist: updates, strong logins and 2FA, file permissions, wp-config.php, no PHP in uploads, XML-RPC, firewalls and tested backups.

How to Install Let's Encrypt SSL with Certbot (Nginx and Apache)
How to install Let's Encrypt SSL with Certbot on a Linux server: Nginx and Apache setup, automatic renewal, wildcard certificates and common errors.

How to Set Up SSH Keys and Disable Password Login
How to set up SSH keys: create an ed25519 key with ssh-keygen, copy it to your Linux server, test the login and safely turn off password login.