cp /etc/passwd{,.dist}
1. 禁用不使用的用户
注意:不建议直接删除,当你需要某个用户时,自己重新添加会很麻烦。也可以usermod -L或passwd -l user锁定。
cp /etc/passwd{,.bak}修改之前先备份
vi /etc/passwd编辑用户,在前面加上#注释掉此行
# cat /etc/passwd|grep ^#
#adm:x:3:4:adm:/var/adm:/sbin/nologin
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt
#uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
#operator:x:11:0:operator:/root:/sbin/nologin
#games:x:12:100:games:/usr/games:/sbin/nologin
#gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
#ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
#nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
#postfix:x:89:89::/var/spool/postfix:/sbin/nologin
注释的组:
# cat /etc/group|grep ^
# #adm:x:4:adm,daemon
#lp:x:7:daemon #uucp:x:14:
#games:x:20: #gopher:x:30:
#video:x:39: #dip:x:40:
#ftp:x:50: #audio:x:63:
#floppy:x:19:
#postfix:x:89:
2. 关闭不使用的服务
# chkconfig --list |grep '3:on'
通用unix打印服务,对服务器无用: ```shell service cups stop chkconfig cups --level 2345 off service cpuspeed stop chkconfig cpuspeed --level 2345 off service bluetooth stop chkconfig bluetooth --level 2345 off service firstboot stop chkconfig firstboot --level 2345 off service netfs stop chkconfig netfs --level 2345 off service nfslock stop chkconfig nfslock --level 2345 off service acpid start && chkconfig acpid on 3. 禁用IPV6 禁止加载IPv6模块 alias net-pf-10 off options ipv6 disable=1 # vi /etc/sysconfig/network NETWORKING_IPV6=no # vi /etc/sysconfig/network-scripts/ifcfg-eth0 IPV6INIT=no IPV6_AUTOCONF=no # chkconfig ip6tables off # lsmod | grep ipv6 # ifconfig | grep -i inet6 4. iptables规则 5. SSH安全设置 # useradd crm -d /apps/crm # passwd crm # useradd sysmgr # passwd sysmgr5.1 只允许wheel用户组的用户su切换# usermod -G wheel sysmgr # vi /etc/pam.d/su # Uncomment the following line to require a user to be in the "wheel" group. auth required pam_wheel.so use_uid 5.2 登录超时 export TMOUT=300 readonly TMOUT5.3 禁止root直接远程登录# vi /etc/ssh/sshd_config PermitRootLogin no5.4 限制登录失败次数并锁定 auth required pam_tally2.so deny=6 unlock_time=180 even_deny_root root_unlock_time=180 5.5 登录IP限制 ## allowed ssh users sysmgr AllowUsers sysmgr@172.29.73.* vi /etc/hosts.deny sshd:allvi /etc/hosts.allow sshd:172.29.73.23 sshd:172.29.73.6. 配置只能使用密钥文件登录 [dir@username ~]$ ssh-keygen -t rsa -b 2048 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): //默认路径,回车 Enter passphrase (empty for no passphrase): //输入你的密钥短语,登录时使用 Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 3e:fd:fc:e5:d3:22:86:8e:2c:4b:a7:3d:92:18:9f:64 root@ibpak.tp-link.net The key's randomart image is: +--[ RSA 2048]----+ | | … | o++o..oo..o| +-----------------+ $ mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys Match User itsection PasswordAuthentication no 7. 减少history命令记录 vi /etc/profile 8. 增强特殊文件权限 chattr +i /etc/passwd chattr +i /etc/shadow chattr +i /etc/group chattr +i /etc/gshadow chattr +i /etc/services #给系统服务端口列表文件加锁,防止未经许可的删除或添加服务 chattr +i /etc/pam.d/su chattr +i /etc/ssh/sshd_config lsattr /etc/passwd /etc/shadow /etc/services /etc/ssh/sshd_config 9. 防止一般网络攻击 9.1 禁ping # echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all iptables -A INPUT -p icmp --icmp-type 0 -s 0/0 -j DROP iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP9.2. 防止IP欺骗 order hosts,bind #名称解释顺序 multi on #允许主机拥有多个IP地址 nospoof on #禁止IP地址欺骗9.3 防止DoS攻击 * soft core 0 * soft nproc 2048 * hard nproc 16384 * soft nofile 1024 * hard nofile 65536core 0表示禁止创建core文件
nproc 128把*多的进程数限制到20
nofile 64表示把一个用户同时打开的*大文件数限制为64
*表示登录到系统的所有用户,不包括root
session required pam_limits.so 10. 修复已知安全漏洞 11. 定期做日志安全检查/var/log/message– 记录系统日志或当前活动日志
/var/log/auth.log– 身份认证日志
/var/log/cron– Crond 日志 (cron 任务)
/var/log/maillog– 邮件服务器日志
/var/log/secure– 认证日志
/var/log/wtmp历史登录、注销、启动、停机日志和,lastb命令可以查看登录失败的用户
/var/run/utmp当前登录的用户信息日志,w、who命令的信息便来源与此
/var/log/yum.logYum 日志
11.1 安装logwatch 12. web服务器安全