{"id":77,"date":"2021-09-18T15:02:00","date_gmt":"2021-09-18T15:02:00","guid":{"rendered":"https:\/\/bestdedicatedhosting.in\/blog\/?p=77"},"modified":"2021-11-02T13:11:39","modified_gmt":"2021-11-02T13:11:39","slug":"how-to-disable-root-account-in-linux-using-4-ways","status":"publish","type":"post","link":"https:\/\/bestdedicatedhosting.in\/blog\/how-to-disable-root-account-in-linux-using-4-ways\/","title":{"rendered":"How to disable root account in Linux using 4 ways"},"content":{"rendered":"\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Description <\/strong><\/p>\n\n\n\n<p> The&nbsp;<strong>root<\/strong>&nbsp;account is the ultimate account on a Linux and other Unix-like operating systems. <\/p>\n\n\n\n<p>This account has access to all commands and files on a system with full read, write and execute permissions.<\/p>\n\n\n\n<p>It is used to perform any kind of task on a system to create\/update\/access\/delete other users\u2019 accounts, install\/remove\/upgrade software packages, and so much more. <\/p>\n\n\n\n<p>The <strong>root<\/strong>&nbsp;user has absolute powers, any actions he\/she performs are critical on a system. <\/p>\n\n\n\n<p>In this regard, any errors by the&nbsp;<strong>root<\/strong>&nbsp;user may have huge implications on the normal operation of a system. <\/p>\n\n\n\n<p>In addition, this account may also be abused by using it improperly or inappropriately either accidentally, maliciously, or through contrived ignorance of policies. <\/p>\n\n\n\n<p>It is advisable to disable the root access in your Linux server, instead, create an administrative account which should be configured to gain root user privileges using the sudo command, to perform critical tasks on the server. <\/p>\n\n\n\n<p>We will explain four ways to disable root user account login in Linux. <\/p>\n\n\n\n<p><strong>Note<\/strong><\/p>\n\n\n\n<p>Before you block access to the <strong>root<\/strong>&nbsp;account, make sure you have created an administrative account, capable of using&nbsp;sudo command&nbsp;to gain root user privileges, with the&nbsp;useradd command&nbsp;and give this user account a strong password. <\/p>\n\n\n\n<p>The flag&nbsp;<code>-m<\/code>&nbsp;means create user\u2019s home directory and&nbsp;<code>-c<\/code>&nbsp;allows to specify a comment: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>useradd -m -c \"Admin User\" admin<\/em><\/strong><br><br><strong><em>passwd admin <\/em><\/strong><br><br><\/pre>\n\n\n\n<p>Next, add this user to the appropriate group of system administrators using the usermod command, where the switch <code>-a<\/code>&nbsp;means append user account and&nbsp;<code>-G<\/code>&nbsp;specifies a group to add the user in (wheel or sudo depending on your Linux distribution): <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>usermod -aG wheel admin    #CentOS\/RHEL<\/em><\/strong><br><br><strong><em>usermod -aG sudo admin     #Debian\/Ubuntu <\/em><\/strong><br> <br>Once you have created a user with administrative privileges, switch to that account in order to block root access. <br><br><strong><em>su admin <\/em><\/strong><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>1.Change root User\u2019s Shell<\/strong><\/p>\n\n\n\n<p>The simplest method to disable root user login is to change its shell from <code>\/bin\/bash<\/code>&nbsp;or&nbsp;<code>\/bin\/bash<\/code>&nbsp;(or any other shell that permits user login) to&nbsp;<code>\/sbin\/nologin<\/code>, in the&nbsp;<strong>\/etc\/passwd<\/strong>&nbsp;file, which you can open for editing using any of your favorite command line editors as shown. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>sudo vim \/etc\/passwd<\/strong><\/pre>\n\n\n\n<p>Change the line: <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>root:x:0:0:root:\/root:\/bin\/bash<br>to<\/strong><br><strong>root:x:0:0:root:\/root:\/sbin\/nologin <\/strong><br><br>Save the file and close it. <\/pre>\n\n\n\n<p>From now on, when&nbsp;<strong>root<\/strong>&nbsp;user logs in, he\/she will get the message \u201c<strong>This account is currently not available.<\/strong>\u201d <\/p>\n\n\n\n<p>This is the default message, but, you can change it and set a custom message in the the file&nbsp;<strong>\/etc\/nologin.txt<\/strong>.<\/p>\n\n\n\n<p>This method is only effective with programs that require a shell for user login, otherwise,&nbsp;<strong>sudo<\/strong>,&nbsp;<strong>ftp<\/strong>&nbsp;and&nbsp;<strong>email<\/strong>clients can access the root account.<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>2.Disable root Login via Console Device (TTY)<\/strong><\/p>\n\n\n\n<p>The second method uses a&nbsp;<strong>PAM<\/strong>&nbsp;module called&nbsp;<strong>pam_securetty<\/strong>, which permits root access only if the user is logging in on a \u201c<strong>secure\u201d TTY<\/strong>, as defined by the listing in&nbsp;<strong>\/etc\/securetty<\/strong>.<\/p>\n\n\n\n<p>The above file allows you to specify which&nbsp;<strong>TTY<\/strong>&nbsp;devices the root user is allowed to login on, emptying this file prevents root login on any devices attached to the computer system.<\/p>\n\n\n\n<p>To create an empty file, run.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>sudo mv \/etc\/securetty \/etc\/securetty.orig<br>sudo touch \/etc\/securetty <br>sudo chmod 600 \/etc\/securetty <\/strong><br><br><\/pre>\n\n\n\n<p>This method has some limitations, it only affects programs such as login, display managers (i.e <strong>gdm<\/strong>,&nbsp;<strong>kdm<\/strong>&nbsp;and&nbsp;<strong>xdm<\/strong>) and other network services that launch a TTY. <\/p>\n\n\n\n<p>Programs such as su, sudo, ssh, and other related openssh tools will have access to the root account. <\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3.Disable SSH Root Login<\/strong><\/p>\n\n\n\n<p>The commonest way of accessing remote servers or VPSs is via SSH and to block root user login under it, you need to edit the <strong>\/etc\/ssh\/sshd_config<\/strong>&nbsp;file <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>sudo vim \/etc\/ssh\/sshd_config<\/em><\/strong><\/pre>\n\n\n\n<p>Then uncomment (if it is commented) the directive <strong>PermitRootLogin<\/strong>&nbsp;and set its value to&nbsp;<code>no<\/code>&nbsp;as shown in the screenshot. <\/p>\n\n\n\n<p>Once you are done, save and close the file. Then restart the <strong>sshd<\/strong>&nbsp;service to apply the recent change in configurations. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>$ sudo systemctl restart sshd<\/em><\/strong><br><strong><em>OR<\/em><\/strong><br><strong><em>$ sudo service sshd restart <\/em><\/strong><br><br><br><\/pre>\n\n\n\n<p>As you may already know, this method only affects openssh tools set, programs such as ssh, scp, sftp will be blocked from accessing the root account.<\/p>\n\n\n\n<p><strong>3.Restrict root Access to Services Via PAM<\/strong><\/p>\n\n\n\n<p> <strong>Pluggable Authentication Modules<\/strong>&nbsp;(<strong>PAM<\/strong>&nbsp;in short) is a centralized, pluggable, modular, and flexible method of authentication on Linux systems. PAM, through the&nbsp;<strong>\/lib\/security\/pam_listfile.so<\/strong>&nbsp;module, allows great flexibility in limiting the privileges of specific accounts. <\/p>\n\n\n\n<p> The above module can be used to reference a list of users who are not allowed to log in via some target services such as login, ssh and any PAM aware programs. <\/p>\n\n\n\n<p>In this case, we want to disable root user access to a system, by restricting access to login and sshd services. First open and edit the file for the target service in the&nbsp;<strong>\/etc\/pam.d\/<\/strong>&nbsp;directory as shown.<br><br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>$ sudo vim \/etc\/pam.d\/login<\/em><\/strong><br><strong><em>OR<\/em><\/strong><br><strong><em>sudo vim \/etc\/pam.d\/sshd<\/em><\/strong> <br><br><br><\/pre>\n\n\n\n<p>Next, add the configuration below in both files.<\/p>\n\n\n\n<p class=\"has-small-font-size\"><strong><em>auth    required       pam_listfile.so \\<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>onerr=succeed  item=user  sense=deny  file=\/etc\/ssh\/deniedusers<\/em><\/strong><br><\/pre>\n\n\n\n<p>When you are done, save and close each file. <\/p>\n\n\n\n<p>Then create the plain file&nbsp;<strong>\/etc\/ssh\/deniedusers<\/strong>&nbsp;which should contain one item per line and not world readable. <\/p>\n\n\n\n<p> Add the name root in it, then save and close it. <\/p>\n\n\n\n<p class=\"has-small-font-size\"><strong><em>$ sudo vim \/etc\/ssh\/deniedusers<\/em><\/strong><\/p>\n\n\n\n<p>Also set the required permissions on this.<\/p>\n\n\n\n<p class=\"has-small-font-size\"><strong><em>$ sudo chmod 600 \/etc\/ssh\/deniedusers<\/em><\/strong><\/p>\n\n\n\n<p> This method only affect programs and services that are PAM aware. You can block root access to the system via ftp and email clients and more. <\/p>\n\n\n\n<p> For more information, consult the relevant man pages. <br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong><em>$ man pam_securetty<\/em><\/strong><br><strong><em>$ man sshd_config <\/em><\/strong><br><strong><em>$ man pam <\/em><\/strong><br><br><\/pre>\n\n\n\n<p>We hope you\u2019ve found this useful!&nbsp; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description The&nbsp;root&nbsp;account is the ultimate account on a Linux and other Unix-like operating systems. This account has access to all commands and files on a system with full read, write and execute permissions. It is used to perform any kind of task on a system to create\/update\/access\/delete other users\u2019 accounts, install\/remove\/upgrade software packages, and so [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-linux"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":1,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/posts\/77\/revisions\/78"}],"wp:attachment":[{"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bestdedicatedhosting.in\/blog\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}