Add user & ssh key in Linux/Ubuntu

If you have just setup VPS (i.e ubuntu 20.04 or any) and want to add new user with its own SSH key, then do:

username=myuser
adduser $username

usermod -aG sudo $username
sshDir=/home/$username
mkdir -p $sshDir/.ssh
touch $sshDir/.ssh/authorized_keys
chmod -R go= $sshDir/.ssh
# idk, if this is needed: chown root:root $sshDir/
chown -R $username:$username $sshDir/
chmod 644 $sshDir/.ssh/authorized_keys
chmod 700 $sshDir/.ssh

next, add the ssh public key (ssh-rsa .....) in authorized_keys file on new line:

nano $sshDir/.ssh/authorized_keys

That’s all.
If you want to be able to log in as the user with PASSWORD (without SSH key), then do:

nano /etc/ssh/sshd_config
find PasswordAuthentication and enable it

(if you forget password, reset it by: passwd $username )

Leave a Comment

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

Puvox - Blog
Scroll to Top