symdesign
4/12/2018 - 10:39 AM

Add SSH user to Digitalocean

Root Login

To log into your server, you will need to know your server's public IP address (hostname -i) and the password for the "root" user's account. If you have not already logged into your server, you may want to follow the first tutorial in this series, How to Connect to Your Droplet with SSH, which covers this process in detail.

ssh root@SERVER_IP_ADDRESS

Create a New User

Once you are logged in as root, we're prepared to add the new user account that we will use to log in from now on.

This example creates a new user called "demo", but you should replace it with a user name that you like:

adduser demo

Root Privileges

Now, we have a new user account with regular account privileges. However, we may sometimes need to do administrative tasks.

To avoid having to log out of our normal user and log back in as the root account, we can set up what is known as "super user" or root privileges for our normal account. This will allow our normal user to run commands with administrative privileges by putting the word sudo before each command.

To add these privileges to our new user, we need to add the new user to the "sudo" group. By default, on Ubuntu 14.04, users who belong to the "sudo" group are allowed to use the sudo command.

As root, run this command to add your new user to the sudo group (substitute the highlighted word with your new user):

usermod -aG sudo username

Add Public Key Authentication

On your local machine, output and copy the public key

cat ~/.ssh/id_rsa.pub

On the server, as the root user, enter the following command to switch to the new user (substitute your own user name):

su - demo

Now you will be in your new user's home directory.

mkdir .ssh
chmod 700 .ssh
nano .ssh/authorized_keys

Paste in the previously copied key and press CTRL-X to exit and save the file.

chmod 600 .ssh/authorized_keys
exit

Use your new User account

ssh demo@SERVER_IP_ADDRESS

Remember, if you need to run a command with root privileges, type "sudo" before it like this:

sudo command

If all is well, you can exit your sessions by typing:

exit