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
PermitRootLogin
and set it toyes
:
PermitRootLogin yes
- Find the line that starts with
PasswordAuthentication
and 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>
PermitRootLogin yes
PasswordAuthentication no
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.