目录
概念:安全外壳协议,提供安全可靠的远程连接
特点:1.ssh是工作在传输层和应用层的协议
2.ssh提供了一组管理命令:ssh、scp、sftp、ssh-copy-id
3.提供了多种身份验证机制
身份验证机制1.密码验证:需要提供密码
2.密钥对验证:无需提供密码,直接登录
验证过程
1.客户端发起请求
2.确认是否保存指纹信息,yes确认保存
3.输入目标主机密码
4.打开子shell建立会话
5.若退出,则使用exit
[root@localhost ~]# ssh root@192.168.10.1 #用当前root远程连接192.168.10.1
The authenticity of host '192.168.10.1 (192.168.10.1)' can't be established.
ECDSA key fingerprint is SHA256:0UJXKtuSoyNbmMYQcEVKZvxb6HXdWaaeTH7t+wSZMRg.
ECDSA key fingerprint is MD5:05:56:5a:7f:aa:02:24:12:b7:d3:3e:b8:f5:a6:2a:d3.
Are you sure you want to continue connecting (yes/no)? yes #输入yes
Warning: Permanently added '192.168.10.1' (ECDSA) to the list of known hosts.
root@192.168.10.1's password: #输入192.168.10.1密码
Last login: Wed May 17 21:39:54 2023
[root@localhost ~]#
[root@localhost ~]# exit
登出
Connection to 192.168.10.1 closed.
[root@localhost ~]#
加密机制
1.单向加密:不可逆的加密算法
2.对称加密:加密和解密使用相同的密钥
3.非对称加密:加密和解密使用一组密钥对(公钥、私钥)
基本参数
vim /etc/ssh/sshd_config #ssh主程序sshd的配置文件
vim /etc/ssh/ssh_host_* #服务器的公钥和私钥文件
vim .ssh/authorized_keys #密钥库文件
vim /etc/ssh/ssh.config #客户端的全局配置文件
vim .ssh/known_hosts #客户端存储主机指纹的文件
vim .ssh.id_rsa #客户端生成的私钥
vim .ssh.id_rsa.pub #客户端生成的公钥
配置文件解析
vim /etc/ssh/sshd_config #进入配置文件
#Port 22 #代表端口22
#AddressFamily any
#ListenAddress 0.0.0.0 #监听地址,0.0.0.0表示监听所有地址
#ListenAddress ::
#PasswordAuthentication yes #是否启用密钥对验证
#PermitEmptyPasswords no #是否启用空密码,密码验证时,最好关闭
PasswordAuthentication yes #是否启用密码验证
vim /etc/ssh/ssh_config #进入配置文件
ConnectTimeout 0 #连接超时时间
基本操作
ssh 格式:ssh [options] [user@]hostname [command]
[root@localhost ~]# ssh 192.168.9.1 #远程连接192.168.9.1
root@192.168.9.1's password: #输入192.168.9.1 root用户的密码
Last login: Wed May 17 21:40:04 2023 #成功登录
[root@localhost ~]# exit
登出
Connection to 192.168.9.1 closed.
[root@localhost ~]# ssh 192.168.9.1 ls -l #在192.168.9.1使用ls -l命令
root@192.168.9.1's password: #输入密码
总用量 8 #成功使用
-rw-------. 1 root root 1683 4月 24 18:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 1731 4月 24 18:21 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 4月 24 18:21 公共
drwxr-xr-x. 2 root root 6 4月 24 18:21 模板
drwxr-xr-x. 2 root root 6 4月 24 18:21 视频
drwxr-xr-x. 2 root root 6 4月 24 18:21 图片
drwxr-xr-x. 2 root root 6 4月 24 18:21 文档
drwxr-xr-x. 2 root root 6 4月 24 18:21 下载
drwxr-xr-x. 2 root root 6 4月 24 18:21 音乐
drwxr-xr-x. 2 root root 6 4月 24 18:21 桌面
[root@localhost ~]#
[root@localhost ~]# scp 192.168.9.1:123.txt . #将192.168.9.1的123.txt复制到本地
root@192.168.9.1's password:
123.txt 100% 0 0.0KB/s
[root@localhost ~]# scp 123.txt 192.168.10.1:/root #将本地123.txt复制到192.168.10.1
root@192.168.10.1's password:
123.txt 100% 0 0.0KB/s 00:00
[root@localhost ~]# ssh 192.168.10.1 ls -l #这样就将192.168.9.1的文件复制到192.168.10.1
root@192.168.10.1's password:
总用量 8
-rw-r--r--. 1 root root 0 5月 18 02:12 123.txt #在192.168.10.1得到了123.txt文件
-rw-------. 1 root root 1695 4月 24 23:29 anaconda-ks.cfg
-rw-r--r--. 1 root root 1743 4月 24 23:30 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 4月 24 23:32 公共
drwxr-xr-x. 2 root root 6 4月 24 23:32 模板
drwxr-xr-x. 2 root root 6 4月 24 23:32 视频
drwxr-xr-x. 2 root root 6 4月 24 23:32 图片
drwxr-xr-x. 2 root root 6 4月 24 23:32 文档
drwxr-xr-x. 2 root root 6 4月 24 23:32 下载
drwxr-xr-x. 2 root root 6 4月 24 23:32 音乐
drwxr-xr-x. 2 root root 6 4月 24 23:32 桌面
[root@localhost ~]#
sftp 作用:安全的文件传输程序
[root@localhost ~]# sftp 192.168.9.1 #安全进入192.168.9.1
root@192.168.9.1's password: #输入密码
Connected to 192.168.9.1.
sftp>
sftp>
sftp> get 123.txt 获取/root/123.txt到123.txt
Fetching /root/123.txt to 123.txt
sftp> lls #查看192.168.9.1当前目录(多的 l 表示 local本地)
123.txt anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
sftp>
sftp> get initial-setup-ks.cfg #获取initial-setup-ks.cfg到本地
Fetching /root/initial-setup-ks.cfg to initial-setup-ks.cfg
/root/initial-setup-ks.cfg 0% 0 0.0KB/s --:-- ETA
/root/initial-setup-ks.cfg 100% 1731 785.7KB/s 00:00
sftp>
sftp> exit
[root@localhost ~]# ll
总用量 8
-rw-r--r--. 1 root root 0 5月 17 18:19 123.txt
-rw-------. 1 root root 1685 4月 24 17:21 anaconda-ks.cfg
-rw-r--r--. 1 root root 1731 5月 17 18:23 initial-setup-ks.cfg #已获取
drwxr-xr-x. 2 root root 6 4月 24 17:29 公共
密钥对验证 作用:提供免交互的密码验证
客户端生成密钥对 ssh-keygen
客户端将公钥上传至服务器 ssh-copy-id
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair. #生成公钥/私钥对
Enter file in which to save the key (/root/.ssh/id_rsa): #输入要保存密钥的文件,直接回车)
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y #(是否覆盖,y)
Enter passphrase (empty for no passphrase): #输入密码端户 空表示无密码短语
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is: #关键指纹是:
SHA256:ZDtsnPn08/1d+N/KaiEJM7EXGXGUfw1QVVcexXCD/Xg root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| o=++=*@|
| . o....+=|
| oo . . o+|
| ==+. o E|
| S=.. o |
| . +o.. . |
| ..o.. .|
| .+ o+|
| ...+.X|
+----[SHA256]-----+
[root@localhost ~]# cd .ssh/ #切换到.ssh/ (隐藏文件ssh/)
[root@localhost .ssh]# ssh-copy-id 192.168.9.1 #客户端将公钥上传至服务器
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.9.1 (192.168.9.1)' can't be established.
ECDSA key fingerprint is SHA256:hWIyTVCuMJynuK1eaGALpj1jXfdIuWRahVwhJ2Rouwo.
ECDSA key fingerprint is MD5:c1:fe:29:ea:ec:81:ab:25:55:a3:8a:3c:45:65:e0:2e.
Are you sure you want to continue connecting (yes/no)? yes #输入yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys #安装了1个新钥匙
root@192.168.9.1's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.9.1'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost .ssh]# ssh 192.168.9.1 #这时候登录发现不需要输入密码
Last login: Thu May 18 02:34:25 2023 from 192.168.9.254
[root@localhost ~]#