Linux常用命令-ssh

命令

ssh

描述

OpenSSH SSH client (remote login program)
远程登录工具

用法

1
2
3
4
5
6
7
ssh [-1246AaCfGgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-E log_file] [-e escape_char]
[-F configfile] [-I pkcs11] [-i identity_file]
[-J [user@]host[:port]] [-L address] [-l login_name] [-m mac_spec]
[-O ctl_cmd] [-o option] [-p port] [-Q query_option] [-R address]
[-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Options:
-1 使用ssh v1版本,v1版本已不再受支持
-2 使用v2版本,默认
-4 使用IPv4地址
-6 使用IPv6地址
-b 使用指定地址连接,适用于有多个IP的情况
-C 启用数据压缩
-E 保存debug日志到指定文件
-f 在命令执行之前请求ssh转到后台
-F 指定用户配置文件,默认为~/.ssh/config
-i 指定私钥文件
-L 配置本地端口转发到远程端口,格式为[bind_address:]port:host:hostport
-l 指定用户名
-N 不执行远程命令
-o 指定ssh选项,用于临时覆盖配置文件中的选项,详情参考man ssh_config
-p 指定端口
-Q 查询模式,支持的参数有cipher,cipher-auth,mac,kex,key,key-cert,key-plain,protocol-version
-q 静默模式登录
-R 配置远程端口转发到本地端口,格式为[bind_address:]port:host:hostport
-t 强制伪终端分配,可用于多次跳转登录
-v 显示详情,可以重复使用,如-vvv

注意

ssh端口转发参考https://www.zsythink.net/archives/2450
创建端口转发时注意普通用户权限只能开通1024以上端口,如需开通80或443需要root权限

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$ ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017

# -i 指定私钥文件
$ ssh -i .ssh/key root@192.168.31.10
# -p 指定端口
$ ssh -p 2222 root@192.168.31.10
# 查询模式,查询支持的密码加密算法和MAC算法(message authentication code)
$ ssh -Q cipher
3des-cbc
blowfish-cbc
cast128-cbc
arcfour
arcfour128
arcfour256
aes128-cbc
aes192-cbc
aes256-cbc
rijndael-cbc@lysator.liu.se
aes128-ctr
aes192-ctr
aes256-ctr
aes128-gcm@openssh.com
aes256-gcm@openssh.com
chacha20-poly1305@openssh.com
# ssh -Q mac

# 不加载任何ssh_config配置,全局配置/etc/ssh/ssh_config,用户配置~/.ssh/config
$ ssh -F /dev/null root@192.168.31.10
# 或
$ ssh -F none root@192.168.31.10

# 解决首次ssh登录需要输入yes,ssh命令增加如下选项,临时
$ ssh -o stricthostkeychecking=no root@192.168.31.10
# 或者更改本地ssh_config配置,增加如下配置,永久
$ vi /etc/ssh/ssh_config
StrictHostKeyChecking no
...

# 不登录远程ssh仅执行远程命令,类似于以指定用户执行命令su www -c "ls /home/www"
$ ssh root@192.168.31.10 'hostname'
# -t 跳转登录,将server1作为跳板机来远程连接到server2,也可以指定不同的用户名,注意提前做好相互之间的密钥认证
$ ssh -t user1@server1 ssh user2@server2
# 先登录到主机20,然后通过20登录到主机10,实现跳转登录.登录后可以通过w命令查看源地址是20
$ ssh -t root@192.168.31.20 ssh root@192.168.31.10
$ w

# -L或-R选项主要是执行的对象不同,-L在本地客户端执行,-R在远程服务器执行,
# 但是达到的效果都是一样的,即通过ssh隧道在本地客户端创建监听端口,实现访问本地监听端口即可访问远程端口
# 配置本地端口转发到远程,格式如下,如果省略localhost则默认绑定到本地127.0.0.1
ssh -fNL localhost:localport:remotehost:remoteport user@remotehost
# 如下访问本地80端口转发到远程的80端口,注意本地端口不能重复,远程端口必须可以访问
# 在主机10上执行以下命令,主机10相当于localhost,主机20相当于remotehost,端口绑定在主机10上,可通过kill或killall命令结束ssh进程,隧道即中止
ssh -fNL 80:192.168.31.20:80 root@192.168.31.20
ss -tunl
curl -I 192.168.31.20
curl -I 127.0.0.1
ps aux | grep ssh
killall ssh
# 或者指定localhost-IP地址,则其他主机可以通过本机IP地址访问
ssh -fNL 192.168.31.10:80:192.168.31.20:80 root@192.168.31.20
curl -I 192.168.31.10

# 另外也可以通过proxyhost建立通道实现代理转发,例如本地想要访问后端服务器,但是后端服务器禁止直接访问,则可以通过代理服务器来转发请求,格式如下
ssh -fNL localhost:localport:remotehost:remoteport user@proxyhost
# 如下本地通过与公网代理服务器建立ssh通道,然后转发请求到后端内网服务器
ssh -fNL 80:172.16.10.10:80 root@123.12.12.12
# 也可以通过代理服务器访问google,需要绑定域名到hosts
ssh -fNL 443:www.google.com:443 root@123.12.12.12
echo "127.0.0.1 www.google.com" >> /etc/hosts
curl -I https://www.google.com

# 配置远程端口转发到本地,格式如下,注意-R选项只能绑定到127.0.0.1,所以可以省略remotehost
ssh -fNR remotehost:remoteport:localhost:localport user@remotehost
# 注意普通用户权限只能开通1024以上端口
# 在主机20上执行以下命令,主机20相当于localhost,主机10相当于remotehost,可通过kill或killall命令结束ssh进程,隧道即中止
ssh -fNR 192.168.31.10:8080:192.168.31.20:80 www@192.168.31.10
ps aux | grep ssh
killall ssh
# 在主机10上执行以下命令,端口依然绑定在主机10上,但是只能主机10本地访问
ss -tunl
curl -I 127.0.0.1:80