1. ssh服务

服务器端:sshd

服务器端的配置文件: /etc/ssh/sshd_config

服务器端的配置文件帮助:man 5 sshd_config

常用参数:

Port                          #生产建议修改
ListenAddress ip
LoginGraceTime 2m
PermitRootLogin yes           #默认ubuntu不允许root远程ssh登录
StrictModes yes               #检查.ssh/文件的所有者,权限等
MaxAuthTries   6              #pecifies the maximum number of authentication attempts permitted per connection. Once the number of failures reaches half this value, additional failures are logged. The default is 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 user1 user2 user3
AllowGroups g1 g2
DenyGroups g1 g2

设置 ssh 空闲60s 自动注销

Vim /etc/ssh/sshd_config
ClientAliveInterval   60
ClientAliveCountMax   0

Service sshd restart
#注意:新开一个连接才有效

解决ssh登录缓慢的问题

vim /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no

systemctl restart sshd

在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的访问频度和并发在线数
  • 经常分析日志

2. 批量部署多台主机基于key验证脚本

#!/bin/bash
#
#********************************************************************
#Author:		jiangfeng
#QQ: 			1461918614
#Date: 			2021-07-26
#FileName:		sshpass.sh
#Description:		The test script
#Copyright (C): 	2021 All rights reserved
#********************************************************************
IPLIST="
10.0.0.147
10.0.0.149
10.0.0.150
10.0.0.151
"
rpm -q sshpass &> /dev/null || yum install sshpass -y
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=1
for IP in $IPLIST;do
	sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done

3. 一些ssh客户端工具

3.1 scp命令

用法:

scp [options] [user@]host:/sourcefile /destpath
scp [options] /sourcefile [user@]host:/destpath
scp [options] [user@]host1:/sourcetpath [user@]host2:/destpath

常用选项:

-C 			压缩数据流
-r 			递归复制
-p 			保持原文件的属性信息
-q 			静默模式
-P PORT 	指明remote host的监听的端口

3.2 rsync命令

rsync工具可以基于ssh和rsync协议实现高效率的远程系统之间复制文件,使用安全的shell连接做为传输方式,比scp更快,基于增量数据同步,即只复制两方不同的文件,此工具来自于rsync包 注意:通信两端主机都需要安装 rsync 软件

rsync  -av /etc server1:/tmp     #复制目录和目录下文件
rsync  -av /etc/ server1:/tmp    #只复制目录下文件

常用选项:

-n 			模拟复制过程
-v 			显示详细过程
-r 			递归复制目录树
-p 			保留权限
-t 			保留修改时间戳
-g 			保留组信息
-o 			保留所有者信息
-l 			将软链接文件本身进行复制(默认)
-L 			将软链接文件指向的文件复制
-u 			如果接收者的文件比发送者的文件较新,将忽略同步
-z 			压缩,节约网络带宽
-a 			存档,相当于-rlptgoD,但不保留ACL(-A)和SELinux属性(-X)
--delete 	源数据删除,目标数据也自动同步删除

3.3 sftp命令

交互式文件传输工具,用法和传统的ftp工具相似,利用ssh服务实现安全的文件上传和下载使用ls cd mkdir rmdir pwd get put等指令,可用?或help获取帮助信息

[root@10 ~]#sftp [user@]host
sftp> help