1.创建私有CA并进行证书申请。
创建私有CA
1)创建CA相关目录和文件
[root@centos8 ~]#mkdir -pv /etc/pki/CA/{certs,crl,newcerts,private}
[root@centos8 ~]#tree /etc/pki/CA/
#index.txt和serial文件在颁发证书时需要使用,如果不存在,会出现错误提示
[root@centos8 ~]#touch /etc/pki/CA/index.txt
[root@centos8 ~]#echo 01 > /etc/pki/CA/serial #默认开始序列号,16进制.
2) 创建CA的私钥
[root@centos8 ~]#cd /etc/pki/CA/
[root@centos8 CA]#(umask 066; openssl genrsa -out private/cakey.pem 2048)
3) 给CA颁发自签名证书
[root@centos8 ~]#openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.pem
#在match模式下,国家,省,公司名称三项必须和CA一致
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:
#查看证书信息
[root@centos8 ~]#cat /etc/pki/CA/cacert.pem
[root@centos8 ~]#openssl x509 -in /etc/pki/CA/cacert.pem -noout -text
[root@centos8 ~]#sz /etc/pki/CA/cacert.pem
#将文件cacert.pem传到windows上,修改文件名为cacert.pem.crt,双击可以看到下面显示
用户申请证书
4)用户生成私钥和证书申请
4.1)生成私钥
[root@centos8 ~]#mkdir /data/app1
[root@centos8 ~]#(umask 066; openssl genrsa -out /data/app1/app1.key 2048)
4.2)生成证书申请文件
[root@centos8 ~]#openssl req -new -key /data/app1/app1.key -out /data/app1/app1.csr
[root@centos8 ~]#ll /data/app1/
默认有三项内容必须和CA一致:国家,省份,组织,如果不同,会出现错误提示
5) CA颁发证书
[root@centos8 ~]#openssl ca -in /data/app1/app1.csr -out /etc/pki/CA/certs/app1.crt -days 1000
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
[root@centos8 ~]#tree /etc/pki/CA
6)查看证书信息
[root@centos8 ~]#cat /etc/pki/CA/certs/app1.crt
7)将证书相关文件发送到用户端使用
[root@centos8 ~]#cp /etc/pki/CA/certs/app1.crt /data/app1/
2.总结ssh常用参数、用法
ssh命令:是ssh客户端,允许实现对远程系统经验证地加密安全访问
ssh命令格式:
ssh [user@]host [COMMAND]
ssh [-l user] host [COMMAND]
常见选项:
-p port:远程服务器监听的端口
-b 指定连接的源IP
-v 调试模式
-C 压缩方式
-X 支持x11转发
-t 强制伪tty分配,如:ssh -t remoteserver1 ssh -t remoteserver2 ssh remoteserver3 ; 1-->2-->3,最终目的链接3
-o option 如:-o StrictHostKeyChecking=no
-i <file> 指定私钥文件路径,实现基于key验证,默认使用文件: ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519,~/.ssh/id_rsa等
范例1:远程连接服务器
[root@centos8 ~]#ssh -t 10.0.0.81 ssh -t 10.0.0.82 ssh 10.0.0.83
范例2:远程执行命令
[root@centos6 ~]#ssh 10.0.0.82 "sed -i.bak '/StrictHostKeyChecking/s/.*/StrictHostKeyChecking no/' /etc/ssh/ssh_config"
范例3:在远程主机运行本地shell脚本
脚本在客服端
[root@centos8 ~]#ssh 10.0.0.83 /bin/bash < test.sh
脚本在服务端
[root@centos8 ~]#ssh 10.0.0.83 /data/test.sh
2 总结sshd服务常用参数。
服务器端:sshd
服务器端的配置文件: /etc/ssh/sshd_config
服务器端的配置文件帮助:man 5 sshd_config
常用参数:
Port 22 #链接外网的服务,端口要一定要修改
ListenAddress ip
LoginGraceTime 2m
PermitRootLogin yes #默认ubuntu不允许root远程ssh登录
StrictModes yes #检查.ssh/文件的所有者,权限等
MaxAuthTries 6
MaxSessions 10 #同一个连接最大会话
PubkeyAuthentication yes #基于key验证
PermitEmptyPasswords no #空密码连接
PasswordAuthentication yes #基于用户名和密码连接
GatewayPorts no
ClientAliveInterval 10 #单位:秒
ClientAliveCountMax 3 #默认3
UseDNS yes #提高速度可改为no
GSSAPIAuthentication yes #提高速度可改为no
MaxStartups #未认证连接最大值,默认值10
Banner /path/file
#以下可以限制可登录用户的办法:
AllowUsers user1 user2 user3
DenyUsers
AllowGroups
范例1:设置ssh 空闲60s 自动注销
Vim /etc/ssh/sshd_config
ClientAliveInterval 60
ClientAliveCountMax 0
Service sshd restart
#注意:新开一个连接才有效
范例2:解决ssh登录缓慢的问题
vim /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
systemctl restart sshd
范例3:在 ubuntu 上启用root 远程ssh登录
#修改sshd服务配置文件
vim /etc/ssh/sshd_config
#PermitRootLogin prohibit-password 注释掉此行
PermitRootLogin yes 修改为下面形式
systemctl restart sshd
ssh服务的最佳实践
- 建议使用非默认端口
- 禁止使用protocol version 1
- 限制可登录用户
- 设定空闲会话超时时长
- 利用防火墙设置ssh访问策略
- 仅监听特定的IP地址
- 基于口令认证时,使用强密码策略,比如:tr -dc A-Za-z0-9_ < /dev/urandom | head -c 12|
- xargs
- 使用基于密钥的认证
- 禁止使用空密码
- 禁止root用户直接登录
- 限制ssh的访问频度和并发在线数
- 经常分析日志