How to use sudo to allow a non-root user run a particular command

With the help of sudo, you can give access to a non-root user to run a root only command. Here is a short howto to provide the non-root user the access to a root only command. The command access that we will give to a user named “john” will be “/sbin/poweroff”.

Giving sudo access to a command

1. open the /etc/sudoers file. It is recommende to open this file using the visudo command.

# visudo

2. Add the following lines at the end. (though can be added anywhere in the file)

User_Alias ADMINS = john                      --> define which user can run the root only commands
Cmnd_Alias POWEROFF = /sbin/poweroff          --> define which command
ADMINS     ALL=POWEROFF                       --> bind the command with the user

3. Now login with the user john and execute the command :

$ sudo /sbin/poweroff

For the first time, it should ask the john’s password. This should power off the system without asking for the root user credentials.

Related Post