{"id":12410,"date":"2019-03-26T23:10:56","date_gmt":"2019-03-26T23:10:56","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12410"},"modified":"2019-03-26T23:10:56","modified_gmt":"2019-03-26T23:10:56","slug":"4-ways-to-disable-root-account-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/4-ways-to-disable-root-account-in-linux\/","title":{"rendered":"4 Ways to Disable Root Account in Linux"},"content":{"rendered":"<p>The\u00a0<strong>root<\/strong>\u00a0account 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\u00a0<a href=\"https:\/\/www.tecmint.com\/add-users-in-linux\/\" target=\"_blank\" rel=\"noopener\">create\/update\/access\/delete other users\u2019 accounts<\/a>,\u00a0<a href=\"https:\/\/www.tecmint.com\/20-linux-yum-yellowdog-updater-modified-commands-for-package-mangement\/\" target=\"_blank\" rel=\"noopener\">install\/remove\/upgrade software packages<\/a>, and so much more.<\/p>\n<p>Because the\u00a0<strong>root<\/strong>\u00a0user has absolute powers, any actions he\/she performs are critical on a system. In this regard, any errors by the\u00a0<strong>root<\/strong>\u00a0user may have huge implications on the normal operation of a system. 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<p>Therefore, 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\u00a0<a href=\"https:\/\/www.tecmint.com\/sudoers-configurations-for-setting-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command<\/a>, to perform critical tasks on the server.<\/p>\n<p>In this article, we will explain four ways to disable root user account login in Linux.<\/p>\n<p><strong>Attention<\/strong>: Before you block access to the\u00a0<strong>root<\/strong>\u00a0account, make sure you have created an administrative account, capable of using\u00a0<a href=\"https:\/\/www.tecmint.com\/run-sudo-command-without-password-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command<\/a>\u00a0to gain root user privileges, with the\u00a0<a href=\"https:\/\/www.tecmint.com\/add-users-in-linux\/\" target=\"_blank\" rel=\"noopener\">useradd command<\/a>\u00a0and give this user account a strong password. The flag\u00a0<code>-m<\/code>\u00a0means create user\u2019s home directory and\u00a0<code>-c<\/code>\u00a0allows to specify a comment:<\/p>\n<pre># useradd -m -c \"Admin User\" admin\r\n# passwd admin\r\n<\/pre>\n<p>Next, add this user to the appropriate group of system administrators using the\u00a0<a href=\"https:\/\/www.tecmint.com\/usermod-command-examples\/\" target=\"_blank\" rel=\"noopener\">usermod command<\/a>, where the switch\u00a0<code>-a<\/code>\u00a0means append user account and\u00a0<code>-G<\/code>\u00a0specifies a group to add the user in (wheel or sudo depending on your Linux distribution):<\/p>\n<pre># usermod -aG wheel admin    #CentOS\/RHEL\r\n# usermod -aG sudo admin     #Debian\/Ubuntu \r\n<\/pre>\n<p>Once you have created a user with administrative privileges, switch to that account in order to block root access.<\/p>\n<pre># su admin\r\n<\/pre>\n<h3>1. Change root User\u2019s Shell<\/h3>\n<p>The simplest method to disable root user login is to change its shell from\u00a0<code>\/bin\/bash<\/code>\u00a0or\u00a0<code>\/bin\/bash<\/code>\u00a0(or any other shell that permits user login) to\u00a0<code>\/sbin\/nologin<\/code>, in the\u00a0<strong>\/etc\/passwd<\/strong>\u00a0file, which you can open for editing using any of your favorite command line editors as shown.<\/p>\n<pre>  \r\n$ sudo vim \/etc\/passwd\r\n<\/pre>\n<p>Change the line:<\/p>\n<pre>root:x:0:0:root:\/root:\/bin\/bash\r\nto\r\nroot:x:0:0:root:\/root:<strong>\/sbin\/nologin<\/strong>\r\n<\/pre>\n<div id=\"attachment_30833\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/10\/change-root-shell.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-30833\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/10\/change-root-shell.png\" alt=\"Change root User Shell\" width=\"512\" height=\"135\" aria-describedby=\"caption-attachment-30833\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-30833\" class=\"wp-caption-text\">Change root User Shell<\/p>\n<\/div>\n<p>Save the file and close it.<\/p>\n<p>From now on, when\u00a0<strong>root<\/strong>\u00a0user logs in, he\/she will get the message \u201c<strong>This account is currently not available.<\/strong>\u201d This is the default message, but, you can change it and set a custom message in the the file\u00a0<strong>\/etc\/nologin.txt<\/strong>.<\/p>\n<p>This method is only effective with programs that require a shell for user login, otherwise,\u00a0<strong>sudo<\/strong>,\u00a0<strong>ftp<\/strong>\u00a0and\u00a0<strong>email<\/strong>clients can access the root account.<\/p>\n<h3>2. Disable root Login via Console Device (TTY)<\/h3>\n<p>The second method uses a\u00a0<strong>PAM<\/strong>\u00a0module called\u00a0<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\u00a0<strong>\/etc\/securetty<\/strong>.<\/p>\n<p>The above file allows you to specify which\u00a0<strong>TTY<\/strong>\u00a0devices the root user is allowed to login on, emptying this file prevents root login on any devices attached to the computer system.<\/p>\n<p>To create an empty file, run.<\/p>\n<pre>$ sudo mv \/etc\/securetty \/etc\/securetty.orig\r\n$ sudo touch \/etc\/securetty\r\n$ sudo chmod 600 \/etc\/securetty\r\n<\/pre>\n<p>This method has some limitations, it only affects programs such as login, display managers (i.e\u00a0<strong>gdm<\/strong>,\u00a0<strong>kdm<\/strong>\u00a0and\u00a0<strong>xdm<\/strong>) and other network services that launch a TTY. Programs such as su, sudo, ssh, and other related openssh tools will have access to the root account.<\/p>\n<h3>3. Disabl SSH Root Login<\/h3>\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\u00a0<strong>\/etc\/ssh\/sshd_config<\/strong>\u00a0file.<\/p>\n<pre>$ sudo vim \/etc\/ssh\/sshd_config\r\n<\/pre>\n<p>Then uncomment (if it is commented) the directive\u00a0<strong>PermitRootLogin<\/strong>\u00a0and set its value to\u00a0<code>no<\/code>\u00a0as shown in the screenshot.<\/p>\n<div id=\"attachment_30834\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/10\/disable-root-login-in-ssh.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-30834\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/10\/disable-root-login-in-ssh.png\" alt=\"Disable Root Login in SSh\" width=\"502\" height=\"173\" aria-describedby=\"caption-attachment-30834\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-30834\" class=\"wp-caption-text\">Disable Root Login in SSh<\/p>\n<\/div>\n<p>Once you are done, save and close the file. Then restart the\u00a0<strong>sshd<\/strong>\u00a0service to apply the recent change in configurations.<\/p>\n<pre>$ sudo systemctl restart sshd \r\nOR\r\n$ sudo service sshd restart \r\n<\/pre>\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<h3>4. Restrict root Acess to Services Via PAM<\/h3>\n<p><strong>Pluggable Authentication Modules<\/strong>\u00a0(<strong>PAM<\/strong>\u00a0in short) is a centralized, pluggable, modular, and flexible method of authentication on Linux systems. PAM, through the\u00a0<strong>\/lib\/security\/pam_listfile.so<\/strong>\u00a0module, allows great flexibility in limiting the privileges of specific accounts.<\/p>\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<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\u00a0<strong>\/etc\/pam.d\/<\/strong>\u00a0directory as shown.<\/p>\n<pre>$ sudo vim \/etc\/pam.d\/login\r\nOR\r\nsudo vim \/etc\/pam.d\/sshd\r\n<\/pre>\n<p>Next, add the configuration below in both files.<\/p>\n<pre>auth    required       pam_listfile.so \\\r\n        onerr=succeed  item=user  sense=deny  file=\/etc\/ssh\/deniedusers\r\n<\/pre>\n<p>When you are done, save and close each file. Then create the plain file\u00a0<strong>\/etc\/ssh\/deniedusers<\/strong>\u00a0which should contain one item per line and not world readable.<\/p>\n<p>Add the name root in it, then save and close it.<\/p>\n<pre>$ sudo vim \/etc\/ssh\/deniedusers\r\n<\/pre>\n<p>Also set the required permissions on this.<\/p>\n<pre>$ sudo chmod 600 \/etc\/ssh\/deniedusers\r\n<\/pre>\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<p>For more information, consult the relevant man pages.<\/p>\n<pre>$ man pam_securetty\r\n$ man sshd_config\r\n$ man pam\r\n<\/pre>\n<p>That\u2019s all! In this article, we have explained four ways of disabling the root user login (or account) in Linux. Do you have any comments, suggestions or questions, feel free to reach us via the feedback form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/disable-root-login-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0root\u00a0account 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\u00a0create\/update\/access\/delete other users\u2019 accounts,\u00a0install\/remove\/upgrade software packages, and so much more. Because &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/26\/4-ways-to-disable-root-account-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;4 Ways to Disable Root Account in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12410","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12410"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12410\/revisions"}],"predecessor-version":[{"id":12411,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12410\/revisions\/12411"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}