Creating user with ssh access

14th January, 2017

There are times when you are an admin of a machine and would like to give ssh access to another users. To do so, one can follow the following steps in Linux (Debian/Ubuntu).

# Add user with no password prompt 
#(here its new_user, so replace new_user with your new username)
$sudo adduser new_user --disabled-password

# Create .ssh directory
$cd /home/new_user
$sudo mkdir .ssh

# Copy paste the public ssh key here
$sudo vim .ssh/authorized_keys

# Set permissions
$sudo chown -R new_user:new_user /home/new_user/
$sudo chown root:root /home/new_user
$sudo chmod 700 /home/new_user/.ssh
$sudo chmod 600 /home/new_user/.ssh/authorized_keys

# Other command lines:

# To restart ssh
$sudo service ssh restart

# To delete user
$sudo deluser new_user
# To delete user and its home directory togther in one step.
$sudo deluser --remove-home new_user

# Grant sudo access to user
$sudo usermod -aG sudo new_user

# Get list of sudo users
$grep -Po '^sudo.+:\K.*$' /etc/group

# To set the permissions of the directories right
chmod go-w ~/
chown -R $USER ~/.ssh
chmod 0700 ~/.ssh
chmod 0600 ~/.ssh/authorized_keys