> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/lat-mov/remote-services/ssh-secure-shell.md).

# SSH - Secure Shell

## Configure Private Key Access

Create an SSH key-pair on your Kali machine

```bash
ssh-keygen -t rsa
```

Make sure the `/home/<user>/.ssh/id_rsa` file has correct file permissions

```bash
chmod 600 id_rsa
```

Then copy the contents of the `/home/<user>/.ssh/id_rsa.pub` file to the `/home/<taget_user>/.ssh/authorized_keys` file on the target machine.

The contents should look something like this

```bash
renu@MoneyBox:/home/lily/.ssh$ cat authorized_keys 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRIE9tEEbTL0A+7n+od9tCjASYAWY0XBqcqzyqb2qsNsJnBm8cBMCBNSktugtos9HY9hzSInkOzDn3RitZJXuemXCasOsM6gBctu5GDuL882dFgz962O9TvdF7JJm82eIiVrsS8YCVQq43migWs6HXJu+BNrVbcf+xq36biziQaVBy+vGbiCPpN0JTrtG449NdNZcl0FDmlm2Y6nlH42zM5hCC0HQJiBymc/I37G09VtUsaCpjiKaxZanglyb2+WLSxmJfr+EhGnWOpQv91hexXd7IdlK6hhUOff5yNxlvIVzG2VEbugtJXukMSLWk2FhnEdDLqCCHXY+1V+XEB9F3 renu@debian
```

The copying can be done either manually or with this command

```bash
ssh-copy-id -i id_rsa.pub user@$TARGET_IP
```

Now you can login with

```bash
ssh -i id_rsa user@$TARGET_IP
```

## SSH Server Administration

### Start the SSH Server

To start the SSH-server on Kali Linux

```bash
sudo systemctl start ssh
```

### Stop the SSH Server

To stop the SSH-server when done

```bash
sudo systemctl stop ssh
```

### Check status of the SSH Server

To check the status of the SSH-server on Kali Linux

```bash
sudo systemctl status ssh
```

### Restart the SSH Server

To restart the SSH-server, e.g. after a configuration change in **/etc/ssh/sshd\_config**

```bash
sudo systemctl restart ssh
```

## Resources

Secure Shell - Wikipedia: <https://en.wikipedia.org/wiki/Secure_Shell>

ssh - Linux manual page: <https://man7.org/linux/man-pages/man1/ssh.1.html>

sshd - Linux manual page: <https://man7.org/linux/man-pages/man8/sshd.8.html>

sshd\_config - Linux manual page: <https://man7.org/linux/man-pages/man5/sshd_config.5.html>

ssh-copy-id - Linux manual page: <https://man7.org/linux/man-pages/man1/ssh-copy-id.1.html>

ssh-keygen - Linux manual page: <https://man7.org/linux/man-pages/man1/ssh-keygen.1.html>

systemctl - Linux manual page: <https://www.man7.org/linux/man-pages/man1/systemctl.1.html>
