Skip to content

Instantly share code, notes, and snippets.

@tumainimosha
Last active April 1, 2019 12:11
Show Gist options
  • Save tumainimosha/6d035ccb658fa944234bf6b27a5a4d2b to your computer and use it in GitHub Desktop.
Save tumainimosha/6d035ccb658fa944234bf6b27a5a4d2b to your computer and use it in GitHub Desktop.
Creating a new user and add ssh public key

Create new user

ubuntu@ip-172-31-37-13:~$ sudo adduser foouser
Adding user `foouser' ...
Adding new group `foouser' (1006) ...
Adding new user `foouser' (1006) with group `foouser' ...
Creating home directory `/home/foouser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for foouser
Enter the new value, or press ENTER for the default
	Full Name []: Foo Bar
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] y
ubuntu@ip-172-31-37-13:~$ 

Enter into new user's console

run: sudo su {{username}} Note: when command runs succesful the bash promt name changes from your user to the new user. See below, bash changes from ubuntu to foouser

ubuntu@ip-172-31-37-13:~$ sudo su foouser
foouser@ip-172-31-37-13:/home/ubuntu$ 

Change directory to the new users home directory

foouser@ip-172-31-37-13:/home/ubuntu$ cd ~
foouser@ip-172-31-37-13:~$ pwd
/home/foouser
foouser@ip-172-31-37-13:~$ 

Create .ssh folder in user's home director and change its permission to 700

foouser@ip-172-31-37-13:~$ mkdir .ssh
foouser@ip-172-31-37-13:~$ chmod 700 .ssh/

Change directory into newly created .ssh folder and create authorized_keys file in it

foouser@ip-172-31-37-13:~$ cd .ssh/
foouser@ip-172-31-37-13:~/.ssh$ nano authorized_keys

Paste new users public key in nano and save

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC52LoTJqmrEl4qDtH231rtmfJjHgbu3fbk+Yaq2szP5KEIw0Mr96DN40d9//RgHUgeuwROqNJwF6Y7nmp1ljnJW5O60YM9GhrsBN1k6DA2+2oCQH0n7UZWI/Q1k/kpwC/Ec0Rb664+Lu6sow0C$

Change permission of authorized_keys file to 600

foouser@ip-172-31-37-13:~/.ssh$ chmod 600 authorized_keys

Exit new users terminal

Exit the new users terminal to return to your own user terminal

foouser@ip-172-31-37-13:~/.ssh$ exit
exit
ubuntu@ip-172-31-37-13:~$

you have succesfully created new user and added their public key to server

If the user needs to have sudo access, add them to sudoers group

sudo usermod -aG sudo foouser

If the user needs to upload files to nginx webroot, add them to www-data group

sudo usermod -aG www-data foouser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment