Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Manage User Sessions in Linux with sudo

Run commands as root, switch to another user and stop other users' processes, in a safe way.

VPS and Cloud Servers2 min read3 steps1 screenshot

How sudo works

sudo lets a permitted user run a command with the rights of another user, usually root. When you run it:

  1. The system checks the sudoers file to see if you are allowed.
  2. It asks for your password.
  3. It runs the command with the higher rights.

Install and set up sudo

On Debian and Ubuntu:

apt install sudo

On CentOS and Fedora:

yum install sudo

Check that it works:

sudo -h
The sudo help page

To give a user the right to use sudo, edit the sudoers file in a safe way with visudo. It checks the syntax before it saves:

sudo visudo

Add this line, with the real user name:

username ALL=(ALL:ALL) ALL

You can also add the user to the sudo group (Ubuntu and Debian) or the wheel group (CentOS):

sudo usermod -aG sudo username

Switch to another user

sudo su username

To get a full login shell as root:

sudo su - root

Enter your password when asked. You are now that user. Type exit to go back.

Log out another user

End all processes of a user:

sudo pkill -u username

Stop all processes with one name:

sudo killall process_name

Change the password of a user:

sudo passwd username

Best practices

  • Use sudo only when you need it.
  • Follow the principle of least privilege. Give users only the rights they need.
  • Avoid sudo in scripts and cron jobs when you can, because a bug runs with high rights.
  • Protect the sudoers file. Always edit it with visudo.
  • Check /var/log/auth.log (Ubuntu) or /var/log/secure (CentOS) to see who ran sudo.

Quick recap

  • sudo runs a command with higher rights after a password check.
  • sudo su - username switches user. pkill -u ends a user's processes.
  • Edit the rules only with visudo.