{"id":4163,"date":"2019-09-14T19:56:21","date_gmt":"2019-09-14T16:56:21","guid":{"rendered":"https:\/\/kifarunix.com\/?p=4163"},"modified":"2024-03-12T21:56:11","modified_gmt":"2024-03-12T18:56:11","slug":"how-to-add-users-to-sudo-group-in-linux","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-add-users-to-sudo-group-in-linux\/","title":{"rendered":"How to Add Users to sudo group in Linux"},"content":{"rendered":"\n<p>In this guide, we are going to learn how to add users to sudo group in Linux. More often than not, you want, as a non-root user, to run commands with elevated privileges in Linux. So the only way this can happen is to give user sudo privileges by adding them to a sudo group or to sudoers file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Users to sudo group in Linux<\/h2>\n\n\n\n<p><code>sudo<\/code> group in Debian and its derivatives is called <code>wheel<\/code> group in CentOS and similar derivatives.<\/p>\n\n\n\n<p>A user can be given sudo privileges by being added to the <code>sudo<\/code>\/<code>wheel<\/code> group or by being added to the <code>sudoers<\/code> file, <code>\/etc\/sudoers<\/code>.<\/p>\n\n\n\n<p>So what is the difference between sudo\/wheel group and sudoers file?<\/p>\n\n\n\n<p>The sudo\/wheel group has the privileges it has based on what is defined on the sudoers file. In sudoers file, you will see the lines below;<\/p>\n\n\n\n<p>In Debian derivatives;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># Allow members of group sudo to execute any command\n%sudo   ALL=(ALL:ALL) ALL<\/code><\/pre>\n\n\n\n<p>In RHEL derivatives;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>## Allows people in group wheel to run all commands\n%wheel  ALL=(ALL)       ALL<\/code><\/pre>\n\n\n\n<p>NOTE that a group is prefixed with&nbsp;<code>%<\/code>.<\/p>\n\n\n\n<p>So based on the two lines above, sudoers security policy requires that users authenticate themselves before they can use sudo command.<\/p>\n\n\n\n<p>However, a password is not required if the invoking user is root, if the target user is the same as the invoking user, or if the policy has disabled authentication for the user or command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add Users to sudo group in Linux<\/h3>\n\n\n\n<p>To add user to&nbsp;<code>wheel<\/code>&nbsp;or&nbsp;<code>sudo<\/code>&nbsp;group, you can use the&nbsp;<code>usermod<\/code>&nbsp;command in the following syntax;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>usermod -aG sudo\/wheel USERNAME<\/code><\/pre>\n\n\n\n<p>Where<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a<\/code>&nbsp;means add the user to the supplementary group that will be specified with&nbsp;<code>-G<\/code>&nbsp;option.<\/li>\n\n\n\n<li><code>G<\/code>&nbsp;specifies the supplementary groups to which the user is being added.<\/li>\n\n\n\n<li><code>sudo\/wheel<\/code>&nbsp;specifies the group to add the user<\/li>\n\n\n\n<li><code>USERNAM<\/code>&nbsp;specifies the name of the user being added to the sudo group.<\/li>\n<\/ul>\n\n\n\n<p>For example, On Debian and its derivatives, to add a user called john to sudo group;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>usermod -aG sudo john<\/code><\/pre>\n\n\n\n<p>To confirm the groups of the user, use&nbsp;<code>id<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>id john\nuid=1002(john) gid=1002(john) groups=1002(john),27(sudo)<\/code><\/pre>\n\n\n\n<p>On RHEL and its derivatives like CentOS;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>usermod -aG wheel john<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>id john\nuid=1001(john) gid=1001(john) groups=1001(john), 10(wheel)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Adding users to sudoers file in Linux<\/h3>\n\n\n\n<p>Well, you can explicitly give users sudo privileges by adding them to the sudoers file. A user whose privileges are defined in the sudoers file doesn&#8217;t necessarily have to be added to the sudo or wheel group.<\/p>\n\n\n\n<p>To edit the sudoers file, use the&nbsp;<code>visudo<\/code>&nbsp;command. This will open the sudoers file with your default editor, usually nano if the EDITOR variable has not been set.<\/p>\n\n\n\n<p>To use vim as your editor, simply run;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>export EDITOR=vim<\/code><\/pre>\n\n\n\n<p>Next, run visudo command. Note that you cannot edit the sudoers file as an ordinary user with no sudo privileges.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>visudo<\/code><\/pre>\n\n\n\n<p>Once you open the sudoers file, you can give a user sudoers rights as follows.<\/p>\n\n\n\n<p>For example, to enable the user&nbsp;<code>john<\/code>&nbsp;to run commands with&nbsp;<code>sudo<\/code>&nbsp;privileges, simply add the line below on the sudoers file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>john ALL=(ALL:ALL) ALL <\/code><\/pre>\n\n\n\n<p>This line allows user john to run all commands with sudo upon authentication.<\/p>\n\n\n\n<p>If you need to allow specific group of users to run the commands with sudo, simply add the line below replacing the&nbsp;<code>groupname<\/code>&nbsp;with your group.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>%groupname ALL=(ALL:ALL) ALL<\/code><\/pre>\n\n\n\n<p>To break down these lines in simple terms;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>john<\/code>&nbsp;or&nbsp;<code>groupname<\/code>: specifies the user or group being assigned the sudo privileges.<\/li>\n\n\n\n<li><code>ALL<\/code>&nbsp;(before =): Specify the host on which the user\/group can have sudo privileges. This means that the user\/group can use sudo on all hosts.<\/li>\n\n\n\n<li><code>ALL:ALL<\/code>&nbsp;(within the brackets): The <code>ALL<\/code> before the colon specifies the user running the command while the <code>ALL<\/code> after the colon specifies the group of the user running the command.<\/li>\n\n\n\n<li><code>ALL<\/code>&nbsp;(the last section): Specifies the command that the user can run. In this case, it means any command.<\/li>\n<\/ul>\n\n\n\n<p>Once the user is given sudo rights, they can now execute privileged commands that are allowed to execute by prefixing them with sudo.<\/p>\n\n\n\n<p>Other Tutorials<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/using-find-command-to-search-for-files-and-directories-in-linux\/\" target=\"_blank\">Using Find Command to Search for Files and Directories in Linux<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/connect-to-wifi-in-linux-using-nmcli-command\/\" target=\"_blank\">Connect to WiFi in Linux Using NMCLI command<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/extract-log-lines-of-specific-dates-from-a-log-file\/\" target=\"_blank\">Extract Log Lines of Specific Dates from a Log File<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-install-and-use-7zip-on-ubuntu-18-04-command-line\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">How to Install and Use 7zip File Archiver on Ubuntu 18.04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we are going to learn how to add users to sudo group in Linux. More often than not, you want, as a<\/p>\n","protected":false},"author":1,"featured_media":14089,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,49,1123],"tags":[1129,1107,1124,1126,1125,1128],"class_list":["post-4163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-command-cheatsheets","category-sudo","tag-etc-sudoers","tag-linux","tag-sudo","tag-sudo-group","tag-sudoers","tag-sudoers-file","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4163"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=4163"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4163\/revisions"}],"predecessor-version":[{"id":21202,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/4163\/revisions\/21202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/14089"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=4163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=4163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=4163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}