I have centos5.

Is there any way that i can log into my vps server with root user from particular ip address only.

I have read that i can use private key to login into sshd. But the problem is i am using SFTP for all my webistes and i don't want non IT users to use keys to login with SFTP.

OR is there any way that only root can use keys to login into shell but for others its normal password


A better way now is to use the Match keyword:

Match Host myworkstation
        PermitRootLogin yes


Match Address
        PermitRootLogin yes

That way, you can leave PermitRootLogin set to 'no', but you can still log in as root from your workstation.

This can also be used, for example, to allow root to rsync data between two hosts.

  • your rsync example is exactly the use-case for which I googled this question. Thank you! ;)
    – Jan
    Jun 1 '17 at 13:57
  • Be aware that this only works using the reverse DNS of the source IP address. (at least for me it was the only way)
    – mveroone
    Jul 18 '18 at 10:37

It is generally a better practice to log in as a non-privileged user first then use 'su -' or 'sudo' to gain root privileges, but...

You could always put the IP restriction on your key in ~root/.ssh/authorized_keys:

from="" ssh-rsa AAAAh9uif...auwehuf== yourkey@yourhost.com

This would allow ssh using the yourkey@yourhost.com key only from

  • Agreed, although that is more complicated to do for things like sftp, rsync, sshfs...
    – mveroone
    Jul 18 '18 at 10:38


PermitRootLogin without-password

In /etc/ssh/sshd_config. Every user excluding root will be allowed to use password logins. Root needs to use keys to login.

  • Will this slution work?? did u tried this
    – John
    Oct 17 '10 at 2:13
  • I use that setting in every server I maintain. It prevents root bruteforce/dict attacks. unixhelp.ed.ac.uk/CGI/man-cgi?sshd_config+5
    – rubiojr
    Oct 17 '10 at 22:12
  • This seems to break git pull though: /etc/ssh/ssh_config: line 68: Bad configuration option: permitrootlogin
    – geoidesic
    Jul 16 '17 at 7:11
  • Also PermitRootLogin does not appear in man ssh_config
    – geoidesic
    Jul 16 '17 at 7:32
  • Also easily overridden with ssh -o PreferredAuthentications=password
    – geoidesic
    Apr 30 '18 at 20:33

Edit sshd_config (usually in /etc/ssh), and add or change the following directives

  PermitRootLogin yes
  AllowUsers root@thehosttoallow

Then restart the daemon

  service ssh restart
  • I believe that, if the OP wanted to use AllowUsers, he would need to specify all users he want to have access. Your example would only allow root to authenticate via ssh.
    – EEAA
    Oct 11 '10 at 0:40
  • 1
    Yes, this is on purpose. To allow any user, *@thehosttoallow indeed let any user in. Oct 11 '10 at 0:43
  • Also easily overridden with ssh -o PreferredAuthentications=password
    – geoidesic
    Apr 30 '18 at 20:36

First, why would you want to prevent users from using key auth? That makes no sense to me.

Second, don't allow root login via ssh. Just don't do it - there's no good reason for needing to do so. It goes against every best practice out there, and for good reason. If you need to grant permissions to read/write certain files, you should be able to do so through the standard linux filesystem permissions. If you need more fine-grained access control, look into the linux ACL system.

  • what if i disable root login and neither of any user has access to some main config file. Then i am locked as i can't login via root
    – John
    Oct 11 '10 at 0:33
  • 1
    When you disable root login via ssh, root will still be able to sign in via the console. Also, you always have the option of granting a normal user root-esque permissions via sudo. This won't work for sftp, but as a failback in case you need to fix things, it would work fine and is the preferred way of granting permissions.
    – EEAA
    Oct 11 '10 at 0:36
  • 1
    @ErikA Allowing root from a unique host is not unsafe. There was a trend against the root access via telnet a few years back (besore ssh was common), but allowing root via ssh to only one host doesn't look that unsafe. Oct 11 '10 at 0:41
  • Sure, it may perhaps be "safe", but I'll still argue that it's a poor idea. I say this mainly due to the fact that, when people are given an easy way to get root access, they will use it, rather than using a safer, more proper way to gain access (sudo).
    – EEAA
    Oct 11 '10 at 1:17
  • 2
    Considering the author question, one can assume he wants to perform administrative tasks through the root user. Allowing direct root access from only one host in this case is a reasonable alternative. Oct 11 '10 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy