Browse help topics

Security

How to Create a Linux User With sudo Access

As root on CloudLinux or another Red Hat family system, create the user with useradd and passwd, then add it to the wheel group, which may use sudo. If your server runs Ubuntu or Debian instead, use adduser and the sudo group. Test it with sudo whoami before you close your root session.

  • Updated
  • Last reviewed
  • Intermediate
  • About 10 minutes

Applies to: Linux OpenSSH Blue Arctic VPS Hosting and Virtual Dedicated Servers (VDS); CloudLinux and other Red Hat family systems

On this page
  1. Before you start
  2. Step 1: Check which Linux you have
  3. Step 2a: CloudLinux and other Red Hat family systems
  4. Step 2b: If your server runs Ubuntu or Debian instead
  5. Step 3: Test before you close the root session
  6. How to tell it worked
  7. How to undo it
  8. Troubleshooting
  9. Still need help?

Working as root (the main administrator account, which can change anything) all the time is risky: one mistyped command can damage the whole server. A separate user with sudo rights works normally and runs an administrator command only when you put sudo in front of it.

Before you start

  • This creates a user for managing the server itself. To add a website or hosting account on a server with cPanel/WHM or Plesk, use the control panel instead.
  • Warning: Keep your current root session open until the new user works. Don’t edit /etc/sudoers (sudo’s settings file) yourself; a mistake there can stop sudo working.
  • Every VPS and VDS plan comes with Core Managed: we look after the server underneath your applications, such as OS security patches, Imunify360 and the control panel. Inside the server, root is yours. Before you change SSH settings, such as which users may log in, open a support ticket. Not sure whether the work is covered by your plan? Open the ticket and we’ll tell you.
  • If you’re logged in as a user with sudo rather than root, put sudo and a space in front of each command below.

Step 1: Check which Linux you have

  1. Run cat /etc/os-release. This only shows information about your system.
  2. Look at the ID and ID_LIKE lines (some systems, such as Debian, have no ID_LIKE line). If they mention cloudlinux, rhel, centos, fedora, almalinux or rocky, use Step 2a. If they mention ubuntu or debian, use Step 2b. If you see something else, open a support ticket before you continue.

The examples use the username alex. Choose your own: start with a lowercase letter, then use lowercase letters or numbers, with no spaces.

Step 2a: CloudLinux and other Red Hat family systems

  1. Run useradd -m alex. This creates the user; -m also creates a home folder. It prints nothing when it works.
  2. Run passwd alex and type a strong password twice. Nothing appears on screen while you type. A line starting BAD PASSWORD means the password is too weak: press Ctrl+C and run the command again with a stronger one.
  3. Run usermod --append -G wheel alex. This adds alex to the wheel group, which may use sudo, and keeps alex’s other groups.
  4. Run grep '^%wheel' /etc/sudoers. This only reads the file. You should see a line starting %wheel and ending ALL (the spacing may differ). If nothing is printed, don’t edit the file: open a support ticket.

Step 2b: If your server runs Ubuntu or Debian instead

  1. Run adduser alex. Type a strong password twice; nothing appears while you type. Press Enter to skip the name and phone questions, then type Y and press Enter when asked if the information is correct.
  2. Run adduser alex sudo. This adds alex to the sudo group, which may use sudo.

Step 3: Test before you close the root session

  1. In your open session, run su - alex (if you aren’t root, sudo su - alex). This switches you to alex; the prompt now shows alex.
  2. Run sudo whoami. The first time, you may see a short message about using sudo carefully; that’s normal.
  3. When you see [sudo] password for alex:, type alex’s password (not root’s) and press Enter. It should print root.
  4. Type exit and press Enter to go back to your own session. Leave it open.
  5. Now test the login. In a new PowerShell or Terminal window, connect as the new user, for example ssh -p PORT alex@203.0.113.10 (leave out -p PORT if your port is 22). If it fails, see Troubleshooting.

How to tell it worked

sudo whoami prints root, and alex can log in from a second window. You can now do everyday work as this user and use sudo for administrator commands.

Tip

Give the new user an SSH key too; see how to set up SSH key authentication.

How to undo it

  • Remove sudo rights only: gpasswd -d alex wheel (Red Hat family) or deluser alex sudo (Ubuntu, Debian).
  • Warning: Never remove the only user you can log in with. Use one of the next two commands, not both: the second deletes everything in /home/alex for good.
  • Remove the user and keep the home folder: log out of every session where you’re logged in as alex, then run userdel alex (Red Hat family) or deluser alex (Ubuntu, Debian). /home/alex stays on the server, with any SSH keys in it. A user created later with the same name can get that folder and those keys.
  • Remove the user and the home folder together: log out as alex, then run userdel -r alex instead.

Troubleshooting

  • “alex is not in the sudoers file”: the group step didn’t take effect. In the root session, run groups alex and check that wheel or sudo is listed. Then log out as alex and log in again.
  • sudo: command not found: sudo isn’t installed. In your root session, run dnf install sudo (Red Hat family) or apt install sudo (Ubuntu, Debian), then test again.
  • The new user can’t log in over SSH: check the password. Some servers accept only SSH keys, or allow only certain users to log in. Don’t change that yourself. The user and its sudo rights still work, as you saw in Step 3. To let alex log in over SSH, open a support ticket and we’ll tell you what we can do.
  • sudo shows a different error (for example, it mentions CageFS or LVE, which are parts of CloudLinux): don’t change any settings. Open a support ticket with the exact message.

Still need help?

Open a support ticket from the Client Portal at my.bluearctic.com. Our support page explains how the 24/7 helpdesk works. Include your VPS IP address, the Linux name from /etc/os-release, the commands you ran and the exact error text. Never include a password or a private key.

Was this article helpful?

Report outdated instructions or suggest an improvement
Type of report

Please do not include passwords or personal information.

Still need help?

Our support team can look into your account and walk you through it.