通过Linux中的代理(跳转)服务器进行SSH和SCP的4种方法

时间:2020-02-23 14:40:31  来源:igfitidea点击:

在本教程中,我们将学习如何通过代理服务器(跳转主机)进行SSH或者SCP

通过代理服务器的SCP

方法1:将scp与ProxyJump一起使用

openssh软件包版本7.4p1-11或者更高版本中,我们可以使用ProxyJump选项通过代理服务器传输文件。
通过代理传输文件的命令scp的语法是:

~]# scp -o "ProxyJump <User><@Proxy-Server>" <File-Name> <User><@Destination-Server>:<Destination-Path>

例如 :

~]# scp -o "ProxyJump [email protected]" dataFile.txt  [email protected]:/tmp
[email protected]'s password: 
[email protected]'s password: 
dataFile.txt                                                                                     100%    5     0.0KB/s   00:00

其中我的代理服务器是" 10.23.100.70",而目标服务器是" 192.168.10.100"

方法2:将scp与ProxyCommand一起使用

SCP使用ssh作为基础协议,因此我们可以将ssh选项与scp命令一起使用。
ProxyCommand选项与scp命令一起使用的语法是:

~]# scp -o "ProxyCommand ssh <user><@Proxy-Server> nc %h %p" <File-Name> <User<@Destination-Server>:<Destination-Path>

其中:

  • %h将被主机名替换以进行连接

  • %p将被端口替换

说明:

在使用ProxyCommand选项时,请确保在提供nc命令的代理服务器上安装了nmap-ncat软件包,否则将显示以下错误消息。

bash: nc: command not found
ssh_exchange_identification: Connection closed by remote host
lost connection

例如:

~]# scp -o "ProxyCommand ssh [email protected] nc %h %p" dataFile.txt  [email protected]:/tmp
[email protected]'s password: 
[email protected]'s password: 
dataFile.txt                                                                                     100%    5     0.0KB/s   00:00

其中我的代理服务器是" 10.23.100.70",而目标服务器是" 192.168.10.100"

通过代理服务器进行SSH

方法1:使用ssh选项传递ProxyCommand

我们可以再次使用ProxyCommand通过代理服务器SSH另一台服务器。
通过代理到SSH的语法为:

~]# ssh -o "ProxyCommand ssh user_name_on_proxy@hostname_or_IP_of_proxy nc %h %p" user_name_on_server@hostname_or_IP_of_server

示例:通过" 10.23.100.70"上的代理以" 192.168.10.100"上的root用户身份登录,并在" proxy_user"的代理上使用登录凭据

~]# ssh -o "ProxyCommand ssh [email protected] nc %h %p" [email protected]
[email protected]'s password: 
[email protected]'s password: 
Last login: Tue Dec 24 10:40:33 2019 from 10.23.100.70
~]# ip a l | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    inet 192.168.10.100/24 brd 192.168.1.255 scope global eth0

如果代理服务器没有安装" nc"命令,或者"我们没有代理服务器的登录凭据",但是代理服务器正在运行像squid这样的代理服务,它将接受SSH连接,则可以使用以下命令。
请注意,此方法要求我们在本地/客户端系统上安装了" nc"命令。

~]# ssh -o "ProxyCommand nc --proxy hostname_or_IP_of_proxy:proxy_service_port --proxy-type http %h %p" user_name_on_server@hostname_or_IP_of_server

例如,通过代理服务以root身份登录192.168.10.100,在10.23.100.70的端口3128上侦听。
代理服务不需要任何凭据。

~]# ssh -o "ProxyCommand nc --proxy 10.23.100.70:3128 --proxy-type http %h %p" [email protected]
[email protected]'s password: 
Last login: Tue Dec 24 10:40:46 2019 from 10.23.100.70
~]# ip a l | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    inet 192.168.10.100/24 brd 192.168.1.255 scope global eth0

方法2:使用ssh客户端配置文件

我们已经深入讨论了SSH客户端配置文件。

因此,除了提供所有选项作为SSH的输入参数之外,我们还可以使用SSH客户端配置文件。

根据以下内容编辑~/.ssh/config文件:

# vim ~/.ssh/config 
...
Host <nickname>
HostName <hostname_of_server>
User <user_on_server>
ProxyCommand ssh <user_on_server><@proxy_server> nc %h %p

说明:

如果此文件中已经有内容,则需要将以上内容添加到末尾。

其中

  • <nickname>:设置目标服务器的昵称

  • <hostname_of_sever>:设置真实的远程服务器/主机名

  • <user_on_server>:目标服务器上存在的真实用户

  • <proxy_server>:代理服务器的IP或者主机名

  • %h将被主机名替换以进行连接

  • %p将被端口替换

接下来,我们可以使用SSH添加详细选项来验证配置

~]# ssh -vvv <target_server>