How to Install and Manage Sudo

Sudo is a linux command which can be used to grant root-level access to authorized users without requiring these users to login as the root user. This is useful for securing a server by disabling root access and instead granting sudo access to select users. 

In this article, we'll outline how to install sudo and add users to the sudo group.

How to Install Sudo

If sudo is not yet installed on your server, follow these commands in order to install sudo on Debian, CentOS, or FreeBSD:

Debian

apt-get install sudo -y


CentOS

yum install sudo -y


How to Add Users to the Sudo Group

Debian / CentOS / FreeBSD

Begin by adding the user account to the server if it does not already exist:

adduser mynewusername

Then you can add the user to the sudo or wheel group:

Debian

usermod -aG sudo mynewusername


CentOS

usermod -aG sudo mynewusername

Testing Sudo Access

You have now installed sudo and added the user to the sudo group. You can test sudo access by switching into the new user using the su command executing:

sudo echo "hello world"

You will be prompted to enter the password of the user, and the screen will display hello world when executed successfully:

user@debian:/root$ sudo echo "hello world"

hello world

If the user has not been added to sudo properly, it will display:

user@debian:/root$ sudo echo "hello world"

user is not in the sudoers file. This incident will be reported.

Next Steps

Check out our other Knowledge Base articles including the Top 5 Ways to Secure Your Linux Server.

About The Author

generic avatar icon