配置Yum源
参考国内清华Yum源仓库地址
备份原有repo仓库配置文件
# mkdir -p /etc/yum.repos.d/backup && \
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ && \
mv /etc/yum.repos.d/backup/CentOS-Base.repo /etc/yum.repos.d/
配置国内清华仓库地址
# sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
-i.bak /etc/yum.repos.d/CentOS-*.repo
更新软件包缓存
# yum makecache
注意,如果需要启用其中一些 repo,需要将其中的 enabled=0 改为 enabled=1
关闭Selinux
# sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config && setenforce 0
修改系统时区为-Shanghai
- 查询系统时区
# timedatectl |grep "Time zone"
Time zone: America/New_York (EDT, -0400)
- 修改系统时区
# timedatectl list-timezones #列出所有时区
# timedatectl set-timezone Asia/Shanghai #设置系统时区为上海
- 验证修改生效
# timedatectl |grep "Time zone"
Time zone: Asia/Shanghai (CST, +0800)
时间同步ntp
- 安装同步工具ntp
# yum install ntp
- 配置ntp指向国内时间服务器
# vim /etc/ntp.conf
server ntp.ntsc.ac.cn iburst
- 启动ntp服务
# systemctl start ntpd
- 验证时间
# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*114.118.7.161 .OLEG. 1 u 43 64 1 40.157 3.016 2.970
- 查看是否应用生效
# timedatectl |grep NTP
NTP enabled: yes
NTP synchronized: yes
- 同步BIOS时间
# clock -r # 查看Bios时间
# clock -w # 同步系统时间到硬件
- 时间管理常用命令
# ntptime
# ntpstat
# timedatectl
ulimit设置
ulimit用于限制每个用户可使用的资源,如CPU、内存、句柄等。Linux系统默认open files数目为1024,有些应用程序提示Too many open files错误,是应为open files资源不够,这就需要ulimit调优。例如大量的静态文件访问的web服务器和缓存服务器,就特别需要注意。
- 查看ulimit信息
# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 31155
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 31155
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
- 临时设置ulimit
# ulimit -n 102400 //这只是在当前终端有效,退出之后,open files又变为默认值。当然也可以写到/etc/profile中,因为每次登录终端时,都会自动执行/etc/profile
- 永久设置ulimit
# vim /etc/security/limits.conf //加入以下配置,重启即可生效
* hard nofile 102400
* soft nofile 102400
* soft nproc 102400
* hard nproc 102400
# systcl -p # 加载内核临时生效
TCP优化
Linux之TCP调优参考
以下配置均在/etc/sysctl.conf
- 关闭ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
- 避免放大攻击
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 = 30
- 允许系统打开的端口范围
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
NetworkManager
在CentOS系统上,目前有NetworkManager和network两种网络管理工具。如果两种都配置会引起冲突,而且NetworkManager在网络断开的时候,会清理路由,如果一些自定义的路由,没有加入到NetworkManager的配置文件中,路由就被清理掉,网络连接后需要自定义添加上去
推荐关闭NetworkManager
# systemctl disable --now NetworkManager
Ping管理
下面列举禁止主机被ping,具体根据自己情况而定
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
用户提权sudo
普通用户临时使用root用户的权限,在无需提供root认证方式的情况下,通过控制用户权限文件/etc/sudoers来实现
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
zhangsan ALL=(ALL) NOPASSWD: ALL