ssh免密登录
# rsa(默认)和 dsa 是服务器连接的时候,必须要使用的协议
没有远程登录(ssh协议连接)过的用户**家目录**下没有.ssh目录
远程ssh协议连接过 家目录下才有.ssh目录
vim粘贴之前要进入编辑模式
虚拟机外网网卡连接,xshell才能连接
杀掉sshd进程或者关闭sshd进程 可导致xshell连不上,需要重启虚拟机才能连接(去机房?)
web01服务器是可以ping外网IP和内网IP的
如果web01服务器断开内网网卡,内网虚拟机断开外网网卡的连接
那么web01 就不能ping内网IP了,因为内网和外网是不能直接连接的,除非web01打开内网网卡,并且和内网虚拟机处于同一个lan区段,内网虚拟机才能间接的连接外网
公钥可以发给自己,免密连接自己,和bash'相似'
免密连接和用户身份无关,系统所有用户都可以使用免密的方式进行 远程连接
DNS,把域名解析成IP
DNS反向解析,把IP解析成域名
[root@web01 ~]# ssh-keygen #默认使用rsa协议生成密匙
[root@web01 ~]# ssh-keygen -t dsa # -t 指定使用dsa 协议生成密匙
1.#创建密钥对
[root@web01 ~]# 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:
SHA256:RbzrPnQlDvH5EclGBCiTNc6IyYAVh4P97wIaVzNMUU4 root@web01
The key`s randomart image is:
+---[RSA 2048]----+ #创建密匙使用的协议
| =+oooE++..=o. |
| o ++.=+=+. = |
| =+ oo++ o . |
| * .o + o |
| . +S + + . |
| . o . o o . |
| + . . o . |
| . . . o |
| . ... |
+----[SHA256]-----+ #加密算法
[root@web01 ~]# cd .ssh/
[root@web01 ~/.ssh]# ll
total 12
-rw------- 1 root root 1675 May 11 19:15 id_rsa #私钥,相当于钥匙
-rw-r--r-- 1 root root 392 May 11 19:15 id_rsa.pub #公钥,相当于锁
-rw-r--r-- 1 root root 170 May 10 10:09 known_hosts #免yes,认证信息
# known_hosts 是使用过ssh之后自动生成的文件,里面记录了ssh连接后 记录免yes 的信息 (有了这个文件,下次再使用ssh的时候不用输入yes),
花园报错:出现一堆@@@@@免密认证失败,1.修改known_hosts文件中对应的 连接记录,2.删除known_hosts (直接该用户 vim ~/.ssh/known_hosts +N 再dd )
2.#发送公钥(需要输入yes和密码,因为使用了ssh远程连接协议),可以发到root用户,也可以发到普通用户
#查看ssh-copy-id 命令 属于哪个包
[root@web01 ~]# yum provides ssh-copy-id
openssh-clients-7.4p1-21.el7.x86_64
[root@web02 ~]# rpm -q openssh-clients #linux系统默认安装过了
openssh-clients-7.4p1-21.el7.x86_64
-i:指定公钥文件
-P:指定端口(注意位置)
-f: 强制安装
ssh-copy-id -i /root/.ssh/id_rsa.pub -o stricthostkeychecking=no #不需要输入yes了
把本地的ssh公钥文件 安装 到远程主机对应的家目录下,并且授权为600且改名(#不是单纯的拷贝)
默认拷贝到 指定主机,指定用户的家目录下
[root@web01 ~/.ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.16.1.41
#连接
[root@web01 ~/.ssh]# ssh 'root@172.16.1.41' #不需要输入密码,会有默认输出
#10.0.0.7查看公钥内容
[root@web01 ~/.ssh]# cat id_rsa.pub
#10.0.0.41,查看公钥内容,可以发现公钥内容是一样的
[root@backup ~]# cd .ssh/
[root@backup ~/.ssh]# ll
total 8
-rw------- 1 root root 392 May 11 19:21 authorized_keys # 600权限,该文件可以存放多个公钥,该文件有10个公钥,说明有10个主机可以免密连接该主机
-rw-r--r-- 1 root root 171 May 8 22:26 known_hosts #
[root@backup ~/.ssh]# cat authorized_keys
2.#自己做公钥(不需要知道root密码)(小心串行让人头疼)
1.查看生成的公钥并复制
[root@web01 ~]# cat /root/.ssh/id_rsa.pub
2.
#创建.ssh目录,没用使用过ssh的用户 家目录下没有.ssh目录
[root@web02 ~]# mkdir -p /root/.ssh/
#粘贴公匙
[root@web02 ~]# vim /root/.ssh/authorized_keys
#文件授权,仿造文件权限(最好是600)
chmod 600 /root/.ssh/authorized_keys
#目录授权,仿造目录权限
chmod 700 /root/.ssh
3.连接
[root@web01 ~]# ssh 10.0.0.8
分发密匙脚本
pass='1'
ip='172.16.1.'
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
for i in 5 6 7 8 9 31 41 51 52 53 54 71 ;
do
sshpass -p $pass ssh-copy-id -i /root/.ssh/id_rsa.pub -o stricthostkeychecking=no root@${ip}${i}
done
ssh优化,后面是粘贴板
[root@m01 ~]# vim /etc/ssh/sshd_config
## 在企业中需要优化哪些项
[root@web01 ~]# ll /etc/ssh/
total 604
-rw-r--r--. 1 root root 581843 Aug 9 2019 moduli
-rw-r--r--. 1 root root 2276 Aug 9 2019 ssh_config
-rw-------. 1 root root 3907 Aug 9 2019 sshd_config #ssh主配置文件
-rw-r-----. 1 root ssh_keys 227 May 8 04:08 ssh_host_ecdsa_key #加密有关
-rw-r--r--. 1 root root 162 May 8 04:08 ssh_host_ecdsa_key.pub
-rw-r-----. 1 root ssh_keys 387 May 8 04:08 ssh_host_ed25519_key
-rw-r--r--. 1 root root 82 May 8 04:08 ssh_host_ed25519_key.pub#加密有关
-rw-r-----. 1 root ssh_keys 1679 May 8 04:08 ssh_host_rsa_key #生成密匙对有关
-rw-r--r--. 1 root root 382 May 8 04:08 ssh_host_rsa_key.pub #生成密匙对有关
---- 安全方面 ----
# 1.修改端口
Port 52020 (1-65535,不能占用其它服务的常用端口)
# 2.禁止root登录
PermitRootLogin no
# 3.禁止密码登录
PasswordAuthentication no
---- 性能方面 ----
# 4.不使用DNS反向解析
UseDNS no
# 5.不使用GSS认证
GSSAPIAuthentication no
## 重启服务
[root@lb01 ~]# systemctl restart sshd
#ssh 配置文件修改
1.修改端口(要取消注释),企业都要修改ssh端口,增加安全性,修改范围是1-65535,不能占用常用的端口(1111,8888,52020),修改之后要使用 -p远程连接
[root@web01 ~]# vim /etc/ssh/sshd_config +17
Port 52022
2.# 禁止root用户直接远程登录(无论密码连接还是公钥认证)
[root@web01 ~]# vim /etc/ssh/sshd_config +37
PermitRootLogin no
3.禁止root用户使用密码直接远程登录
[root@web01 ~]# vim /etc/ssh/sshd_config +65
PasswordAuthentication no
4.ssh进行dns反向解析,影响ssh连接效率参数
[root@web01 ~]# vim /etc/ssh/sshd_config +115
UseDNS no
5.禁止GSS认证,减少连接时产生的延迟
[root@web01 ~]# vim /etc/ssh/sshd_config +79
GSSAPIAuthentication no
修改ssh-copy-id命令 指定安装的公钥 位置,想放哪就放哪(需要知道密码)
[root@web01 ~]# vim /etc/ssh/sshd_config +47
AuthorizedKeysFile .ssh/authorized_keys
DNS:
作用就是把域名解析成IP,/etc/hosts,/etc/resolv.conf(递归查询),根域名解析服务器(迭代查询)
DNS反向解析:
作用就是把IP解析成域名
mysql有些情况下会自动把 IP解析成域名,我们需要手动在/etc/hosts配置相应的域名解析,整个过程叫做DNS的反向解析
ssh登录总结
1.网络,ssh root@10.0.0.31
当一台主机开启外网网卡和内网网卡,那么,既可以使用外网网段连接,又可以使用内网网段进行连接
当一台主机只开启外网网卡,那么,只能使用外网网段进行连接
当一台主机只开启内网网卡,那么,只能使用内网网段进行连接
2.秘钥
id_rsa #私钥
known_hosts #公钥
id_rsa.pub #该主机使用ssh远程登录的认证信息,可以查看不需要输入yes进行连接的主机IP
authorized_keys #ssh配置文件中指定的公钥存放文件,可以查看有哪些公钥,能被哪些主机免密连接
3.sshd进程
该进程是服务器远程连接ssh服务的守护进程,关闭或者断开或者kill都会导致服务器不能远程连接,这样只能去机房开启sshd进
程了
实现免密免输入yes连接的脚本
推送过后,第二次可以直接连接(不用输入yes和密码)
#推送过后,无论使用172.16.1.网段还是10.0.0.网段, 跳板机第二次都可以直接连接
#!/bin/bash
pass='1'
ip='172.16.1.'
ip2='10.0.0.'
rpm -q sshpass &>/dev/null
[ echo $? -ne 0 ];yum install -y sshpass &>/dev/null
[ ! -f /root/.ssh/id_rsa.pub ];\
ssh-keygen -t rsa -P "" -f /root/.ssh/id_rsa
rm -rf /root/.ssh/known_hosts
for i in 5 6 7 8 9 31 41 51 52 53 54 61 200;
do
sshpass -p $pass ssh-copy-id -i /root/.ssh/id_rsa.pub -o stricthostkeychecking=no root@${ip}${i}
sshpass -p $pass ssh-copy-id -i /root/.ssh/id_rsa.pub -o stricthostkeychecking=no root@${ip2}${i}
done
chmod 600 /root/mjh.sh
read -n1 -sp '想不想让我表演点什么?(y或n) ' num
if [ $num == y ];then
echo ------------------------------磁盘信息---------------------
df -h
echo ------------------------------内存信息---------------------
free -h
echo ------------------------------ssh 进程信息---------------------
ps -ef|grep [s]sh
else
echo 扎心了,烙铁
fi