Ubuntu Terminal – adding user with SSH access

Objective

In the following example I’m doing the following

  • create a new user using ubuntu terminal
  • add my public key to authorized keys
  • add user to sudoers (to be able to use sudo)
  • enable ssh access for this user

Scripts

# creating user
adduser [username]

# adding user to sudo (administrative privelleges)
visudo 

# search for a line "root ALL=(ALL:ALL) ALL" and add a new line there
[username] ALL=(ALL:ALL) ALL

# delete a user
deluser [username]

# when deleting the user, the line added in visudo should be also deleted
# delete home directory of the user along with the account deletion
deluser --remove-home [username]
# adding user without password, with ssh access and sudo 

# 1. adding user without password
adduser --disable-password --gecos "" [username]

# 2. creating ssh user and supplying the public key
mkdir /home/[username]/.ssh
touch -f /home/[username]/.ssh/authorized_keys

echo "[public key contents]" >> /home/[username]/.ssh/authorized_keys

echo "AllowUsers [username]" >> /etc/ssh/sshd_config
sudo service ssh reload

# 3. giving sudo access
echo "[username] ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/[username]

Majd Arbash

...

Leave a Reply