SSH 远程管理服务
1.ssh简介
ssh为Secure Shell 的缩写。SSH 为建立在应用层和传输层基础上的安全协议
端口号 :22
linux 中守护进程 :sshd
安全服务:OpenSSH
服务端主程序 :/usr/sbin/sshd
客户端主程序 :/usr/bin/ssh
服务端配置文件 : /etc/ssh/sshd_config
客户端配置文件 :/etc/ssh/ssh_config
ssh 服务是linux 默认安装
2.SSH 加密原理
对称加密 :用同一个密码可以 加密和解密
非对称加密:需要用两个密钥进行加密和解密 称为公钥和私钥。
ssh采用 非对称加密
ssh加密保护文件非安全 不被窃取
3.SSH的配置文件
Port 22 端口
Listenaddress 监听的ip
Protol 2 ssh版本
hostKey 私钥保存位置
ServerKeyBits 1024 私钥的位数
4.SSH 命令
ssh root@要管理的linux主机ip
exit 退出
[root@localhost ~]# ssh root@192.168.144.14
root@192.168.144.14's password:
Last login: Mon Sep 24 21:57:47 2018 from 192.168.144.11
scp 远程复制
下载
scp root@192.168.1.114:/root/text.txt .
下载其他主机文件 到当前目录
[root@localhost ~]# scp root@192.168.144.14:/tmp/1 .
root@192.168.144.14's password:
1 100% 0 0.0KB/s 00:00
上传
scp -r /root/123/ root@192.168.44.2/root
将本机tmp目录 上传至别的主机tmp下
[root@localhost ~]# scp -r /tmp/ root@192.168.144.14:/tmp
root@192.168.144.14's password:
输入密码即可
ssh 的密钥和登录 就是不用密码验证
简称免密钥登录
步骤为将客户端的公钥导入服务器端的指定文件中,则客户端可以使用私钥接入和管理服务器端
本次实验 客户端ip 为 192.168.125.129
服务端 ip 为 192.168.125.128
步骤一 :
1.在本地主机生成 “密钥对” 并将公钥传送到远程主机中
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 回车设置密钥的存储路径
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:
fc:1d:b0:94:b7:4c:81:c2:b6:5d:7e:8c:13:b9:17:b4 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| . ..o. |
| + ..+... |
| . ++oo=E. |
| ....*=.+ |
| S . ++ |
| . . . |
| . . |
| |
| |
+-----------------+
将生成好的公钥密钥传送到远程主机
[root@localhost ~]# ssh-copy-id 192.168.125.128
The authenticity of host '192.168.125.128 (192.168.125.128)' can't be established.
ECDSA key fingerprint is 9e:3a:84:61:9f:5c:a7:95:1b:69:27:5b:51:ca:6f:21.
Are you sure you want to continue connecting (yes/no)? 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
root@192.168.125.128's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.125.128'"
and check to make sure that only the key(s) you wanted were added.
第二步 :在远程主机中修改sshd服务的配置文件 (修改后要重启服务)
1.编辑ssh服务程序主配置文件
注意 : 是在远程主机端修改ssh主配置文件
[root@localhost ~]# vi /etc/ssh/sshd_config
将允许密码验证的参数设置为no
3.将允许密钥验证的参数设置为yes
4.保存退出配置文件
5.重启ssh服务程序即可生效
[root@localhost ~]# ssh 192.168.125.128
Last login: Sun Oct 7 23:16:39 2018 from 192.168.125.129
* * * *
* * * *
****** ***** ******** *
* * * * *** * * * *
* * ***** ******** *
* * * * * *
* * *** ******** *
成功了 哈哈哈哈!!