Ssh – Add a user without password but with SSH and public key

authenticationpasswordsshuseradd

I want to add a user to Red Hat Linux that will not use a password for logging in, but instead use a public key for ssh. This would be on the command line.

Best Answer

Start with creating a user:

useradd -m -d /home/username -s /bin/bash username

Create a key pair from the client which you will use to ssh from:

ssh-keygen -t dsa

Copy the public key /home/username/.ssh/id_dsa.pub onto the RedHat host into /home/username/.ssh/authorized_keys

Set correct permissions on the files on the RedHat host:

chown -R username:username /home/username/.ssh
chmod 700 /home/username/.ssh
chmod 600 /home/username/.ssh/authorized_keys

Ensure that Public Key authentication is enabled on the RedHat host:

grep  PubkeyAuthentication /etc/ssh/sshd_config
#should output:
PubkeyAuthentication yes

If not, change that directive to yes and restart the sshd service on the RedHat host.

From the client start an ssh connection:

ssh username@redhathost

It should automatically look for the key id_dsa in ~/.ssh/. You can also specify an identity file using:

ssh -i ~/.ssh/id_dsa username@redhathost
Related Question