00. 目录
01. 命令概述
ssh命令是openssh套件中的客户端连接工具,可以给予ssh加密协议实现安全的远程登录服务器,实现对服务器的远程管理。
02. 命令格式
格式:ssh [选项] [参数]
03. 常用选项
-1 强制使用ssh协议版本1
-2 强制使用ssh协议版本2
-4 强制使用IPv4地址
-6 强制使用IPv6地址
-A 开启认证代理连接转发功能
-a 关闭认证代理连接转发功能
-b<IP地址> 使用本机指定的地址作为对位连接的源IP地址
-C 请求压缩所有数据
-F<配置文件> 指定ssh指令的配置文件,默认的配置文件为“/etc/ssh/ssh_config”
-f 后台执行ssh指令
-g 允许远程主机连接本机的转发端口
-i<身份文件> 指定身份文件(即私钥文件)
-l<登录名> 指定连接远程服务器的登录用户名
-N 不执行远程指令
-o<选项> 指定配置选项
-p<端口> 指定远程服务器上的端口
-q 静默模式,所有的警告和诊断信息被禁止输出
-X 开启X11转发功能
-x 关闭X11转发功能
-y 开启信任X11转发功能
04. 参考示例
4.1 登录远程服务器
[deng@localhost ~]$ ssh 172.16.0.76
The authenticity of host '172.16.0.76 (172.16.0.76)' can't be established.
ECDSA key fingerprint is SHA256:YheY5RH3PZ27UMYxdPwhraYfS4bCK/p3+xwdWL0O9WQ.
ECDSA key fingerprint is MD5:27:a1:b7:c7:df:7d:91:04:40:bd:e9:79:bb:98:8f:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.0.76' (ECDSA) to the list of known hosts.
deng@172.16.0.76's password:
Last login: Thu Aug 22 22:06:22 2019 from 172.16.0.51
[deng@localhost ~]$
4.2 指定用户登录远程服务器
[deng@localhost ~]$ ssh -l deng 172.16.0.76
deng@172.16.0.76's password:
Last login: Thu Aug 22 22:06:33 2019 from 172.16.0.76
[deng@localhost ~]$
4.3 查看远程服务器分区列表
[deng@localhost ~]$ ssh -l root 172.16.0.76 /sbin/fdisk -l
root@172.16.0.76's password:
磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000cc727
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 8800255 4194304 82 Linux swap / Solaris
/dev/sda3 8800256 209715199 100457472 83 Linux
[deng@localhost ~]$
4.4 强制使用ssh协议版本2
[deng@localhost ~]$ ssh -2 -l root 172.16.0.76
root@172.16.0.76's password:
Last login: Thu Aug 22 21:42:57 2019
[root@localhost ~]#
4.5 开启认证代理连接转发功能
[root@localhost ~]# ssh -A -l root 172.16.0.76
The authenticity of host '172.16.0.76 (172.16.0.76)' can't be established.
ECDSA key fingerprint is SHA256:YheY5RH3PZ27UMYxdPwhraYfS4bCK/p3+xwdWL0O9WQ.
ECDSA key fingerprint is MD5:27:a1:b7:c7:df:7d:91:04:40:bd:e9:79:bb:98:8f:e4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.0.76' (ECDSA) to the list of known hosts.
root@172.16.0.76's password:
Last login: Thu Aug 22 22:10:56 2019 from 172.16.0.76
[root@localhost ~]#
4.6 关闭认证代理连接转发功能
[root@localhost ~]# ssh -a -l root 172.16.0.76
root@172.16.0.76's password:
Last login: Thu Aug 22 22:11:31 2019 from 172.16.0.76
[root@localhost ~]#
4.7 指定用户名登录(二)
[root@localhost ~]# ssh root@172.16.0.76
root@172.16.0.76's password:
Last login: Thu Aug 22 22:12:26 2019 from 172.16.0.76
[root@localhost ~]#
4.8 指定端口登录远程服务器
[root@localhost ~]# ssh -p 22 root@172.16.0.76
root@172.16.0.76's password:
Last login: Thu Aug 22 22:13:31 2019 from 172.16.0.76
[root@localhost ~]#
4.9 通过代理登录
[root@localhost ~]# ssh -D 7576 root@172.16.0.76
root@172.16.0.76's password:
Last failed login: Thu Aug 22 22:15:00 CST 2019 from 172.16.0.76 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Thu Aug 22 22:14:03 2019 from 172.16.0.76
[root@localhost ~]#
4.10 执行远程指令
[root@localhost ~]# ssh deng@172.16.0.76 date
deng@172.16.0.76's password:
2019年 08月 22日 星期四 22:15:56 CST
[root@localhost ~]#
4.11 在远程主机运行一个图形界面的程序
[root@localhost ~]# ssh -X deng@172.16.0.76
deng@172.16.0.76's password:
4.12 绑定源地址
[root@localhost ~]# ssh -b 172.16.0.76 deng@172.16.0.76
deng@172.16.0.76's password:
Last login: Thu Aug 22 22:20:55 2019 from 172.16.0.76
[deng@localhost ~]$
4.13 对所有数据请求压缩
[deng@localhost ~]$ ssh -C deng@172.16.0.76
deng@172.16.0.76's password:
Last login: Thu Aug 22 22:22:00 2019 from 172.16.0.76
[deng@localhost ~]$ ls
4.14 打开调试模式
[deng@localhost ~]$ ssh -v deng@172.16.0.76
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to 172.16.0.76 [172.16.0.76] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/deng/.ssh/id_rsa type -1
4.15 静默模式,所有的警告和诊断信息被禁止输出
[deng@localhost ~]$ ssh -q deng@172.16.0.76
deng@172.16.0.76's password:
Last failed login: Thu Aug 22 22:23:52 CST 2019 from 172.16.0.76 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Thu Aug 22 22:22:34 2019 from 172.16.0.76
[deng@localhost ~]$