# SSH-Key access as a non-root user.

[The previous article](https://wamaithanyamu.com/secure-server-login-using-ssh-keys) covered how to log in to a Vultr instance using ssh keys as root. It is recommended to disable root access in the event of an attack. Root privileges mean the user has access to all aspects of the system. Normally, ssh keys are used using automation tools such as Ansible to perform automated tasks such as pulling code from github on the server and restarting the server. For such a task, Ansible having root access would be a point of vulnerability should attackers get hold of the Ansible private ssh keys. Therefore, creating a non-root user with limited permissions is advised. 

Login to the Vultr instance or whichever ssh server you are using. This guide assumes the ssh server is an Ubuntu 22.04 LTS x64 instance.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669885190195/pCPlyItxH.png align="left")

## Install tmux
tmux is an open-source terminal multiplexer for Unix-based systems. tmux is used for simultaneously running multiple terminal sessions. For example, you may need one terminal running the frontend server and another terminal running a backend server on a full-stack application. 

Install tmux using:

```sh
sudo apt-get install tmux
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669884904162/n-aHpW6Kt.png align="left")

Start a tmux session using the tmux command.

```sh
tmux
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669887416952/ylJ2cNvTz.png align="left")
You can exit the tmux session using the exit command.

```sh
exit
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669887446268/DAu4esztG.png align="left")

To get back to the tmux session, list all available tmux sessions using tmux ls.

```sh
tmux ls
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669887293274/4XtzvVz4R.png align="left")

Re-launch the session using

```sh
tmux attach-session -t 0
```


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669887513479/8jRVUxGrk.png align="left")

## Create a new user

Create a new user using the adduser command followed by the name you want to assign to the new user.
```sh
adduser wamaitha
```
Create a password for the user and fill in the rest of the details on the prompt.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669885769874/gJeWUlYYQ.png align="left")

Verify the user was created using awk. Awk is a programming language used on bash as a scripting tool for text processing. Awk can be used for pattern matching. Users are stored on the /etc/passwd file. The command below returns the names of the users on the system as the /etc/passwd file is quite verbose.

```sh
awk -F':' '{ print $1}' /etc/passwd
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669888040270/vvPAMa6OL.png align="left")

## Move SSH keys
Create a .ssh directory on the new user's home directory

```sh
mkdir /home/wamaitha/.ssh
```

Move the ssh keys to the new folder

```sh
mv /root/.ssh/authorized_keys /home/wamaitha/.ssh
```
Change ownership of the .ssh directory from root to the new user.

```sh
chown -R wamaitha:wamaitha /home/wamaitha/.ssh
```

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669895687704/8l95fkem2.png align="left")

By default, new users are added to a group of the same name. Log out from tmux using exit, then log out from Vultr using exit.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669896012610/28RlpqNhy.png align="left")

Login as the new user using ssh with the username as follows


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1669896096091/ldysBZ7jy.png align="left")

Use the same passphrase used while generating the ssh keys. 

