一、服务器配置
1.配置公网的服务器ssh登录(如腾讯云服务器、阿里云服务器)
sudo apt-get install openssh-server #安装openssh-server
sudo service ssh start #启动openssh-server
ps -e |grep sshd #查看ssh服务是否启动 -e查看全部进程
service ssh status #查看ssh运行状态
2.增加用户:
sudo adduser test_user (删除则用userdel 或userdel -r连同用户文件一起删除)输入密码后直接回车
root@root:~# sudo adduser test
Adding user ` test' ...
Adding new group ` test' (1001) ...
Adding new user ` test' (1001) with group ` test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]Y
3.生成公私秘钥
登入test用户后, ssh-keygen -t rsa直接回车。生成的私钥可以保存到客户端A机器,用于无密码登录;
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa):
Created directory '/home/test/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/test/.ssh/id_rsa.
Your public key has been saved in /home/test/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7g47YR3S39cHlfPK2TwzL69/HFNBm7fKM71J85z1WL0 test@root
The key's randomart image is:
+---[RSA 2048]----+
| ...|
| ++|
| . +=|
| . o . =|
| oSo . . O.|
| o.. . o B**|
| .... * =%|
| .+ =+&|
| .oo oEB|
+----[SHA256]-----+
4.配置公钥到配置文件并重启
cat /home/test/.ssh/id_rsa.pub >>/home/test/.ssh/authorized_keys
sudo service ssh restart
sudo service sshd restart
5.配置云服务器允许所有端口,或部分端口
更改云服务器配置:
配置/etc/ssh/sshd_config文件的项为Yes
重启云服务器
GatewayPorts yes
二:客户端A反向远程连接服务器
ssh -i /test/id_rsa -o StrictHostKeyChecking=no -CqTfnN -R :33333:localhost:22222 test@remotehost
id_rsa:服务器配对的私钥
-o StrictHostKeyChecking=no :登录时不用输yes
33333:指定远程服务器连接端口
localhost:客户端A
22222:客户端A的ssh使用端口
test:服务器用户名
remotehost:服务器IP
三、客户端B通过服务器反向连接客户端A
ssh -p 33333 root@remotehost
33333:客户端A指定连接服务器端口
root:客户端A的用户
remotehost:服务器的ip
输入密码为客户端A的root用户登录密码
ssh反向登录
最新推荐文章于 2024-09-04 11:00:19 发布