阅读(2990)

linux下Centos7系统加固方法

最后一次修改 2018年03月01日

注意:此教程的云服务器以centos7以上为例,云服务器于阿里云购买

其他服务商的云服务器配置大同小异

建议:linux的服务器不建议安装图形化工具,因为占内存,占带宽,占资源,弊远大于利

手动更新系统:

yum -y update

防火墙配置:

service firewalld start //启动防火墙
systemctl enable firewalld.service //开机自启

selinux配置:

vim /etc/selinux/config

修改:

SELINUX=enforcing //设置强制模式
reboot //重启生效

ssh配置:(防暴力破解)

useradd normal //创建一个系统用户,设置只能通过这个用户远程登录系统
vim /etc/ssh/sshd_config

修改:

Port 2000 //端口必须大于1024
Protocol 2 //没有的话就添加,有就不用
PermitEmptyPasswords no //禁止空密码登录
X11Forwarding no //禁止端口转发
PermitRootLogin no //禁止root用户登录
MaxAuthTries 3 //允许三次尝试
LoginGraceTime 20 //在20秒内不能完成登录,则断开连接
AllowUsers normal //添加,只允许这个用户远程登录

保存退出,重启ssh

service sshd restart

防火墙开启ssh端口

firewall-cmd --zone=public --add-port=2000/tcp --permanent
firewall-cmd --reload

selinux开启ssh端口

yum -y install policycoreutils-python //安装selinux端口管理工具
semanage port -a -t ssh_port_t -p tcp 2000 //添加端口
semanage port -l |grep ssh //查看selinux开启的ssh端口
service sshd restart

防止IP SPOOF攻击

vim /etc/host.conf

末尾添加

nospoof on

禁止被ping

vim /etc/sysctl.conf

有则修改,无则添加

net.ipv4.icmp_echo_ignore_all=0

保存配置

sysctl -p

防火墙禁止被ping

firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop' 
firewall-cmd --reload

注意:也可以在阿里云控制台的安全组规则,删除允许ICMP协议的规则

每十多天更新一次系统,删除没有用到的软件,清除yum缓存

crontab -e

以下内容按需修改

0 0 */10 * * yum update -y
0 0 */11 * * yum autoremove -y
0 0 */12 * * yum clean all

防火墙禁止端口扫描(centos7无效,端口还是被扫描出来了,不知道centos7以下是否生效)

iptables -F #清除防火墙策略
iptables -A INPUT -p tcp --tcp-flags ALL FIN,URG,PSH -j Drop
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j Drop
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j Drop
iptables -A INPUT -p tcp --tcp-flags SYN,SYN --dport 80 -j Drop

卸载阿里云的云盾(安骑士),因为服务器本来就内存紧张,云盾弊大于利,卸载

wget http://update.aegis.aliyun.com/download/uninstall.sh
chmod +x uninstall.sh
./uninstall.sh
wget http://update.aegis.aliyun.com/download/quartz_uninstall.sh
chmod +x quartz_uninstall.sh
./quartz_uninstall.sh
pkill aliyun-service
rm -fr /etc/init.d/agentwatch /usr/sbin/aliyun-service
rm -rf /usr/local/aegis*

注意:卸载完成后,可以删除以上两个脚本文件。如果无法wget到文件,请联系站长索要!

屏蔽云盾IP,云盾会定期扫描服务器模拟黑客攻击

vim shield_ip.sh

添加如下内容:

#!/bin/bash
echo "开始屏蔽云盾扫描云服务器的IP"
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.201.0/28" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.201.16/29" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.201.32/28" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.192/29" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.200/30" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.184/29" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.183/32" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.206/32" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.205/32" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.195/32" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="140.205.225.204/32" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.224.0/26" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.224.64/26" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.224.128/26" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.224.192/26" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.222.64/26" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.222.128/26" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.222.192/26" drop'
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address="106.11.223.0/26" drop'
firewall-cmd --reload

保存退出

chmod +x shield_ip.sh
./shield_ip.sh

注意:这些IP地址段来源于阿里云官方给的云盾服务器IP,来源:(https://help.aliyun.com/knowledge_detail/37436.html)

编码设置:

vim /etc/locale.conf

删除原有,添加如下内容:

LANG=zh_CN.utf8 //中文界面
LC_MESSAGES=en_US.utf8 //英文提示

reboot //重启生效

进入阿里云控制台,云服务器ECS–>安全组–>配置规则–>添加安全组规则

安全组添加ssh端口,否则外网是无法进入的,包括ftp和apache的端口不在安全组开放的话

下载xshell远程登录软件,normal用户远程登录至linux系统,xshell的使用不再赘述,登录成功后

su - root //提权

注意:在阿里云控制台远程连接登录系统后,不能以任何用户一直处于登录状态,使用系统完后,必须退出用户登录,界面保持在需要输入用户名的界面

如:在阿里云控制台登录(而不是xshell登录),退出用户登录命令

logout //exit也可以

注意:root用户的话必须退出两次才可以

最后:在阿里云控制台–>安全(云盾)–>态势感知–>开启态势感知服务–>设置邮箱或短信提醒