问题描述
在宿舍有一台Raspberry Pi,运行着Transmission和sshd。外网有一台VPS,可被Raspberry Pi访问。现在有另一台主机(我的笔记本XPS),无外网IP,无法直接访问Raspberry Pi,希望远程访问Raspberry Pi上的Transmission和sshd。
解决方案
在Raspberry Pi上的操作
将id_rsa.pub中的内容添加到远程服务器
1
| ssh $USERNAME@$SERVER_IP 'mkdir -p .ssh && cat >> .ssh/authorized_keys' < ~/.ssh/id_rsa.pub
|
/etc/systemd/system下新建transmission-rev-tun.service,内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [Unit]
Description=AutoSSH to Huawei Cloud: (19091, 9091)
Wants=network-online.target
After=network-online.target
[Service]
EnvironmentFile=/etc/systemd/system/rev-tunnel/huawei.conf
ExecStart=autossh -M 0 -R 19091:localhost:9091 $USERNAME@$SERVER_IP -p $SERVER_SSH_PORT -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -N -i /root/.ssh/id_rsa
ExecReload=/bin/kill -s HUP $MAINPID
RestartSec=60
Restart=always
[Install]
WantedBy=multi-user.target
|
/etc/systemd/system下新建ssh-rev-tun.service,内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [Unit]
Description=AutoSSH to Huawei Cloud: (10022, 22)
Wants=network-online.target
After=network-online.target
[Service]
EnvironmentFile=/etc/systemd/system/rev-tunnel/huawei.conf
ExecStart=autossh -M 0 -R 10022:localhost:22 $USERNAME@$SERVER_IP -p $SERVER_SSH_PORT -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -N -i /root/.ssh/id_rsa
ExecReload=/bin/kill -s HUP $MAINPID
RestartSec=60
Restart=always
[Install]
WantedBy=multi-user.target
|
然后运行systemctl enable --now ssh-rev-tun.service transmission-rev-tun.service。
在XPS上的操作
1
2
| ssh -L 19091:localhost:19091 $USERNAME@$SERVER_IP -o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" -N -T -f
ssh -L 10022:localhost:10022 $USERNAME@$SERVER_IP -o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" -N -T -f
|