{"id":5147,"date":"2021-03-18T10:10:15","date_gmt":"2021-03-18T10:10:15","guid":{"rendered":"https:\/\/linuxways.net\/?p=5147"},"modified":"2021-03-18T10:10:15","modified_gmt":"2021-03-18T10:10:15","slug":"how-to-add-a-user-to-sudoers-on-ubuntu","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-add-a-user-to-sudoers-on-ubuntu\/","title":{"rendered":"How to Add a User to Sudoers on Ubuntu"},"content":{"rendered":"<p>The sudo command in Linux allows you to run all those commands which require root privileges. Without root privileges, a normal user is very limited in what they can do in Linux. But, practically you cannot give every user all privileges to do whatever they want to do with a system. However, there are some users whom you want to assign root privileges to perform all or a limited number of duties. The sudo command lets you do this by allowing a user to run all or a specific set of commands as a root user.<\/p>\n<p>To add a user to sudoers (list of users who can run sudo commands) on Ubuntu, there are three different methods including both the command line and the GUI. We will explain each method with example. If you are interested in how to add a new user in Linux, visit our post on How to <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-add-and-remove-users-on-ubuntu-20-04\/\">How to Add and Remove Users on Ubuntu 20.04<\/a>.<\/p>\n<p><strong>Note<\/strong>: For the demonstration, we will be using the Ubuntu 20.04 LTS. You can use the same procedures on other Linux distributions that use sudo.<\/p>\n<h2>Check If a User Has Sudo Rights<\/h2>\n<p>To find out if a user has sudo rights, you will need to check if it is a part of the group \u201csudo\u201d. You can check it using the command below in the Terminal:<\/p>\n<pre>$ groups &lt;username&gt;<\/pre>\n<p>If the user has sudo rights, you will see the &#8220;sudo&#8221; group in the output.<\/p>\n<p>For instance, to check if a user named \u201cummara\u201d has sudo access, the command would be:<\/p>\n<pre>$ groups ummara<\/pre>\n<p>The following output shows the user \u201cummara\u201d is a member of sudo groups and therefore has sudo privileges.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"681\" height=\"79\" class=\"wp-image-5148\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-236.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-236.png 681w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-236-300x35.png 300w\" sizes=\"auto, (max-width: 681px) 100vw, 681px\" \/><\/p>\n<h2>Add a User to Sudoers on Ubuntu<\/h2>\n<p>There are different methods to add a user to sudoers on Ubuntu. Remember, you will need to run the commands described here as a root or sudo.<\/p>\n<h3>Method #1 By Adding User to the sudo group (Command Line)<\/h3>\n<p>To add a user to sudoers and assign it sudo privileges, use the <em>usermod<\/em> command as follows:<\/p>\n<pre>$ sudo usermod -aG sudo username<\/pre>\n<p>This command will <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-add-user-to-group-in-linux\/\">add user to a group<\/a> named \u201csudo\u201d. The members of sudo group are allowed to run the sudo commands.<\/p>\n<p>For instance, to add a user named &#8220;ummara&#8221; to sudoers, the command would be:<\/p>\n<pre>$ sudo usermod -aG sudo ummara<\/pre>\n<p>Enter the sudo password, after which the user will be added to the sudo group. To confirm the user is now in the sudoers, issue the command below:<\/p>\n<pre>$ groups &lt;username&gt;<\/pre>\n<p>The groups command tells <a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-find-out-which-groups-a-user-belongs-to-in-ubuntu-20-04\/\">which groups a user belongs to<\/a>.<\/p>\n<p>In our scenario, the command would be:<\/p>\n<pre>$ groups ummara<\/pre>\n<p>In the output, you should see the sudo group.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"133\" class=\"wp-image-5149\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-237.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-237.png 573w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-237-300x70.png 300w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/p>\n<p>Now the user will be allowed to run the sudo commands, like we have tried the \u201c<em>sudo apt update<\/em>\u201d command:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"889\" height=\"130\" class=\"wp-image-5150\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-238.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-238.png 889w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-238-300x44.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-238-768x112.png 768w\" sizes=\"auto, (max-width: 889px) 100vw, 889px\" \/><\/p>\n<h3>Method 2: By Adding User to the Sudoers File (Command Line)<\/h3>\n<p>Another method to assign sudo access is by adding the user to the sudoers file. You can edit the sudoers file as follows:<\/p>\n<pre>$ sudo visudo<\/pre>\n<p>Enter your password. Now scroll down the bottom of the file and add an entry in the following format:<\/p>\n<pre>username ALL=(ALL) NOPASSWD:ALL<\/pre>\n<p>For instance, we want to add a user named &#8220;ummara&#8221; to sudoers. For this purpose, we have appended the below entry in the sudoers file:<\/p>\n<pre>ummara ALL=(ALL) NOPASSWD:ALL<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"795\" height=\"314\" class=\"wp-image-5151\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-239.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-239.png 795w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-239-300x118.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-239-768x303.png 768w\" sizes=\"auto, (max-width: 795px) 100vw, 795px\" \/><\/p>\n<p>Once done, save and close the sudoers file. Now the user will be allowed to run all commands as sudo.<\/p>\n<p>After editing the sudoers file, you can check if its syntax is correct. Issue the below command to do so:<\/p>\n<pre>$ sudo visudo -c<\/pre>\n<p>If the syntax is right, you will see this output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"82\" class=\"wp-image-5152\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-240.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-240.png 479w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-240-300x51.png 300w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/p>\n<p>To confirm the user is now in the sudoers, issue the command below:<\/p>\n<pre>$ groups &lt;username&gt;<\/pre>\n<p>You should see the sudo group in the output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"573\" height=\"56\" class=\"wp-image-5153\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-241.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-241.png 573w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-241-300x29.png 300w\" sizes=\"auto, (max-width: 573px) 100vw, 573px\" \/><\/p>\n<h1>Limiting Root Access with Sudo<\/h1>\n<p>In certain scenarios, there is a requirement for users to perform a certain task that needs root privileges. However, it is not reasonable to assign them all privileges. With sudo, you can limit the root access to commands they can use. For instance, you want a user named \u201cummara\u201d to give access only to the \u201cuseradd\u201d and \u201cadduser\u201d command. In this case, edit the sudoers file using the command below:<\/p>\n<pre>$ sudo visudo<\/pre>\n<p>Then scroll down the file and add the below entry:<\/p>\n<pre>ummara ALL=(ALL) NOPASSWD: \/usr\/sbin\/useradd,\/usr\/sbin\/adduser<\/pre>\n<p>Save the sudoers file and exit.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"230\" class=\"wp-image-5154\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-242.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-242.png 773w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-242-300x89.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-242-768x229.png 768w\" sizes=\"auto, (max-width: 773px) 100vw, 773px\" \/><\/p>\n<p>Now the user \u201cummara\u201d will be able to use only useradd and adduser commands.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"186\" class=\"wp-image-5155\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-243.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-243.png 615w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-243-300x91.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p>However, if \u201cummara\u201d attempts to run any other sudo command, it will be refused.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1027\" height=\"165\" class=\"wp-image-5156\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-244.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-244.png 1027w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-244-300x48.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-244-1024x165.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-244-768x123.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-244-1020x165.png 1020w\" sizes=\"auto, (max-width: 1027px) 100vw, 1027px\" \/><\/p>\n<h3>Method #3: By Changing Account Type to Administrator (GUI)<\/h3>\n<p>There is another way through which you can add a user to sudoers from the graphical user interface. Here are the steps to do so:<\/p>\n<p>1. Open the Settings utility by right-clicking the desktop and choosing <strong>Settings<\/strong> from the menu.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"278\" height=\"422\" class=\"wp-image-5157\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-245.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-245.png 278w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-245-198x300.png 198w\" sizes=\"auto, (max-width: 278px) 100vw, 278px\" \/><\/p>\n<p>Then from the Settings window, select the <strong>Users<\/strong> tab. Then unlock the user settings by clicking the <strong>Unlock<\/strong> button.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"818\" height=\"438\" class=\"wp-image-5158\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-246.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-246.png 818w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-246-300x161.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-246-768x411.png 768w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/p>\n<p>Now enter your password for authentication and click <strong>Authenticate<\/strong>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"385\" height=\"340\" class=\"wp-image-5159\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-247.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-247.png 385w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-247-300x265.png 300w\" sizes=\"auto, (max-width: 385px) 100vw, 385px\" \/><\/p>\n<p>Now select the user to whom you want to give sudo access. Then under the user <strong>Account Settings<\/strong>, switch the toggle button in front of <strong>Administrator<\/strong> to on state as shown in the screenshot below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"811\" height=\"570\" class=\"wp-image-5160\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-248.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-248.png 811w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-248-300x211.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-248-768x540.png 768w\" sizes=\"auto, (max-width: 811px) 100vw, 811px\" \/><\/p>\n<p>By doing so, the user will be added to the sudo group and it will be able to use all the sudo commands.<\/p>\n<h2>Remove User from Sudoers<\/h2>\n<p>To remove a user from sudoers, use the following command:<\/p>\n<pre>$ sudo gpasswd -d &lt;username&gt; sudo<\/pre>\n<p>This command will remove the specified user from the sudo group and hence it will no longer be able to run the commands as a root user.<\/p>\n<p>To confirm if the user has been removed from the sudoers, run the command below:<\/p>\n<pre>$ groups &lt;username&gt;<\/pre>\n<p>There should be no \u201csudo\u201d group in the output.<\/p>\n<p>Sudo is a very handy tool for system administrators that allow them to provide root access to a user with granularity. Here, we have covered how to add a user to sudoers using different methods, limit root access with sudo and remove a user from sudoers.<\/p>","protected":false},"excerpt":{"rendered":"<p>The sudo command in Linux allows you to run all those commands which require root privileges. Without root privileges, a normal user is very limited in what they&hellip;<\/p>","protected":false},"author":1,"featured_media":5258,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[227,100,155],"class_list":["post-5147","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-sudoers","tag-ubuntu-20-04","tag-user"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5147","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=5147"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/5147\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/5258"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=5147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=5147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=5147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}