Creating user with ssh access

25th April, 2024

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

# Switch to new user
$su new_user

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

# Copy paste the public ssh keys here
$vim .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