wiki-grav/pages/04.other/ssh/default.en.md

162 lines
3.5 KiB
Markdown
Raw Normal View History

---
title: SSH
visible: true
---
[toc]
2023-01-05 12:44:27 +01:00
## Linux Server
2023-01-05 12:44:27 +01:00
### Installation
2023-01-05 12:44:27 +01:00
#### Debian
2023-01-05 12:44:27 +01:00
`# apt install openssh-server`
#### Arch
2023-01-05 12:44:27 +01:00
`# pacman -S openssh`
`# systemctl enable ssh`
### Configuration file
2023-01-05 12:44:27 +01:00
`/etc/ssh/sshd_config`
Make sure to restart the sshd service after changes.
### Change port
2023-01-05 12:44:27 +01:00
Uncomment `Port` and set any port number
### Root login
2023-01-05 12:44:27 +01:00
`PermitRootLogin` setting
```
yes -> Able to log in with password as root
```
### Password Authentication
2023-01-05 12:44:27 +01:00
`PasswordAuthentication` setting
```
yes -> Allow login with passwords
no -> Only allow ssh keys
```
2023-01-05 12:44:27 +01:00
On OpenBSD also set `KbdInteractiveAuthentication` to `no`
## Windows Server
2023-01-05 12:44:27 +01:00
Open PowerShell as administrator
`Add-WindowsCapability -Online -Name OpenSSH.Server`
Start service
2023-01-05 12:44:27 +01:00
`Start-Service sshd`
Enable service
2023-01-05 12:44:27 +01:00
`Set-Service -Name sshd -StartupType 'Automatic'`
Check whether firewall rule exists
2023-01-05 12:44:27 +01:00
`Get-NetFirewallRule -Name *ssh*`
Create firewall rule for port 22
2023-01-05 12:44:27 +01:00
`New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22`
## Linux Client
2023-01-05 12:44:27 +01:00
### Configuration file
2023-01-05 12:44:27 +01:00
`/etc/ssh/ssh_config`
### Connect to non-standard port
2023-01-05 12:44:27 +01:00
`$ ssh -p (port) (user)@(ip)`
### X11 passthrough
2023-01-05 12:44:27 +01:00
`$ ssh -X (user)@(ip)`
### ssh keys
2023-01-05 12:44:27 +01:00
Create new key:
2023-01-05 12:44:27 +01:00
`$ ssh-keygen`
2023-01-05 12:44:27 +01:00
_Example_ for ed25519 key:
`$ ssh-keygen -t ed25519`
2023-01-05 12:44:27 +01:00
The "-C" flag can be used to add comments in ssh key files.
Enable the ssh key:
2023-01-05 12:44:27 +01:00
`$ ssh-copy-id -i (public key file) (user)@(ip/domain)`
If you are copying the ssh key from a different client, use the "-f" flag
2023-01-05 12:44:27 +01:00
`$ ssh-copy-id -f -i (public key file) (user)@(ip/domain)`
## Windows Client
2023-01-05 12:44:27 +01:00
Open PowerShell as administrator
2023-01-05 12:44:27 +01:00
`Add-WindowsCapability -Online -Name OpenSSH.Client`
## SSH Tunnel systemd Service
2023-01-05 12:44:27 +01:00
SSH tunnels can be created as systemd services
_Example tunnel:_
`ssh -NTfL 8080:webserver:80 user@remotehost`
### Tunnel settings
2023-01-05 12:44:27 +01:00
Save the file under `/etc/systemd/system/(application/tunnel name)`
```
PATH_TO_KEY=(ssh key path)
LOCAL_PORT=8080
REMOTE_ADDR=webserver
REMOTE_PORT=80
REMOTE_USER=user
REMOTE_HOST=remotehost
```
### Tunnel service
2023-01-05 12:44:27 +01:00
This service can be used with multiple different "tunnel settings" files. Similar to how the wg-quick service works with different wireguard configs.
2023-01-05 12:44:27 +01:00
Save this file under `/etc/systemd/system/local-tunnel@.service`
```
[Unit]
Description=Setup a local tunnel to %I
After=network.target
[Service]
EnvironmentFile=/etc/default/local-tunnel@%i
ExecStart=/usr/bin/ssh -i ${PATH_TO_KEY} -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -nNT -L ${LOCAL_PORT}:${REMOTE_ADDR}:${REMOTE_PORT} ${REMOTE_USER}@${REMOTE_HOST}
RestartSec=15
Restart=always
KillMode=mixed
[Install]
WantedBy=multi-user.target
```
Finally, the tunnel can be enabled
`# systemctl daemon-reload`
2023-01-05 12:44:27 +01:00
`# systemctl enable --now local-tunnel@(application/tunnel name)`
2023-01-05 15:40:19 +01:00
## CLI Options
### No matching host key type found
Full error message:
`Unable to negotiate with <host> port <port>: no matching host key type found. Their offer: <comma separated list of host keys>`
This happens, when a host key is used that has been deprecated in the locally installed ssh client.
Use the option `-oHostKeyAlgorithms=+<host key type>` with ssh to connect regardless.
## References
2023-01-05 12:44:27 +01:00
- [Windows OpenSSH Installation](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse)
- [SSH Tunnel as systemd service](https://ivanmorenoj.medium.com/ssh-tunnel-as-systemd-service-3c53bd157ac1)