Create new Linux user on Ubuntu server with SSH public access key only

Add the user

sudo useradd -s /bin/bash -d /home/userubuntu/ -m -G sudo userubuntu

Create authorized_keys file and put you public ssh key

mkdir /home/userubuntu/.ssh/
chmod 0700 /home/userubuntu/.ssh/

sudo -- sh -c "echo 'put the ssh-rsa public key here' > /home/userubuntu/.ssh/authorized_keys"
sudo chown -R userubuntu:userubuntu /home/userubuntu/.ssh/

Allow the user to use sudo without password, type sudo visudo and edit the file, adding userubuntu ALL=(ALL) NOPASSWD:ALL like this

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
dimitar ALL=(ALL) NOPASSWD:ALL

Allow only public key access, no password allowed. sudo vim /etc/ssh/sshd_config

ChallengeResponseAuthentication no
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
UsePAM no

restart the ssh service : sudo systemctl restart sshd

Leave a Reply

Your email address will not be published. Required fields are marked *