-
实现SSH客户端免密登陆SSH服务端
默认使用SSH登陆服务端时,系统会提示输入服务端的用户密码,为了省去输入密码这一步,可以执行如下操作:
(1)生成ssh秘钥,如果不执行将提示无法识别“ssh-copy-id”错误
ssh-keygen -t rsa
执行成功提示信息如下:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hailian/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/hailian/.ssh/id_rsa.
Your public key has been saved in /home/hailian/.ssh/id_rsa.pub.
The key fingerprint is:
93:40:84:46:b8:6a:4b:cb:08:38:e3:c9:f7:1b:f6:33 hailian@ubuntu
(2)¬设置登陆密码到秘钥文件中
ssh-copy-id -i .ssh/id_rsa.pub root@47.103.49.210
执行成功后,根据提示输入服务端密码,打印信息如下:
root@47.103.49.210’s password: (此处输入服务端密码)
Now try logging into the machine, with “ssh ‘root@47.103.49.210’”, and check in:
~/.ssh/authorized_keys
to make sure we haven’t added extra keys that you weren’t expecting.
(3)验证无密码登陆
ssh root@47.103.49.210
将成功登陆到ssh服务端,无需输入密码,打印信息如下:
Last login: Sat Feb 29 10:49:33 2020 from 111.179.138.233
Welcome to Alibaba Cloud Elastic Compute Service !
[root@iZuf6hf3z0yo0ut46ahh2rZ ~]# -
用autossh工具实现掉线自动连接
SSH是基于TCP/IP实现的,长时间不用会自动关断,为了实现稳定传输,可以使用autossh工具实现掉线自动连接
(1)首先执行autossh如果未安装,系统将提示安装信息
hailian@ubuntu:~$ autossh (检查是否安装)
The program ‘autossh’ is currently not installed. You can install it by typing:
sudo apt-get install autossh
hailian@ubuntu:~$ sudo apt-get install autossh
(2)设置掉线自动连接
sudo autossh -M 5021 -fCNR 5022:localhost:22 root@47.103.49.210
执行完成,会弹出一个输入“root@47.103.49.210”密码的对话框,将对应密码输入点击“确认”即可。
(3)完成以上步骤,即可实现公网SSH访问私网客户端了,在服务端执行
ssh –p 5022 hailian@47.103.49.210 (此处 IP可用localhost替代)服务端连接成功后,打印信息如下:
[root@iZuf6hf3z0yo0ut46ahh2rZ ~]#
[root@iZuf6hf3z0yo0ut46ahh2rZ ~]# ssh -p 5022 hailian@localhost
hailian@localhost’s password:
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-generic x86_64)
Documentation: https://help.ubuntu.com/
New release ‘14.04.5 LTS’ available.
Run ‘do-release-upgrade’ to upgrade to it.
Last login: Sat Feb 29 00:44:08 2020 from localhost
hailian@ubuntu:~$ ls
Desktop kingrp32888.1.tar minicom.log Pictures rk32885.1 test Videos
Documents lollipop2ndrelease mount-dir Public rk32888.1 tftpboot Downloads lollipop2ndreleasepro6818 Music QT6818 Templates ubuntu6818
hailian@ubuntu:~$
(4)最后可以设置开机自动启动autossh,在/etc/rc.local中添加命令:
sudo autossh -M 5021 -fCNR 5022:localhost:22 root@47.103.49.210 -
在服务端设置端口映射,实现不同地点内网互相访问
前面完成了内网到公网的透传,实现在公网上通过5022端口可以反向连接到内网。此时公网5022端口已经与内网SSH默认22端口建立连接,因此其他内网客户端无法再访问5022端口,因此需要再次将5022端口映射到另外一个端口5023,此过程称为SSH的正向代理,在公网电脑上执行命令:
sudo ssh –fCNL *:5023:loclalhost:5022 localhost
执行成功,则可以使用一台内网电脑运行如下指令连接到另外一台内网电脑的SSH服务端:
sudo ssh –p 5023 hailian@47.103.49.210
转载:https://blog.csdn.net/hltx666/article/details/104578049