
[Local PC]
|
| SSH / VSCode
v
[VPS (공인 IP, 인바운드 허용)]
^
| Reverse SSH (outbound)
|
[Internal Server]
VPS 생성함 - Oracle, AWS 등 상용 서비스 : AWS Lightsail 사용하였음
방화벽에서 아래 포트 허용함
/etc/ssh/sshd_config 수정함
AllowTcpForwarding yes
GatewayPorts yes
SSH 재시작함
sudo systemctl restart ssh
VPS 접속용 키 준비함
chmod 600 vps_key.pem
Reverse SSH 실행함
ssh -i vps_key.pem -N -R 2222:localhost:22 vps_user@<VPS_IP>
의미
로컬 PC의 SSH config 작성함
파일 위치
Host vps
HostName <VPS_IP>
User vps_user
IdentityFile ~/.ssh/vps_key.pem
IdentitiesOnly yes
Host internal-server
HostName localhost
User internal_user
Port 2222
ProxyJump vps
ssh internal-server
sudo apt update
sudo apt install -y autossh
autossh -M 0 -N \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-i vps_key.pem \
-R 2222:localhost:22 \
vps_user@<VPS_IP>
/etc/systemd/system/reverse-ssh.service
[Unit]
Description=Reverse SSH Tunnel
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/bin/autossh -M 0 -N \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-i /path/to/vps_key.pem \
-R 2222:localhost:22 \
vps_user@<VPS_IP>
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable reverse-ssh
sudo systemctl start reverse-ssh
systemctl status reverse-ssh