How to Disable SSH Login for root User

One way to secure your server is to disable the root access or login through ssh, because any cracker can attempt to brute force your root password and potentially get access to your system if they can figure out your password. The safest way is to keep a separate account that you can regularly use and simply sudo to root when necessary.

1. Add the user.
– In the following example, we will use the user name burnz.
– The command adduser will automatically create the user, initial group, and home directory.

[ ~]# adduser burnz

[ ~]# id burnz
uid=10018(burnz) gid=10018(burnz) groups=10018(burnz)

[ ~]# ls -lad /home/burnz/
drwx------ 2 burnz burnz 4096 Dec 25 16:01 /home/burnz/

2. Set the password for the burnz user. When prompted, type and then retype the password.

[ ~]# passwd burnz
Changing password for user burnz.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[ ~]#

3. SSH to the server with the new burnz user and ensure that the login works.

[root@localhost ~]#ssh's password:

[ ~]$

4. Verify that you can su (switch user) to root with the burnz user.

[ ~]$ su -

[ ~]$ whoami

5. Edit /etc/ssh/sshd_config with your favorite text editor.

[root@root ~]# nano /etc/ssh/sshd_config
Change this line:
PermitRootLogin yes

Edit to this:
PermitRootLogin no

6. Ensure that you are logged into the box with another shell before restarting sshd to avoid locking yourself out of the server.

[root@root ~]# /etc/init.d/sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]

You will now be able to connect to your server via ssh with the burnz user and then use the command su to switch to the root user.