centos 系统如何加强安全防护

文 | 诸葛运帷 子龙兄


1.ssh登录端口修改
vim /etc/ssh/sshd_config
#Port 22 //这行去掉#号
Port 51866 //下面添加这一行
为什么不先删除22,以防其他端口没配置成功,而又把22的删除了,无法再次进入服务器
systemctl restart sshd

修改SELinux
在色Linux中添加修改后的端口
semanage port -a -t ssh_port_t -p tcp 修改后的端口

需要安装以下命令(看是否已将安装好)
yum provides semanage yum -y install policycoreutils-python
# 为 ssh 添加新的允许端口 $ semanage port -a -t ssh_port_t -p tcp 修改后的端口 ----------添加端口

# 查看当前 SELinux 允许的端口

$ semanage port -l | grep ssh --------------查看添加的端口 ssh_port_t tcp 2048, 22
需要开启selinux ,然后添加端口访问


注意semange 不能禁用 ssh 的 22 端口:
semanage port -l | grep ssh ssh_port_t tcp 2048, 22

ssh配置超时时间
vim /etc/ssh/sshd_config
ClientAliveInterval 60 ClientAliveCountMax 10
ClientAliveInterval 是指系统判断超时的时间,单位是s,这里的意思是60s无响应则判断为超时一次
ClientAliveCountMax 是指允许超时的次数,这里允许超时十次


所以这里的配置允许超时 600s = 10min 。


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

安装DenyHosts
DenyHosts是Python语言写的一个程序,它会分析sshd的日志文件(/var/log/secure),当发现重 复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。

安装包已经下载完毕
tar zxvf DenyHosts-2.6.tar.gz
cd DenyHosts-2.6 进入安装解压目录
python setup.py install

DenyHosts
cd /usr/share/denyhosts/ #默认安装路径
cp denyhosts.cfg-dist denyhosts.cfg #denyhosts.cfg为配置文件
cp daemon-control-dist daemon-control #daemon-control为启动程序
chown root daemon-control #添加root权限
chmod 700 daemon-control #修改为可执行文件
ln -s /usr/share/denyhosts/daemon-control /etc/init.d #对daemon-control进行软连接,方便管理
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

/etc/init.d/daemon-control start #启动denyhosts
chkconfig daemon-control on #将denghosts设成开机启动


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
[root@www denyhosts]# vi denyhosts.cfg SECURE_LOG = /var/log/secure

#ssh日志文件

# format is: i[dhwmy]

# Where i is an integer (eg. 7)

# m = minutes

# h = hours

# d = days

# w = weeks

# y = years

# # never purge: PURGE_DENY = 50m

#过多久后清除已阻止IP HOSTS_DENY = /etc/hosts.deny

#将阻止IP写入到hosts.deny BLOCK_SERVICE = sshd

#阻止服务名 PURGE_THRESHOLD =

#定义了某一IP最多被解封多少次。某IP暴力破解SSH密码被阻止/解封达到了PURGE_THRESHOLD次,则会被永久禁止; DENY_THRESHOLD_INVALID = 1

#允许无效用户登录失败的次数 DENY_THRESHOLD_VALID = 10

#允许普通用户登录失败的次数 DENY_THRESHOLD_ROOT = 5

#允许root登录失败的次数 WORK_DIR = /usr/local/share/denyhosts/data

#将deny的host或ip纪录到Work_dir中 DENY_THRESHOLD_RESTRICTED = 1

#设定 deny host 写入到该资料夹 LOCK_FILE = /var/lock/subsys/denyhosts

#将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务。 HOSTNAME_LOOKUP=NO

#是否做域名反解 ADMIN_EMAIL =

#设置管理员邮件地址 DAEMON_LOG = /var/log/denyhosts

#DenyHosts日志位置


以后可以直接查看hosts.deny文件就能找到攻击ip的记录

vim /etc/hosts.deny
更改DenyHosts的默认配置之后,重启DenyHosts服务即可生效:

/etc/init.d/daemon-control restart #重启denyhosts

如果想删除一个已经禁止的主机IP,并加入到允许主机例表,只在 /etc/hosts.deny 删除是没用的。

需要进入 /usr/share/denyhosts 目录,进入以下操作:
1、停止DenyHosts服务:$ /etc/init.d/daemon-control stop
2、在 /etc/hosts.deny 中删除你想取消的主机IP
3、编辑 DenyHosts 工作目录的所有文件,通过
$ grep 127.0.0.1 /usr/share/denyhosts/data/*

#这行没明白,然后一个个删除文件中你想取消的主机IP所在的行:
*/usr/share/denyhosts/data/hosts
*/usr/share/denyhosts/data/hosts-restricted
*/usr/share/denyhosts/data/hosts-root
*/usr/share/denyhosts/data/hosts-valid
*/usr/share/denyhosts/data/users-hosts

4、添加你想允许的主机IP地址到
vi /etc/hosts.allow
# We mustn't block localhost
sshd: 127.0.0.1
sshd: 192.168.1.*
sshd: 公网IP

5、启动DenyHosts服务: /etc/init.d/daemon-control


2.centos 密码复杂度修改,密码过期时间


系统对密码的控制是有两部分(我知道的)组成:
  1 cracklib
  2 login.defs
  声明:login.defs主要是控制密码的有效期。对密码进行时间管理。
在CentOS下设置密码复杂度分为两步
(1)修改/etc/login.defs文件
vim /etc/login.defs
PASS_MAX_DAYS 90    # 密码最长过期天数
PASS_MIN_DAYS 80     # 密码最小过期天数
PASS_MIN_LEN 10     # 密码最小长度
PASS_WARN_AGE 7     # 密码过期警告天数
默认:

centos 系统如何加强安全防护

(2)修改/etc/pam.d/system-auth文件vim /etc/pam.d/system-auth
在 password requisite pam_cracklib.so 一行换成如下内容:
password requisite pam_cracklib.so retry=5 difok=3 minlen=10 ucredit=-1 lcredit=-3 dcredit=-3 dictpath=/usr/share/cracklib/pw_dict
参数含义:
尝试次数:5 ;

最少不同字符:3 ;

最小密码长度:10 ;

最少大写字母:1;

最少小写字母:3 ;

最少数字:3 ;

字典位置:/usr/share/cracklib/pw_dict
可以安装yum -y install cracklib*
命令进行密码复杂度测试
echo "mypass@word1" | cracklib-check

centos 系统如何加强安全防护

centos7 下密码定时修改,并将密码发送邮箱1.安装sendmail
yum -y install sendmail
systemctl start sendmail

2.安装mailx
yum install -y mailx


3.配置文件
vim /etc/mail.rc
结尾追加
set bsdcompat
set from=邮箱账号@163.com
set smtp=smtp.163.com
set smtp-auth-user=邮箱用户名
set smtp-auth-password=邮箱密码
set smtp-auth=login

二、更改密码脚本
vim password.sh
------------------------------------------------------------------------------------------------------------
#!/bin/bash
ip=`ip a show dev ens33|grep -w inet|awk '{print $2}'|awk -F '/' '{print $1}'`

#这一行是提取本机IP的,不需要改
tr -dc _A-Z-a-z#$%^*-0-9 /home/password.txt

#这一行是生成随机密码的
cat /home/password.txt |passwd root --stdin

#这一行是将生成的密码本机的密码
SendStatus=`mail -v -s "$ip test-root-password" [email protected]< /home/password.txt | grep -c "Mail Delivery Status Report will be mailed to <root>"`/<root>

#这一行是将密码发送到指定邮箱,这里你们设置自己的邮箱
if [ "$SendStatus" == "1" ] ; then
echo "Sender mail ok"
fi


rm -rf /home/password.txt
exit
---------------------------------------------------------------------------------------------------------

三、最后可以做个定时任务每月执行一次
0 0 * * 7 /home/password.sh > /dev/null 2>&1 每周日的晚上12点执行
成立:


centos 系统如何加强安全防护


禁用root账号ssh登录修改配置文件:vim /etc/ssh/sshd_config 改成no 然后重启sshd服务及可

centos 系统如何加强安全防护

·给普通账号赋权限:vim /etc/sudoers
方法一:将新创建的账号放在root的组下
将文件中的%wheel 一行,取消注释
%wheel ALL=(ALL) ALL

然后修改用户,使其属于root组(wheel),命令如下:

#usermod -g root 新建账号
修改完毕,现在可以用tommy帐号登录,然后用命令 sudo su - ,即可获得root权限进行操作。

方法二: 修改文件/etc/sudoers 找到root一行
添加内容
账号 ALL=(ALL) ALL
修改完毕,现在可以用tommy帐号登录,然后用命令 sudo su - ,即可获得root权限进行操作。

方法三:需改/etc/passwd 文件 把用户ID修改0

centos 系统如何加强安全防护

改成0:1002 即可例如:

centos 系统如何加强安全防护


centos 系统如何加强安全防护

要做未雨绸缪的先知者,不做亡羊补牢的救火员。




IT运维之眼-“诸葛运帷” 业务级运维监控管理平台,企业端到移动端的运维监控整体解决方案。系统以业务系统监控为主线,基于“业务、软件、网络、设备、动环”多个监控视角的运维体系架构,打造了一个多维度可视化的综合运维监控管理平台。系统以故障提前预警、问题快速定位为核心。切实保障信息系统的安全稳定运行。


分享到:


相關文章: