Need to give a user sudo privileges in Linux? Here’s a quick and easy guide on how to do it.
Linux can be an intimidating operating system to work with, but it’s worth learning how to use it if you’re going to be working on servers. With that said, Linux comes with some commands that can help you do your job more quickly and easily. One of these commands is sudo. This allows you to give other users temporary privileges to change settings and files that you normally wouldn’t have access to change yourself or that would take too long for you to change with your normal credentials.
Open up terminal
If you’re already logged in as the superuser, you can skip this step. Otherwise, open up terminal and type in su followed by your password. Once you’re logged in as the superuser, type in visudo. This will open up the configuration file for sudoers in the vi editor. Scroll down until you find the section that says # User privilege specification. Below the line %wheel ALL=(ALL) ALL add the following line: %johnny ALL=(ALL) NOPASSWD: ALL . Make sure to hit escape after you’ve typed johnny so that vi doesn’t automatically insert another paragraph at the end of your text. Hit ESCAPE again after typing johnny. Save and exit out of vi by typing :wq , then hitting enter or esc key. Finally, close out of terminal (type exit )
Edit sudoers file
In order to give a user sudo privileges in Linux, you’ll need to edit the sudoers file. This file is usually located at /etc/sudoers, but it may be located elsewhere depending on your distribution. The command visudo should suffice for editing this file as it opens the sudoers file with root permissions and checks that everything written to it conforms to syntax rules before saving. You can also run visudo -c which will check only for syntax errors without writing anything to disk.
Add new line
In order to add a user to the sudo group, you’ll first need to open up a terminal and log into your server as the root user. Once you’re logged in, run the following command: `usermod -aG sudo username`. This will add the specified user to the sudo group. From now on, that user will be able to use commands like sudo su or sudo apt-get install. You can also edit the file /etc/sudoers and specify that a certain user is allowed to use sudo by editing their line.
If you want all users of your machine to have sudo access, then just edit the file /etc/sudoers with these lines:
root ALL=(ALL) ALL
this_is_very_dangerous ALL=(ALL) NOPASSWD: ALL
Close the file
When you’re done, remember to close the file by typing exit at the command prompt. If you need to exit vim, type :q! (without the quotes). That’s it! You’ve now given a user sudo privileges in Linux. The steps are similar for different versions of Linux, but the commands might be slightly different. For example, if your distribution is Ubuntu 16.04 LTS, you would enter sudo visudo instead of visudo. Keep reading for more information about this process. To take away a user’s privilege to use sudo:
- Open terminal and change directory with cd /etc/sudoers.
- Type vi sudoers.
- Find the line that says username ALL=(ALL:ALL) ALL without quotes.
- Add !username without quotes after ALL=(ALL:ALL) so that it says username ALL=(ALL:ALL) ALL ! username.
Restart the machine
If you’re having trouble with your computer, one of the first things you can try is restarting it. This simple act can often fix minor issues and get your machine back up and running smoothly. The command for restarting a Linux computer depends on the distribution. On Ubuntu or Debian systems, for example, type sudo shutdown -r now into the terminal.
If you need to give a user sudo privileges in Linux, there’s a quick and easy way to do it. Just follow these steps – Log into the computer as the root user or with the su command if your system has this installed
- Find the name of the user account that needs sudo privileges
- Type in gpasswd -a with either admin or wheel)
- Press enter and type exit