Add SSH-Tunnel-Quick-Setup.md

This commit is contained in:
khairul169 2024-07-06 23:43:56 +00:00
parent 9070517bdb
commit 56c9d2e555

62
SSH-Tunnel-Quick-Setup.md Normal file
View File

@ -0,0 +1,62 @@
## Generate SSH Key
```sh
ssh-keygen -t ed25519 -C "khairul169"
cat .ssh/id_ed25519.pub
```
## Add SSH Public Key to the server
```sh
ssh root@hostname
mkdir .ssh
nano .ssh/authorized_keys
```
## Test SSH Connection
```sh
ssh root@hostname -i .ssh/id_ed25519
```
## Install AutoSSH
```sh
sudo apt install autossh -y
```
## Create Tunnel Script
`sudo nano /etc/ssh-tunnel.sh`
```sh
#!/bin/sh
SSH_KEY="/root/.ssh/id_ed25519"
SRC_PORT=80
SRC_HOST=localhost
DEST_PORT=8081
autossh -M 0 root@hostname -R "$DEST_PORT:$SRC_HOST:$SRC_PORT" -i $SSH_KEY -N -o "StrictHostKeyChecking no" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -o "ExitOnForwardFailure yes"
```
`sudo chmod +x /etc/ssh-tunnel.sh`
## Create & Enable Systemd Service
`sudo nano /etc/systemd/system/sshtunnel.service`
```
[Unit]
Description=AutoSSH tunnel service to My SSH Tunnel Server
[Service]
Environment="AUTOSSH_GATETIME=0"
ExecStart=/etc/ssh-tunnel.sh
Restart=on-failure
RestartSec=10
TimeoutSec=10
[Install]
WantedBy=default.target
```
```sh
sudo systemctl daemon-reload
sudo systemctl enable --now sshtunnel
sudo systemctl status sshtunnel
```