SSH操作
端口转发
本地转发
本地转发(local forwarding)指的是,创建一个本地端口,将发往该端口的所有通信都通过 SSH 服务器,转发到指定的远程服务器的端口。这种情况下,SSH 服务器只是一个作为跳板的中介,用于连接本地计算机无法直接连接的远程服务器。本地转发是在本地计算机建立的转发规则。
它的语法如下,其中会指定本地端口(local-port)、SSH 服务器(tunnel-host)、远程服务器(target-host)和远程端口(target-port)。
# 与 tunnel-host 建立ssh channel, 当访问本机的 local-port 使, 将请求传输到 tunnel-host 上, 由 tunnel-host 向 target-host 的 target-port 发起请求,获取数据后返回本机。
# 可以理解 tunnel-host 是个代理
ssh -L -N -f local-port:target-host:target-port tunnel-host有三个配置参数:
-L:转发本地端口。-N:不发送任何命令,只用来建立连接。没有这个参数,会在 SSH 服务器打开一个 Shell。-f:将 SSH 连接放到后台。没有这个参数,暂时不用 SSH 连接时,终端会失去响应。
如果经常使用本地转发,可以将设置写入 SSH 客户端的用户个人配置文件(~/.ssh/config)。
Host test.example.com
LocalForward client-IP:client-port server-IP:server-port远程转发
远程转发指的是在远程 SSH 服务器建立的转发规则。
它跟本地转发正好反过来。建立本地计算机到远程 SSH 服务器的隧道以后,本地转发是通过本地计算机访问远程 SSH 服务器,而远程转发则是通过远程 SSH 服务器访问本地计算机(访问远程SSH服务器端口, 请求会转发到本地的端口上)。
$ ssh -R remote-port:target-host:target-port -N remotehost上面命令中,-R参数表示远程端口转发,remote-port是远程 SSH 服务器的端口,target-host和target-port是目标服务器及其端口,remotehost是远程 SSH 服务器。
如果经常执行远程端口转发,可以将设置写入 SSH 客户端的用户个人配置文件(~/.ssh/config)。
Host remote-forward
HostName test.example.com
RemoteForward remote-port target-host:target-port完成上面的设置后,执行下面的命令就会建立远程转发。
$ ssh -N remote-forward
# 等同于
$ ssh -R remote-port:target-host:target-port -N test.example.com动态转发
动态转发指的是,本机与 SSH 服务器之间创建了一个加密连接,然后本机内部针对某个端口的通信,都通过这个加密连接转发。它的一个使用场景就是,访问所有外部网站,都通过 SSH 转发。
动态转发需要把本地端口绑定到 SSH 服务器。至于 SSH 服务器要去访问哪一个网站,完全是动态的,取决于原始通信,所以叫做动态转发。
$ ssh -D local-port tunnel-host -N上面命令中,-D表示动态转发,local-port是本地端口,tunnel-host是 SSH 服务器,-N表示这个 SSH 连接只进行端口转发,不登录远程 Shell,不能执行远程命令,只能充当隧道。
举例来说,如果本地端口是2121,那么动态转发的命令就是下面这样。
$ ssh -D 2121 tunnel-host -N注意,这种转发采用了 SOCKS5 协议。访问外部网站时,需要把 HTTP 请求转成 SOCKS5 协议,才能把本地端口的请求转发出去。
下面是 SSH 隧道建立后的一个使用实例。
$ curl -x socks5://localhost:2121 http://www.example.com上面命令中,curl 的-x参数指定代理服务器,即通过 SOCKS5 协议的本地2121端口,访问http://www.example.com。
如果经常使用动态转发,可以将设置写入 SSH 客户端的用户个人配置文件(~/.ssh/config)。
DynamicForward tunnel-host:local-portSimple Operation
- Connect to a remote server:
ssh username@remote_host
- Connect to a remote server with a specific identity (private key):
ssh -i path/to/key_file username@remote_host
- Connect to a remote server using a specific [p]ort:
ssh username@remote_host -p 2222
- Run a command on a remote server with a [t]ty allocation allowing interaction with the remote command:
ssh username@remote_host -t command command_arguments
- SSH tunneling: [D]ynamic port forwarding (SOCKS proxy on localhost:1080):
ssh -D 1080 username@remote_host
- SSH tunneling: Forward a specific port (localhost:9999 to example.org:80) along with disabling pseudo-[T]ty alloc
ation and executio[N] of remote commands:
ssh -L 9999:example.org:80 -N -T username@remote_host
- SSH [J]umping: Connect through a jumphost to a remote server (Multiple jump hops may be specified separated by co
mma characters):
ssh -J username@jump_host username@remote_host
- Close a hanged session:
<Enter> ~ .FAQ
提示错误内容为: no matching host key type found. Their offer: ssh-rsa
两种解决办法:
- 临时 ssh 增加参数 -o HostKeyAlgorithms=+ssh-rsa
- 编辑文件
~/.ssh/config, 增加以下内容:
HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedKeyTypes +ssh-rsa
# StrictHostKeyChecking和CheckHostIP参数也可以加入这个文件scp命令提示:/usr/libexec/sftp-server: not found,但存在sftp-server
可能是服务不兼容问题,尝试使用-O参数,让scp以传统模式(SSH Channel)来传输文件。