centos7.2系统优化

来源:互联网 发布:下载淘宝网商城 编辑:程序博客网 时间:2024/06/05 07:25
1 修改网卡名称为eth0
#mv ifcfg-eno16777729 ifcfg-eth0
#vi /etc/sysconfig/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0 rhgb quiet net.ifnames=0 biosdevname=0"
GRUB_DISABLE_RECOVERY="true"

修改 /etc/udev/rules.d/90-eno-fix.rules名称

#grub2-mkconfig -o /boot/grub2/grub.cfg #生成启动菜单
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-327.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-327.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-e8675ae79abd41309dac42388f8d9116
Found initrd image: /boot/initramfs-0-rescue-e8675ae79abd41309dac42388f8d9116.img

#reboot
#ip addr 或者
#yum install net-tools #默认centos7不支持ifconfig 需要看装net-tools包

2.更新系统

3.修改文件属性,最低权限原则
chmod 700 /bin/rpm 只有root权限用户才可以使用rpm命定,安装软件包
chmod 664 /etc/hosts
chmod 644 /etc/passwd
chmod 644 /etc/exports
chmod 644 /etc/issue
chmod 664 /var/log/wtmp
chmod 664 /var/log/btmp
chmod 644 /etc/services
chmod 600 /etc/shadow
chmod 600 /etc/login.defs
chmod 600 /etc/hosts.allow
chmod 600 /etc/hosts.deny
chmod 600 /etc/securetty
chmod 600 /etc/security
chmod 600 /etc/ssh/ssh_host_key
chmod 600 /etc/ssh/sshd_config
chmod 600 /var/log/lastlog
chmod 600 /var/log/messages

4. 添加用户fang
[root@bogon ~]# useradd fang
[root@bogon ~]# echo 123456 | passwd --stdin fang
Changing password for user fang.
passwd: all authentication tokens updated successfully.
[root@bogon ~]# usermod -G wheel fang
[root@bogon ~]# sed -i '6s/^#//g' /etc/pam.d/su
[root@bogon ~]# grep wheel /etc/pam.d/su #只有WHEEL组的可以su
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
# Uncomment the following line to require a user to be in the "wheel" group.
auth required pam_wheel.so use_uid

扩展:为用户fang添加sudo,除关机外的其他所有操作:
# visudo
Cmnd_Alias SHUTDOWN = /sbin/halt, /sbin/shutdown, /sbin/poweroff, /sbin/reboot, /sbin/init
fang ALL=(ALL) ALL,!SHUTDOWN
%wheel ALL=(ALL) ALL,!SHUTDOWN #修改wheel组的权限,禁止关机
Defaults logfile=/var/log/sudo.log

5 禁用selinux
[root@bogon ~]# grep -i ^selinux /etc/selinux/config
SELINUX=enforcing
SELINUXTYPE=targeted
[root@bogon ~]# sed -i '/^SELINUX/s/enforcing/disabled/g' /etc/selinux/config
[root@bogon ~]# getenforce
Enforcing

6.关闭防火墙安装iptables
systemctl stop firewalld.service 
systemctl disable firewalld.service
yum install iptables-services -y #安装

7.修改主机名
[root@bogon ~]# hostnamectl set-hostname hequan.com
[root@bogon ~]# hostname
hequan.com

8.查看并管理服务
[root@hequan ~]# systemctl -t service
[root@hequan ~]# systemctl list-unit-files -t service
yum install -y bash-completion #补全服务名称,退出bash再进就可以

9.设置字符集
[root@hequan ~]# echo $LANG
zh_CN.UTF-8
[root@hequan ~]# vi /etc/locale.conf
LANG="en_US.UTF-8"
[root@hequan ~]# source /etc/locale.conf

10.yum
yum install gcc cmake bzip2-devel curl-devel db4-devel libjpeg-devel libpng-devel freetype-devel libXpm-devel gmp-devel libc-client-devel openldap-devel unixODBC-devel postgresql-devel sqlite-devel aspell-devel net-snmp-devel libxslt-devel libxml2-devel pcre-devel mysql-devel pspell-devel libmemcached libmemcached-devel zlib-devel vim wget lrzsz tree
安装163源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
cd /etc/yum.repos.d/
wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean all
yum makecache
   
其他
yum -y install yum-plugin-priorities ##安装优先级插件
sed -i -e "s/\]$/\]\npriority=1/g" /etc/yum.repos.d/CentOS-Base.repo ##设置基本yum源的优先级为1
yum -y install epel-release ##安装epel源
sed -i -e "s/\]$/\]\npriority=5/g" /etc/yum.repos.d/epel.repo ##设置优先级为5
sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo ##禁用epel源
yum -y install http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm ##安装rpmforge的源
sed -i -e "s/\]$/\]\npriority=10/g" /etc/yum.repos.d/rpmforge.repo ##设置优先级为10
sed -i -e "s/enabled = 1/enabled = 0/g" /etc/yum.repos.d/rpmforge.repo ##禁用yum源
使用方法:yum --enablerepo=rpmforge install [Package]

安装epel源
yum -y install epel-release


11 配置sshd

ListenAddress 0.0.0.0 ##只启用ipv4
/etc/ssh/sshd_config

PermitRootLogin no ##禁止root使用ssh登录
MaxAuthTries 2 ##两次不行就切断重新SSH启动登入

PubkeyAuthentication no ##禁止证书登陆AuthorizedKeysFile /dev/null
ChallengeResponseAuthentication yes禁止SSH客户端记住密码

加快ssh登录速度
GSSAPIAuthentication no ##关闭 GSSAPIUseDNS no ##禁止DNS 反向解析
一般 SSH 依次进行的认证方法的是 publickey, gssapi-keyex, gssapi-with-mic, password, 这个你可以ssh -v开启 debug 模式在连接日志看到。   一般用户只使用 password 认证方式,但前面 3 个认证过程系统还是会尝试,这就浪费时间了,也就造成 SSH 登录慢

AllowTcpForwarding no禁止使用SSH映射Socks5翻墙等等


12 加大打开文件数的限制(open files)
ulimit -n
ulimit -a
vi /etc/security/limits.conf
最后添加
* soft nofile 1024000
* hard nofile 1024000
hive - nofile 1024000
hive - nproc 1024000

用户进程限制
# sed -i 's#4096#65535#g' /etc/security/limits.d/20-nproc.conf #加大普通用户限制 也可以改为unlimited
# egrep -v "^$|^#" /etc/security/limits.d/20-nproc.conf 
* soft nproc 65535
root soft nproc unlimited

reboot

13 优化内核
cat /etc/sysctl.conf
#CTCDN系统优化参数
#关闭ipv6节省系统资源
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
#决定检查过期多久邻居条目
net.ipv4.neigh.default.gc_stale_time=120
#使用arp_announce / arp_ignore解决ARP映射问题
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
# 避免放大攻击
net.ipv4.icmp_echo_ignore_broadcasts = 1
# 开启恶意icmp错误消息保护
net.ipv4.icmp_ignore_bogus_error_responses = 1
#关闭路由转发
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
#开启反向路径过滤
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
#处理无源路由的包
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
#关闭sysrq功能
kernel.sysrq = 0
#core文件名中添加pid作为扩展名
kernel.core_uses_pid = 1
# 开启SYN洪水攻击保护
net.ipv4.tcp_syncookies = 1
#修改消息队列长度
kernel.msgmnb = 65536
kernel.msgmax = 65536
#设置最大内存共享段大小bytes
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
#timewait的数量,默认180000
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
#每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目
net.core.netdev_max_backlog = 262144
#限制仅仅是为了防止简单的DoS 攻击
net.ipv4.tcp_max_orphans = 3276800
#未收到客户端确认信息的连接请求的最大值
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
#内核放弃建立连接之前发送SYNACK 包的数量
net.ipv4.tcp_synack_retries = 1
#内核放弃建立连接之前发送SYN 包的数量
net.ipv4.tcp_syn_retries = 1
#启用timewait 快速回收
net.ipv4.tcp_tw_recycle = 1
#开启重用。允许将TIME-WAIT sockets 重新用于新的TCP 连接
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
#当keepalive 起用的时候,TCP 发送keepalive 消息的频度。缺省是2 小时
net.ipv4.tcp_keepalive_time = 1800
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
#允许系统打开的端口范围
net.ipv4.ip_local_port_range = 1024 65000
#修改防火墙表大小,默认65536
net.netfilter.nf_conntrack_max=655350
net.netfilter.nf_conntrack_tcp_timeout_established=1200
# 确保无人能修改路由表
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
sysctl -p #生效

14 时间设置
yum install chrony
vi /etc/chrony.conf
server 0.centos.pool.ntp.org
server 3.europe.pool.ntp.org

systemctl enable chronyd.service
systemctl start chronyd.service

timedatectl set-timezone Asia/Shanghai
timedatectl set-time "2015-01-21 11:50:00"(可以只修改其中一个)修改日期时间
timedatectl 查看时间状态
chronyc sources -v 查看时间同步源
chronyc sourcestats -v
hwclock -w #先同步一遍时间到硬件时间


15、远程5分钟无操作自动注销:
vim /etc/profile
最后添加:
export TMOUT=300   ---5分钟自动注销下来
找到
HISTSIZE=1000
修改为:
HISTSIZE=100 --减少日记字节为100KB,太大内容过多容易漏重要信息.
HISTTIMEFORMAT=”%Y-%m-%d %H:%M:%S”


16.禁止IP伪装
vim /etc/host.conf
在里面加上:
nospoof on

17临时文件安全
临时文件不应该有执行权限
/tmp
/dev/sda3 /tmp ext4 nosuid,noexec,nodev,rw 0 0
同时使用符号连接将/var/tmp 指向 /tmp
/dev/shm
none /dev/shm tmpfs defaults,nosuid,noexec,rw 0 0



18 PAM 插件认证加固配置
配置文件
ls /etc/pam.d/chfn crond login passwd remote runuser-l smtp ssh-keycat sudo-i system-auth-acchsh fingerprint-auth newrole password-auth run_init smartcard-auth smtp.postfix su su-lconfig-util fingerprint-auth-ac other password-auth-ac runuser smartcard-auth-ac sshd sudo system-auth
认证插件
ls /lib64/security/
18.1. pam_tally2.so
此模块的功能是,登陆错误输入密码3次,5分钟后自动解禁,在未解禁期间输入正确密码也无法登陆。
在配置文件 /etc/pam.d/sshd 顶端加入
auth required pam_tally2.so deny=3 onerr=fail unlock_time=300
查看失败次数
# pam_tally2Login Failures Latest failure Fromroot 14 07/12/13 15:44:37 192.168.6.2neo 8 07/12/13 15:45:36 192.168.6.2
重置计数器
# pam_tally2 -r -u rootLogin Failures Latest failure Fromroot 14 07/12/13 15:44:37 192.168.6.2# pam_tally2 -r -u neoLogin Failures Latest failure Fromneo 8 07/12/13 15:45:36 192.168.6.2
pam_tally2 计数器日志保存在 /var/log/tallylog 注意,这是二进制格式的文件
例 1. /etc/pam.d/sshd - pam_tally2.so
# cat /etc/pam.d/sshd#%PAM-1.0auth required pam_tally2.so deny=3 onerr=fail unlock_time=300auth required pam_sepermit.soauth include password-authaccount required pam_nologin.soaccount include password-authpassword include password-auth# pam_selinux.so close should be the first session rulesession required pam_selinux.so closesession required pam_loginuid.so# pam_selinux.so open should only be followed by sessions to be executed in the user contextsession required pam_selinux.so open env_paramssession optional pam_keyinit.so force revokesession include password-auth


以上配置root用户不受限制, 如果需要限制root用户,参考下面
auth required pam_tally2.so deny=3 unlock_time=5 even_deny_root root_unlock_time=1800


18.2. pam_listfile.so
用户登陆限制
将下面一行添加到 /etc/pam.d/sshd 中,这里采用白名单方式,你也可以采用黑名单方式
auth required pam_listfile.so item=user sense=allow file=/etc/ssh/whitelist onerr=fail


将允许登陆的用户添加到 /etc/ssh/whitelist,除此之外的用户将不能通过ssh登陆到你的系统
# cat /etc/ssh/whitelistneowww


例 2. /etc/pam.d/sshd - pam_listfile.so
# cat /etc/pam.d/sshd#%PAM-1.0auth required pam_listfile.so item=user sense=allow file=/etc/ssh/whitelist onerr=failauth required pam_tally2.so deny=3 onerr=fail unlock_time=300auth required pam_sepermit.soauth include password-authaccount required pam_nologin.soaccount include password-authpassword include password-auth# pam_selinux.so close should be the first session rulesession required pam_selinux.so closesession required pam_loginuid.so# pam_selinux.so open should only be followed by sessions to be executed in the user contextsession required pam_selinux.so open env_paramssession optional pam_keyinit.so force revokesession include password-auth

sense=allow 白名单方式, sense=deny 黑名单方式
auth required pam_listfile.so item=user sense=deny file=/etc/ssh/blacklist onerr=fail
更多细节请查看手册 $ man pam_listfile
18.3. pam_access.so
编辑 /etc/pam.d/sshd 文件,加入下面一行
account required pam_access.so
保存后重启sshd进程

编辑 /etc/security/access.conf 文件cat >> /etc/security/access.conf << EOF- : root : ALL EXCEPT 192.168.6.1EOF
只能通过 192.168.6.1 登陆, 添加多个IP地址
- : root : ALL EXCEPT 192.168.6.1 192.168.6.2
测试是否生效


18.4. pam_wheel.so
限制普通用户通过su命令提升权限至root. 只有属于wheel组的用户允许通过su切换到root用户
编辑 /etc/pam.d/su 文件,去掉下面的注释
auth required pam_wheel.so use_uid
修改用户组别,添加到wheel组
# usermod -G wheel www# id wwwuid=501(www) gid=501(www) groups=501(www),10(wheel)
没有加入到wheel组的用户使用su时会提示密码不正确。
$ su - rootPassword:su: incorrect password

18.5设定用户登录,普通用户登录识别超过6次锁定300s.
echo "account required pam_tally.so deny=100 no_magic_root reset" >>/etc/pam.d/system-auth
echo "auth required pam_tally.so onerr=fail deny=6 unlock_time=300" >>/etc/pam.d/system-auth