yyp@yyp-HP-246-Notebook-PC:~$ sudo apt-get -y install autossh
Processing triggers for man-db (2.8.3-2ubuntu0.1) …
yyp@yyp-HP-246-Notebook-PC:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yyp/.ssh/id_rsa):
Created directory ‘/home/yyp/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yyp/.ssh/id_rsa.
Your public key has been saved in /home/yyp/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NJr/kt2cMYA4。。。。。。yyp@yyp-HP-246-Notebook-PC
The key’s randomart image is:
±–[RSA 2048]----+
|oo… oO|
| .o.o+.o |
±—[SHA256]-----+
yyp@yyp-HP-246-Notebook-PC:~$ ssh-copy-id -i .ssh/id_rsa.pub root@123.123.123.123
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “.ssh/id_rsa.pub”
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: ERROR: ssh: connect to host 123.123.123.123 port 22: Connection timed out
yyp@yyp-HP-246-Notebook-PC:~$
yyp@yyp-HP-246-Notebook-PC:~$
yyp@yyp-HP-246-Notebook-PC:~$
yyp@yyp-HP-246-Notebook-PC:~$
https://www.jianshu.com/p/09fd97f8c43f
ubuntu16.04利用ssh反向代理及autossh实现内网穿透
0.072
2019.08.27 13:56:45
字数 280
阅读 121
内网机器:A
公网机器:B
1、内网机器(A)安装autossh
apt-get -y install autossh
配置免密登录
ssh-keygen
ssh-copy-id -i .ssh/id_rsa.pub root@123.123.123.123//此地址只用作举例
启动端口(A)
autossh -fNR 8888:localhost:22 -p 40022 root@123.123.123.123//此地址只用作举例
参数解释:
-f:是指autossh后台运行,不会阻塞shell继续向下执行;
-N:是指建立的ssh连接只用于转发数据,不解析命令;
-R:是指建立反向隧道,一般我们ssh某个服务器是正向隧道;
8888:是公网服务器上的代理端口;
localhost:22是指代理到本机时需要访问的ip和端口,即内网机器A的ip地址+端口;
-p:是外网机器B的ssh端口;
最后是公网服务器的地址,autossh会调用ssh建立到它的ssh反向隧道。
注意:autossh只有三个命令,其他命令都是通过调用ssh提供的,autossh在这里只是起到了监听ssh是否断开并将其自动拉起的作用。执行完这条命令,将会在外网机器(B)123.123.123.123上启动并监听8888端口,如下(外网机器查看)
clipboard.png
可知,该端口是默认绑定在lo地址上的,所以在外网机器上反向登陆服务器时,可用localhost或者127.0.0.1,但是不能使用真实网卡的地址去连接。
以上操作,除过查看端口的操作外,其他操作全部在内网机器完成。
2、外网机器连接内网服务器
ssh -p 8888 localhost
或
ssh -p 8888 127.0.0.1
3、添加到系统服务,方便管理
cd /etc/systemd/system/ && vim autossh.service
[Unit]
Description=Front autossh
After=sshd.target
[Service]
User=root
Type=simple
ExecStart=/usr/bin/autossh -p 40022 -NR 8888:localhost:22 root@123.123.123.123
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always
[Install]
WantedBy=multi-user.target
启停autossh
systemctl stop|start|status autossh.service
以上内容仅为个人的理解,有不对的地方欢迎指正