最小化安装
分区
/boot 1024MB
Swap 4096 8192m
/
查看IP地址
ip addr
vi /etc/sysconfig/network-scripts/ifcfg-ens18
ONBOOT=yes
netstat,和ifconfig不能使用,安装net-tools
yum install net-tools
关闭不用的服务
systemctl disable rpcbind
systemctl stop postfix.service
systemctl disable postfix.service
yum remove postfix
Chronyd用于同步时间,代替ntp
systemctl stop chronyd.service
systemctl disable chronyd.service
网络管理包
systemctl stop NetworkManager.service
systemctl disable NetworkManager.service
hostnamectl set-hostname zhfile
字符集配置
locale
vi /etc/locale.conf
LANG="en_US" 或者
LANG="zh_CN.GBK"
关闭防火墙和selinux
systemctl stop firewalld.service
systemctl disable firewalld.service
firewall-cmd --state
yum remove firewalld
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
开启iptables防火墙
vi iptables.sh
#!/bin/bash
# chkconfig: 2345 08 92
# description: Starts, stops and saves iptables firewall
/usr/sbin/iptables -F
#iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/usr/sbin/iptables -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
/usr/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
/usr/sbin/iptables -A INPUT -p tcp --dport 1024:65535 --sport 80 -j ACCEPT
/usr/sbin/iptables -A INPUT -p tcp -s 192.168.1.187 -j ACCEPT
/usr/sbin/iptables -A INPUT -p tcp -s 192.168.3.187 -j ACCEPT
/usr/sbin/iptables -A INPUT -p tcp -s localhost -j ACCEPT
/usr/sbin/iptables -A INPUT -p tcp -j DROP
cp iptables.sh /etc/init.d/
chkconfig iptables.sh on
chkconfig --list
systemctl list-unit-files
systemctl list-unit-files|grep
优化部分系统参数
yum vim
ulimit -a查看系统的默认限制,open files,max user processes
单个进程可以打开的文件数限制
vi /etc/security/limits.conf
* - nofile 65535
用户最大进程数限制max user processes根据需要修改,如655350
普通用户限制655350,root用户无限制
vi /etc/security/limits.d/20-nproc.conf
* soft nproc 655350
root soft nproc unlimited
系统systemd默认限制
vi /etc/systemd/system.conf
DefaultLimitNOFILE=655350
DefaultLimitNPROC=655350
所有进程可以打开的文件数限制,系统在安装的时候根据硬件自动设置的
cat /proc/sys/fs/file-max
可以暂时不修改,如果修改在/etc/sysctl.conf, 加入
fs.file-max = 6553560
查看某个进程(pid)的限制
cat /proc/PID/limits |grep "Max open files"
cat /proc/sys/fs/file-max
184108
更新系统,安装软件
yum update
yum install gcc rsync gcc-c++ zlib-devel tcpdump telnet wget
配置时间服务
yum remove chrony
使用ntp
yum install ntp
vi /etc/init.d/updatetime.sh
#!/bin/bash
case $1 in
stop)
exit
;;
*)
#ntpdate 120.25.108.11 203.107.6.88
/sbin/ntpdate ntp1.aliyun.com
;;
esac
chmod u+x updatetime.sh
使用crontab -e,输入如下信息,整点对时
0 0-23 * * * /etc/init.d/updatetime.sh
删除不用的用户
配置ssh和用户登录
修改ssh
默认端口和禁止root用户直接登录,密码验证错误次数
vi /etc/ssh/sshd_config
Port 22
PermitRootLogin no
MaxAuthTries 3 密码验证错误3次,断开连接默认6次
添加普通用户设置密码,用ssh远程登录
useradd puser
passwd puser
# vi /etc/ssh/sshd_config
找到
#ClientAliveInterval 0
#ClientAliveCountMax 3
修改为
ClientAliveInterval 300
ClientAliveCountMax 3
重启sshd服务
systemctl restart sshd
这样连接centos7长时间不操作也不会连接超时中断了。
登录锁定
配置系统用户密码输入错误3次,锁定10分钟(不包含root,因为root禁止远程登录)
vi /etc/pam.d/password-auth
auth required pam_tally2.so deny=3 unlock_time=600
account required pam_tally2.so
配置锁定
ssh 登录锁定
vi /etc/pam.d/sshd
在第一行下即#%PAM-1.0的下面添加:
auth required pam_tally2.so deny=3 unlock_time=600
终端登录锁定,暂时不用
vi /etc/pam.d/login
auth required pam_tally2.so deny=3 unlock_time=600
查看锁定的用户
pam_tally2
解锁用户
pam_tally2 -u puser -r
如果要包含root用使用下面设置
auth required pam_tally2.so deny=3 even_deny_root unlock_time=600
auth required pam_tally2.so deny=3 unlock_time=600 even_deny_root root_unlock_time=1200
记录用户登录失败信息
/var/log/secure
查看用户登录审计信息
aureport -au
重启
/etc/init.d/sshd restart
只有wheel组的用户可以su为root
vi /etc/pam.d/su
auth required pam_wheel.so use_uid #去掉这行注释
vi /etc/login.defs
SU_WHEEL_ONLY yes
groupadd wheel
usermod -G wheel puser
id puser
从wheel组中删除puser用户
gpasswd -d puser wheel
或者直接修改文件
vi /etc/group
wheel:x:10:puser,test
设置指定目录的权限
find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 644 {} \;
用户登录注销时间
set命令显示当前shell的变量,包括当前用户的变量;
env命令显示当前用户的变量;
export命令显示当前导出成用户变量的shell变量。
/etc/profile
export TMOUT=900 # 设置900秒内用户无操作就字段断开终端
命令历史记录条数
/etc/profile
Export HISTFILESIZE=1000 HISTSIZE=1000
HISTFILESIZE=1000
定义了在 .bash_history 中保存命令的记录总数1000,
HISTSIZE 定义了 history 命令输出的记录数
开启auditd审计功能
yum -y install audit
审计配置文件 /etc/audit/auditd.conf
service auditd start
systemctl enable auditd
添加审计规则
开启审计服务,会在下面目录产生日志文件
/var/log/audit/
查看统计报表
aureport
aureport -m
aureport -au
查看参数帮助
aureport --help
查看现有规则
auditctl -l
规则配置文件(设置一个自定义的规则)
/etc/audit/rules.d/audit.rules
-w /etc/passwd -p wa -k passwd_file 监控passwd文件的写入和属性修改,统计时关键字passwd_file
配置文件
/etc/audit/auditd.conf
重启审计服务
service auditd restart
开启系统更新安全补丁
yum安全更新:
使用:检查安全更新
yum --security check-update
只安装安全更新
yum update --security
检查特定软件有无安全更新,列出更新的详细信息,比如ssh
yum info-security ssh
自动安全更新脚本
vi /root/yum-seupdate.sh
#!/bin/bash
dt=`date +%Y/%m/%d:%T`
echo -e "==============$dt===========\n">>/var/log/security_update/security_update.log
yum update --security >> /var/log/security_update/security_update.log
echo -e "==============end=====================\n">>/var/log/security_update/security_update.log
每2小时执行一次安全更新
使用crontab -e,输入如下信息,整点对时
0 */2 * * * bash /root/yum-seupdate.sh
配置日志循环,每周1次,保留3周
vi /etc/logrotate.d/security_update
/var/log/security_update/*.log {
weekly
missingok
rotate 3
notifempty
sharedscripts
postrotate
/root/yum-seupdate.sh > /dev/null
endscript
}
强制执行一次日志循环
logrotate -f /etc/logrotate.d/security_update