> For the complete documentation index, see [llms.txt](https://ivalexev.gitbook.io/rednote/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ivalexev.gitbook.io/rednote/utility/service/ssh-22.md).

# SSH (22)

<details>

<summary>Protocol Information</summary>

SSH (Secure Shell) is a remote administration protocol that offers encryption and is the successor to Telnet. It is generally used for remote access to servers and systems.\
SSH authentication can be configured in two main ways: user/password, or with key.

</details>

## Port

<table data-header-hidden><thead><tr><th width="166">Port</th><th>Description</th></tr></thead><tbody><tr><td><mark style="color:green;"><strong>22</strong></mark><strong> TCP</strong></td><td>SSH</td></tr></tbody></table>

## Config File

* **Linux**: `/etc/ssh/sshd_config`
* **Windows**: `%ProgramData%\ssh\sshd_config`

## Interact

If **anonymous access** is enabled, it's possible log in with username `“Anonymous”` and password `“”`.

{% code overflow="wrap" %}

```bash
ssh <USER>@<IP>
```

{% endcode %}

{% code overflow="wrap" %}

```bash
ssh -i <KEY> <USER>@<IP>
```

{% endcode %}

### Setup

{% code overflow="wrap" %}

```bash
# Start
sudo systemctl start ssh
# Start on every login
sudo systemctl enable ssh
```

{% endcode %}

{% code overflow="wrap" %}

```bash
# Check
sudo systemctl status ssh
```

{% endcode %}

{% code overflow="wrap" %}

```bash
# Setup config
sudo vim /etc/ssh/sshd_config
## PermitRootLogin no
## PasswordAuthentication no
## PubkeyAuthentication yes
## AuthorizedKeysFile <1> <2>
```

{% endcode %}

{% code overflow="wrap" %}

```bash
# Restart
sudo systemctl restart ssh
```

{% endcode %}

{% code overflow="wrap" %}

```bash
# Stop
sudo systemctl stop ssh
# Disable
sudo systemctl disable ssh
```

{% endcode %}

### SSH Key

{% code overflow="wrap" %}

```bash
ssh-keygen
```

{% endcode %}

Move the public key in `~/.ssh/authorized_keys` target machine (append if it already exists).

{% code overflow="wrap" %}

```bash
cat id_rsa.pub >> authorized_keys
```

{% endcode %}

| Algorithm             | Private Key  | Public Key       |
| --------------------- | ------------ | ---------------- |
| RSA                   | `id_rsa`     | `id_rsa.pub`     |
| DSA (obsolete)        | `id_dsa`     | `id_dsa.pub`     |
| ECDSA                 | `id_ecdsa`   | `id_ecdsa.pub`   |
| Ed25519 (recommended) | `id_ed25519` | `id_ed25519.pub` |

### Upload Key on Server SSH

{% code overflow="wrap" %}

```bash
ssh-keygen -f ~/.ssh/id_rsa -N ""
ssh-copy-id -i ~/.ssh/id_rsa.pub <USER>@<IP>
```

{% endcode %}

### scp

```bash
scp [-r] [-O] <USER>@<IP>:/<REMOTE_FILE> /<TO_LOCAL>
scp [-r] [-O] /<LOCAL_FILE> <USER>@<IP>:/<TO_REMOTE>
```
