How to allow root login from one IP address with ssh public keys only
Here’s how you can allow root login from one IP address using SSH public keys in a Linux environment:
- Make sure the target machine has an SSH server installed, such as OpenSSH.
- On the target machine, open the SSH configuration file, typically located at
/etc/ssh/sshd_config. - Find the line that starts with
PermitRootLoginand set it toyes:
PermitRootLogin yes- Find the line that starts with
PasswordAuthenticationand set it tono:
PasswordAuthentication no- Add the following line at the end of the file to specify the IP address that should be allowed to log in as root using a public key:
Match Address <ip_address>
n PermitRootLogin yes
n PasswordAuthentication no
n PubkeyAuthentication yes- Replace
<ip_address>with the desired IP address. - Save the changes to the file and close it.
- Restart the SSH server to apply the changes:
systemctl restart ssh- On the client machine, generate a public-private key pair using the following command:
ssh-keygen -t rsa- Copy the public key to the target machine:
ssh-copy-id root@<target_machine_ip_address>- From now on, you should be able to log in as root from the specified IP address using the public key.
