第420集集群架构-SSH远程服务管理 | 字数总计: 4.4k | 阅读时长: 18分钟 | 阅读量:
集群架构 - SSH远程服务管理 1. SSH远程服务端 1.1 SSH服务概述 SSH服务由服务端软件OpenSSH和SSH客户端组成。
OpenSSH提供一个安全的远程Secure shell用于管理远程Linux系统。
OpenSSH使用非对称加密手段加密保护通信数据。
1.2 SSH服务特点
SSH建立在应用层基础之上的安全协议 : 可靠,提供安全的远程登录会话协议
SSH服务端后台启动名为sshd : 实时监听22端口,响应来自客户端的请求连接
SSH服务端几乎支持所有的UNIX平台 : 同时有效的防止远程管理过程中信息泄露
SSH客户端适用多种平台 : ssh远程连接、scp远程拷贝、sftp远程传输
1.3 SSH服务详细概述 SSH协议优势 :
加密传输数据
防止中间人攻击
支持多种认证方式
支持端口转发
OpenSSH详细概述 :
OpenSSH是SSH协议的免费开源实现
提供SSH客户端和服务器端程序
支持多种加密算法
支持密钥认证和密码认证
1.4 安装SSH服务端 1 2 3 4 5 6 7 8 9 10 11 12 13 [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~] tcp LISTEN 0 128 :22 *:* users :(("sshd",pid=1234 ,fd=3 ))
2. SSH远程客户端 2.1 SSH客户端概述 SSH是典型的客户端和服务端的交互模式,客户端广泛的支持各个平台。
Windows平台 :
有很多工具可以支持SSH连接功能
建议使用Xshell、PuTTY、SecureCRT等
Linux平台 :
需要安装客户端软件
yum install openssh-clients
2.2 客户端连接SSH服务方式 基本连接方式 1 2 3 4 5 6 7 8 [root@liyanzhao ~] [root@liyanzhao ~] [root@liyanzhao ~]
示例 :
1 2 3 4 5 6 7 8 ssh root@192.168.56.11 ssh -p 2222 root@192.168.56.11 ssh root@192.168.56.11 "ls -l /tmp"
2.3 SCP全量远程拷贝命令 命令参数说明 :
-P: 指定端口,默认22端口可不写(大写)
-r: 递归拷贝目录
-p: 保持文件属性不变
-l: 限制传输速度
推送:push(上传) 1 2 3 4 5 scp -P22 -pr /etc/inittab root@192.168.56.11:/tmp/ scp -P22 -pr /etc/sysconfig root@192.168.56.11:/tmp/
拉取:pull(下载) 1 2 3 4 5 scp -P22 -pr root@192.168.56.11:/tmp/inittab /root/ scp -P22 -pr root@192.168.56.11:/tmp/sysconfig /root/
注意 : scp通过加密的远程拷贝,支持数据推送和拉取,但仅能全量拷贝效率低。
2.4 SFTP远程传输命令 SFTP(SSH File Transfer Protocol)是SSH的扩展,提供交互式文件传输功能。
连接远程SFTP 1 2 sftp root@192.168.56.12
SFTP常用命令
命令
说明
get
下载文件
put
上传文件
ls
列出远程目录
lls
列出本地目录
cd
切换远程目录
lcd
切换本地目录
pwd
显示远程当前目录
lpwd
显示本地当前目录
quit 或 exit
退出
SFTP使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 sftp root@192.168.56.12 sftp> get conf.txt /tmp/ sftp> put /root/t1.txt /root/ sftp> get -r /remote/dir /local/dir sftp> put -r /local/dir /remote/dir
3. SSH远程登录方式 3.1 基于账户密码远程登录 知道服务器的IP端口、账号密码,即可通过ssh客户端登陆远程主机,远程主机联机过程中传输数据都是加密的。
1 2 3 4 5 ➜ ~ ssh -p22 root@192.168.56.11 root@192.168.56.11's password: Last login: Mon Dec 18 16:33:38 2017 from 192.168.56.1 [root@A_Server ~]#
密码登录特点 :
简单易用
需要每次输入密码
密码可能被暴力破解
安全性相对较低
3.2 基于密钥远程登录 默认情况下,通过ssh客户端登陆远程服务器,需要提供远程系统上的帐号与密码,但为了降低密码泄露的机率和提高登陆的方便性,建议使用密钥验证方式。
SSH密钥原理 :
ssh key使用非对称加密方式生成公钥和私钥
私钥存放在本地~/.ssh目录
公钥存储至需要登陆的服务器~/.ssh/authorized_keys
生成SSH密钥对 1 2 3 4 5 6 7 8 9 10 11 [root@A_Server ~] [liyanzhao@A_Server ~]$ ssh-keygen -t rsa -C liyanzhao@foxmail.com Generating public/private rsa key pair. Enter file in which to save the key (/home/liyanzhao/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/liyanzhao/.ssh/id_rsa. Your public key has been saved in /home/liyanzhao/.ssh/id_rsa.pub.
密钥类型 :
rsa: RSA算法(推荐)
dsa: DSA算法
ecdsa: ECDSA算法
ed25519: Ed25519算法(最新,推荐)
分发公钥到远程服务器 1 2 3 4 5 6 7 [liyanzhao@A_Server .ssh]$ ssh-copy-id -i ~/.ssh/id_rsa.pub liyanzhao@192.168.56.12 /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 liyanzhao@192.168.56.12's password: Number of key(s) added: 1
使用密钥登录 1 2 3 4 [liyanzhao@A_Server ~]$ ssh liyanzhao@192.168.56.12 Last login: Wed Dec 13 18:07:16 2017 from 192.168.56.1 [liyanzhao@B_Server ~]$
密钥登录特点 :
无需输入密码
安全性高
支持批量管理
适合自动化脚本
常见错误处理 错误1: no route to host
1 2 3 firewall-cmd --list-all
错误2: Connection refused
4. SSH配置安全防护 4.1 SSH服务登录防护手段
更改SSH服务远程登录端口
更改SSH服务监听本地内网IP
更改SSH服务禁止密码登录
更改SSH服务禁止ROOT管理员登录
更改SSH服务密码登录认证为密钥登录
重要服务器尽可能不使用公网IP地址
使用防火墙限制来源IP地址
4.2 SSH服务配置文件 SSH服务登录防护手段配置文件:/etc/ssh/sshd_config
4.3 SSH安全配置详解 更改SSH远程连接端口 1 2 3 4 5 vim /etc/ssh/sshd_config Port 6666
注意 : 修改端口后需要:
绑定本地内网地址 1 2 ListenAddress 192.168.56.11
禁止Root管理员登录
禁止密码登录 1 2 PasswordAuthentication no
允许以root密钥方式登陆 1 2 PermitRootLogin without-password
禁止使用空密码 1 2 PermitEmptyPasswords no
关闭DNS解析,优化连接速度 1 2 3 UseDNS no GSSAPIAuthentication no
4.4 完整SSH安全配置示例 添加如下配置至sshd配置文件/etc/ssh/sshd_config,根据需求做调整:
1 2 3 4 5 6 7 8 9 Port 6666 ListenAddress 192.168.56.11 PasswordAuthentication no PermitRootLogin no GSSAPIAuthentication no UseDNS no
4.5 应用配置并重启服务 1 2 3 4 5 6 7 8 sshd -t systemctl restart sshd systemctl status sshd
4.6 其他安全建议 1 2 3 4 5 6 7 8 9 10 11 12 13 14 MaxAuthTries 3 ClientAliveInterval 300 ClientAliveCountMax 2 AllowUsers liyanzhao DenyUsers root AllowGroups wheel DenyGroups test
5. SSH密钥分发实战 5.1 需求说明 要求所有服务器在统一用户liyanzhao,实现A机器从本地分发数据到B、C机器上,在分发到B、C的过程中不需要输入密码验证,当然除了分发的功能,还可以批量查看所有客户机上的CPU,LOAD,MEM,系统版本等信息。即实现从A服务器发布数据到B、C客户端服务器以及查看信息的免密码登录验证解决方案。
5.2 密钥批量分发 步骤1: 添加普通用户账号 部署密钥之前,先分别在A、B、C服务器上添加普通用户并配置密码。
1 2 3 useradd liyanzhao echo "123456" | passwd --stdin liyanzhao
步骤2: A服务器切换普通用户并生成密钥 如下操作均在A-Server服务器上操作:
1 2 3 4 5 6 7 8 9 10 11 [root@A_Server ~] [liyanzhao@A_Server ~]$ ssh-keygen -t rsa -C A-Server.com Generating public/private rsa key pair. Enter file in which to save the key (/home/liyanzhao/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/liyanzhao/.ssh/id_rsa. Your public key has been saved in /home/liyanzhao/.ssh/id_rsa.pub.
步骤3: A服务器分发秘钥至B服务器 SSH不是默认端口,使用-P指定对应端口。
1 2 [liyanzhao@A_Server ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub "-p6666 liyanzhao@192.168.56.12"
步骤4: 通过A服务器登陆至B服务器测试 1 2 3 4 [liyanzhao@A_Server ~]$ ssh -p22 liyanzhao@192.168.56.12 Last login: Wed Dec 13 18:07:16 2017 from 192.168.56.1 [liyanzhao@B_Server ~]$
步骤5: 分发A服务器秘钥至C服务器 1 2 [liyanzhao@A_Server ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub "-p6666 liyanzhao@192.168.56.13"
步骤6: 通过A服务器登陆至C服务器测试 1 2 3 4 [liyanzhao@A_Server ~]$ ssh -p22 liyanzhao@192.168.56.13 Last login: Wed Dec 13 18:07:16 2017 from 192.168.56.1 [liyanzhao@C_Server ~]$
5.3 批量执行命令及文件分发 批量执行命令脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@A_Server ~] if [ $# -ne 1 ];then echo "USAGE $0 : {Please Command}" exit 1 fi Command="$1 " IP="192.168.56" User=liyanzhao Port=22 for i in 12 13do echo "##Server IS $IP .$i ###" /usr/bin/ssh $User @$IP .$i -p"$Port " "$Command " done EOF chmod +x ssh_command.sh./ssh_command.sh "uptime" ./ssh_command.sh "free -h" ./ssh_command.sh "df -h"
批量分发文件脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@A_Server ~] if [ $# -ne 2 ];then echo "USAGE $0 : {LocalFile|RemoteFile}" exit 1 fi File1="$1 " DirFile="$2 " IP="192.168.56" User=liyanzhao Port=22 for i in 12 13do echo "##Server IS $IP .$i ###" /usr/bin/scp -rp -P"$Port " $File1 $User @$IP .$i :"$DirFile " done EOF chmod +x ssh_file.sh./ssh_file.sh /root/test.txt /tmp/ ./ssh_file.sh /root/config/ /tmp/
6. SSH企业项目案例 6.1 项目需求 ssh密钥实现批量分发备份、批量管理实战考试
时间 : 5分钟
环境 : 请准备有三台linux机器分别为A,B,C
6.2 题目1: 一把钥匙开多把锁 用自己的名字后面加888(例如:liyanzhao888)的用户完成一把钥匙开多把锁(A钥匙,B、C锁)的免密码验证登陆部署场景。
架构图 :
1 2 A服务器(liyanzhao888)--密钥--> B服务器(liyanzhao888) A服务器(liyanzhao888)--密钥--> C服务器(liyanzhao888)
解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 useradd liyanzhao888 echo "123456" | passwd --stdin liyanzhao888su - liyanzhao888 ssh-keygen -t rsa -C "A-Server" ssh-copy-id liyanzhao888@B服务器IP ssh-copy-id liyanzhao888@C服务器IP ssh liyanzhao888@B服务器IP ssh liyanzhao888@C服务器IP
6.3 题目2: 多把钥匙开一把锁 在不破坏题1的前提下,同样用自己的名字加888的用户完成多把钥匙开一把锁(B、C钥匙,A锁)的免密码验证登陆部署场景。
架构图 :
1 2 B服务器(liyanzhao888)--密钥--> A服务器(liyanzhao888) C服务器(liyanzhao888)--密钥--> A服务器(liyanzhao888)
解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 su - liyanzhao888 ssh-keygen -t rsa -C "B-Server" ssh-copy-id liyanzhao888@A服务器IP su - liyanzhao888 ssh-keygen -t rsa -C "C-Server" ssh-copy-id liyanzhao888@A服务器IP ssh liyanzhao888@A服务器IP ssh liyanzhao888@A服务器IP
6.4 题目3: 批量分发文件到用户家目录 如何实现从A指定目录批量分发文件到B、C用户的家目录(借助问题1的部署结果)。
时间 : 2分钟
解答 : 考察scp用法
解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 scp /path/to/file liyanzhao888@B服务器IP:~/ scp /path/to/file liyanzhao888@C服务器IP:~/ cat > batch_scp.sh <<'EOF' File="$1 " for host in B服务器IP C服务器IPdo scp $File liyanzhao888@$host :~/ done EOF chmod +x batch_scp.sh./batch_scp.sh /path/to/file
6.5 题目4: 分发文件到任意目录(普通用户) 实现从A指定目录的文件分发到B、C的任意目录下(不用root用户如何实现)?
时间 : 5分钟
解答 : 考察普通用户提权
解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 visudo liyanzhao888 ALL=(ALL) NOPASSWD: /usr/bin/scp scp /path/to/file liyanzhao888@B服务器IP:/tmp/ ssh liyanzhao888@B服务器IP "sudo cp /tmp/file /etc/" ssh liyanzhao888@B服务器IP "chmod 777 /target/dir" scp /path/to/file liyanzhao888@B服务器IP:/target/dir/ rsync -avz /path/to/file liyanzhao888@B服务器IP:/target/dir/
6.6 题目5: 批量查看系统信息 如何快速查看所有机器的负载load,CPU,内存等信息(借助问题1的部署结果)。
思考 : 如果服务器数量多,如何并发查看和分发数据
时间 : 5分钟
解决方案 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 cat > batch_info.sh <<'EOF' for host in B服务器IP C服务器IPdo echo "=== $host ===" ssh liyanzhao888@$host "uptime && free -h && df -h" done EOF cat > batch_info_parallel.sh <<'EOF' for host in B服务器IP C服务器IPdo { echo "=== $host ===" ssh liyanzhao888@$host "uptime && free -h && df -h" } & done wait EOF yum install pssh -y cat > hosts.txt <<EOF liyanzhao888@B服务器IP liyanzhao888@C服务器IP EOF pssh -h hosts.txt -i "uptime && free -h && df -h"
6.7 高级批量管理方案 使用Ansible(推荐) 1 2 3 4 5 6 7 8 9 10 11 12 yum install ansible -y cat > /etc/ansible/hosts <<EOF [webservers] B服务器IP C服务器IP EOF ansible webservers -m shell -a "uptime && free -h"
使用expect自动化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 yum install expect -y cat > auto_scp.exp <<'EOF' set host [lindex $argv 0]set file [lindex $argv 1]set dest [lindex $argv 2]spawn scp $file liyanzhao888@$host :$dest expect { "yes/no" { send "yes\r" ; exp_continue } "password:" { send "123456\r" } } expect eof EOF chmod +x auto_scp.exp./auto_scp.exp B服务器IP /path/to/file /tmp/
7. SSH高级应用 7.1 SSH隧道 本地端口转发 1 2 ssh -L 8080:localhost:80 user@remotehost
远程端口转发 1 2 ssh -R 8080:localhost:80 user@remotehost
动态端口转发(SOCKS代理) 1 2 ssh -D 1080 user@remotehost
7.2 SSH跳板机 1 2 3 4 5 6 7 8 9 10 ssh -J jumpuser@jumphost targetuser@targethost cat >> ~/.ssh/config <<EOF Host target HostName targethost User targetuser ProxyJump jumpuser@jumphost EOF
7.3 SSH配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 vim ~/.ssh/config Host server1 HostName 192.168.56.11 User root Port 22 IdentityFile ~/.ssh/id_rsa Host server2 HostName 192.168.56.12 User liyanzhao Port 6666 IdentityFile ~/.ssh/id_rsa
8. SSH故障排查 8.1 常见问题 问题1: 连接超时 1 2 3 4 5 6 7 8 ping 目标服务器IP telnet 目标服务器IP 22 firewall-cmd --list-all
问题2: 权限被拒绝 1 2 3 4 5 6 7 8 ls -l ~/.ssh/chmod 600 ~/.ssh/id_rsachmod 644 ~/.ssh/id_rsa.publs -l ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keys
问题3: 主机密钥验证失败 1 2 3 4 5 ssh-keygen -R 目标服务器IP vim ~/.ssh/known_hosts
8.2 调试模式 1 2 3 4 5 6 7 8 ssh -v user@host ssh -vv user@host ssh -vvv user@host tail -f /var/log/securejournalctl -u sshd -f
9. SSH安全最佳实践 9.1 安全建议
使用密钥认证 : 禁用密码认证
更改默认端口 : 避免使用22端口
限制用户登录 : 使用AllowUsers/DenyUsers
使用防火墙 : 限制来源IP
定期更新 : 保持SSH软件最新
监控日志 : 定期检查登录日志
使用fail2ban : 防止暴力破解
9.2 Fail2ban配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 yum install fail2ban -y cat > /etc/fail2ban/jail.local <<EOF [sshd] enabled = true port = 22 maxretry = 3 bantime = 3600 EOF systemctl start fail2ban systemctl enable fail2ban
实战优化