{"id":12164,"date":"2021-11-18T06:10:47","date_gmt":"2021-11-18T06:10:47","guid":{"rendered":"https:\/\/linuxways.net\/?p=12164"},"modified":"2024-03-25T02:10:30","modified_gmt":"2024-03-25T02:10:30","slug":"methods-to-disable-root-account-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/methods-to-disable-root-account-in-linux\/","title":{"rendered":"Methods to disable root account in Linux"},"content":{"rendered":"<p>In Linux, the root user has access to everything and can do many things. It has access to each and every command of the linux system and can delete,edit, update, execute, read, write to all the available files and folders. It has all the permission so it is the supreme account on linux.<\/p>\n<p>Such access might be troublesome in most cases. As some users might execute wrong commands at the wrong time which will directly affect the system.<\/p>\n<p>In most cases, new users are created and given the limited privileges so the system will not be affected by troublesome commands. To perform the critical tasks. such users are given the privilege of root user by using sudo command.<\/p>\n<h2><a id=\"post-12164-_pnjl41wq844x\"><\/a>Create a user with sudo privilege<\/h2>\n<p>In this article, we are going to use ubuntu20.04 LTS. Let\u2019s create one user with the privilege of root user by providing sudo access. We can create such user by following the below command:<\/p>\n<pre>\r\n# adduser test\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"683\" height=\"340\" class=\"wp-image-12166\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-331.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-331.png 683w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-331-300x149.png 300w\" sizes=\"auto, (max-width: 683px) 100vw, 683px\" \/><\/p>\n<p>Now, add this user to the sudo group by following the below command so it can perform tasks with the root privilege.<\/p>\n<pre>\r\n# usermod -aG sudo test\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"676\" height=\"50\" class=\"wp-image-12167\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-332.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-332.png 676w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-332-300x22.png 300w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/p>\n<p>Here, a means append and G means a group. We append the test user to the sudo group.<\/p>\n<p>Now let&#8217;s switch to the test user which has sudo privilege.<\/p>\n<p>We are going to discuss different methods to disable root account in Linux. Some of the methods are discussed with the points below.<\/p>\n<h2><a id=\"post-12164-_j1uvpgbx12d5\"><\/a>Changing shell of root user to disable root login<\/h2>\n<p>One of the methods to disable root account in linux is to change the shell of the root user. First, Open the \/etc\/passwd file and change the \/bin\/bash or \/bin\/sh to \/sbin\/nologin. You can follow the below instruction to do so.<\/p>\n<pre>\r\n$ sudo vim \/etc\/passwd\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"75\" class=\"wp-image-12170\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-335.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-335.png 680w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-335-300x33.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Change the line as per above screenshot and save it.<\/p>\n<p>Now, the root user cannot login and certain messages will be displayed as shown in the below screenshot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"92\" class=\"wp-image-12171\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-337.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-337.png 682w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-337-300x40.png 300w\" sizes=\"auto, (max-width: 682px) 100vw, 682px\" \/><\/p>\n<h2><a id=\"post-12164-_pwm8naktbqb3\"><\/a>Disabling Root Login for SSH<\/h2>\n<p>When you try to login to servers, you can easily login to it through SSH. But, if you want to disable root login to that server, you can simply edit the \/etc\/ssh\/sshd_config file. You can edit with your favorable editor. Here, we are using nano.<\/p>\n<pre>\r\n$ sudo nano \/etc\/ssh\/sshd_config\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"451\" class=\"wp-image-12175\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-339.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-339.png 703w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-339-300x192.png 300w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/p>\n<p>Add the line as shown on screenshot or edit the line just above it to \u201cPermitRootLogin no\u201d to disable root login to that specific server.<\/p>\n<h2><a id=\"post-12164-_11vf0sl16vjd\"><\/a>Using passwd command to disable root login<\/h2>\n<p>It is easy to disable root login by using the passwd command.<\/p>\n<pre>\r\n$ sudo passwd -l root\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"237\" class=\"wp-image-12178\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-343.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-343.png 680w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-343-300x105.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p>Here, after running the above command, we cannot login to the root user until the new password is set for the root user.<\/p>\n<h2><a id=\"post-12164-_py4z33w2z6xn\"><\/a>Using usermod command to disable root login<\/h2>\n<p>It is similar to the passwd command with little change. We can run the following command to disable root login.<\/p>\n<pre>\r\n$ sudo usermod -L root\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"687\" height=\"75\" class=\"wp-image-12181\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-344.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-344.png 687w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/11\/word-image-344-300x33.png 300w\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" \/><\/p>\n<p>Same as above method, after running the usermod command, we cannot login to the root user until the new password is set for the root user.<\/p>\n<h2><a id=\"post-12164-_xn14tso26db0\"><\/a>Conclusion<\/h2>\n<p>We have discussed four methods to disable the root login in Linux. You can use any of these methods as per your requirement or purpose. We hope you enjoyed reading the article. Thank you!<\/p>","protected":false},"excerpt":{"rendered":"<p>In Linux, the root user has access to everything and can do many things. It has access to each and every command of the linux system and can&hellip;<\/p>","protected":false},"author":110,"featured_media":12184,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,5,2],"tags":[],"class_list":["post-12164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-mint","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12164","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\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=12164"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/12164\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/12184"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=12164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=12164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=12164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}