62 lines
1.1 KiB
Markdown
62 lines
1.1 KiB
Markdown
## 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
|
|
``` |