内核升级
centos7
参考链接:CentOS-7 升级内核 - 知乎
1、查看系统版本
cat /etc/redhat-release
2、查看当前内核版本
uname -r
3、检查是否安装ELRepo
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
已加载插件:fastestmirror
Error getting repository data for elrepo-kernel, repository not found
# 看到error说明没有安装ELRepo
4、升级安装ELRepo:http://elrepo.org/tiki/HomePage
更新yum源仓库
yum -y update
载入ELRepo仓库的公共密钥
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
或者:
#安装ELRepo仓库的yum源
yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
# 或升级
rpm -Uvh https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
5、再次查看可用安装包
#查看可用的系统内核包
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
# 长期维护版本为lt,最新主线稳定版为ml
6、安装最新的内核
# 我这里选择的是长期维护版本kernel-lt 如需更新最新稳定版选择kernel-ml
yum --enablerepo=elrepo-kernel install -y kernel-lt
......
正在安装 : kernel-lt-5.4.108-1.el7.elrepo.x86_64
......
已安装:
kernel-lt.x86_64 0:5.4.108-1.el7.elrepo
7、查看可用内核版本及启动顺序
awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg
0 : CentOS Linux (5.4.108-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1160.11.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-1160.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-20210128140208453518997635111697) 7 (Core)
8、安装辅助工具(非必须,有些系统自带该工具):grub2-pc
yum install -y grub2-pc
9、设置内核默认启动顺序
grub2-set-default 0
或者
grub-set-default 'CentOS Linux (5.4.96-1.el7.elrepo.x86_64) 7 (Core)'
# CentOS Linux (5.4.96-1.el7.elrepo.x86_64) 7 (Core) 这个是具体的版本
10、vim /etc/default/grub
#编辑/etc/default/grub文件
vim /etc/default/grub
设置 GRUB_DEFAULT=0 # 只需要修改这里即可
GRUB_TIMEOUT=1
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved #---将这里的saved修改成0----
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto spectre_v2=retpoline rhgb quiet net.ifnames=0 console=tty0 console=ttyS0,115200n8 noibrs"
GRUB_DISABLE_RECOVERY="true"
11、生成grub 配置文件
# 运行grub2-mkconfig命令来重新创建内核配置
grub2-mkconfig -o /boot/grub2/grub.cfg
12、重启系统
reboot
# 或者
shutdown -r now
#重启完成后,查看内核版本是否正确
uname -r
13、查看系统中已安装的内核
rpm -qa | grep kernel
kernel-devel-3.10.0-1160.11.1.el7.x86_64
kernel-tools-3.10.0-1160.11.1.el7.x86_64
kernel-3.10.0-1160.el7.x86_64
kernel-3.10.0-1160.11.1.el7.x86_64
kernel-lt-5.4.108-1.el7.elrepo.x86_64
kernel-tools-libs-3.10.0-1160.11.1.el7.x86_64
kernel-headers-3.10.0-1160.11.1.el7.x86_64
14、删除旧内核,这一步是【可选】的
yum remove -y kernel-devel-3.10.0 kernel-3.10.0 kernel-headers-3.10.0
# 查看已安装内核
rpm -qa | grep kernel
# 也可以安装 yum-utils 工具,当系统安装的内核大于3个时,会自动删除旧的内核版本
yum install -y yum-utils
13 、升级内核工具包
# 删除旧版本工具包--可选
yum remove kernel-tools-libs.x86_64 kernel-tools.x86_64
# 安装新版本工具包
yum --disablerepo=\* --enablerepo=elrepo-kernel install -y kernel-lt-tools.x86_64
# 查看已安装内核
rpm -qa | grep kernel
centos8
第一步:升级系统软件
先升级一下系统的原有软件,因为后续可能有些步骤可能会依赖于一些已经安装的软件,减少后续的麻烦,所以需要执行一下升级操作:
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
dnf update -y
第二步:添加 elrepo 源
要升级内核,肯定要有源,这里我使用的是 elrepo 源,通过以下步骤添加:
-
添加 elrepo 的源
yum install -y https://www.elrepo.org/elrepo-release-8.el8.elrepo.noarch.rpm
-
添加 elrepo 的 gpg key
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
-
查看是否添加成功
yum repolist | grep elrepo
第三步:安装新内核
安装新的内核(lt不指定版本号,用最新的),在使用的时候需要指定一下源:
dnf --enablerepo="elrepo-kernel" install -y kernel-lt
安装完毕之后就可以了
第四步:设置 grub2
在第三步我已经安装好了新的内核,现在可以查看一下系统中的所有内核:
grubby --info=ALL | grep ^kernel
kernel="/boot/vmlinuz-4.18.0-240.1.1.el8_3.x86_64"
kernel="/boot/vmlinuz-5.4.228-1.el8.elrepo.x86_64"
kernel="/boot/vmlinuz-4.18.0-348.7.1.el8_5.x86_64"
grubby --default-kernel
/boot/vmlinuz-5.4.228-1.el8.elrepo.x86_64
我这里已经设置了新的内核为默认启动的,你如果按照这个步骤安装发现新的内核不是默认的,那么需要以这个步骤设置:
grubby --set-default=/boot/vmlinuz-5.4.228-1.el8.elrepo.x86_64
这个命令的后面的字符串 /boot/vmlinuz-5.4.228-1.el8.elrepo.x86_64
就是设置哪个内核为默认内核,然后你重启系统,你会发现如果不手动干预的情况下,系统会默认从新的内核启动。
启动之后,查看一下当前的内核版本:
uname -msr
Linux 5.4.228-1.el8.elrepo.x86_64 x86_64
第五步:删除旧内核(可选)
How to Remove Old Linux Kernels in CentOS/RHEL 8 | CentLinux
dnf -y remove --oldinstallonly --setopt installonly_limit=3 kernel
配置用户资源限制
1、查看进程的限制
cat /proc/pid/limits
/etc/security/limits.conf
是用于配置用户资源限制的文件。编辑配置文件/etc/security/limits.conf,添加或修改内容(默认65535太小)
vim /etc/security/limits.conf
* soft nofile 1024000
* hard nofile 1024000
* soft nproc 1024000
* hard nproc 1024000
* soft memlock unlimited
* hard memlock unlimited
* soft nofile 1024000
:
*
表示匹配所有用户。soft
表示软限制,用户可以在特定会话中超过这个限制,但系统会记录警告。nofile
表示文件描述符的限制,这控制了一个用户可以同时打开的文件数。1024000
是软限制的值,这里设置为 1024000 个文件描述符。
* hard nofile 1024000
:
- 与上面的配置类似,但这是硬限制,用户不能超过这个限制。
* soft nproc 1024000
:
nproc
表示进程数的限制,控制一个用户可以同时运行的进程数。1024000
是软限制,用户可以在特定会话中超过这个限制,但系统会记录警告。
* hard nproc 1024000
:
- 与上面的配置类似,但这是硬限制,用户不能超过这个限制。
* soft memlock unlimited
:
memlock
限制了用户可以锁定在内存中的数据量。将其设置为unlimited
表示没有软限制。
* hard memlock unlimited
:
- 与上面的配置类似,但这是硬限制,用户不能超过这个限制。
再打开配置/etc/rc.local,添加如下内容
vim /etc/rc.local
ulimit -SHn 65535
ban恶意攻击ip
1、手动ban恶意ip
查看log
less /var/log/secure
禁用ip,iptables -I是"Insert"(插入)的缩写,用于向防火墙规则链中插入新的规则
iptables -I INPUT -s 87.236.176.193 -j DROP
解禁ip,iptables -D是"Delete"(删除)
iptables -D INPUT -s 87.236.176.193 -j DROP
2、自动ban-
yum install fail2ban
3、定时脚本
系统时间校准
1、cenos8
CentOS 8 已经转入使用 chrony 来进行时间同步,因此 NTP 软件包已经不再包含在默认的软件源中,因此无法使用 yum 命令来安装它。
sudo dnf install chrony
sudo systemctl enable chronyd
sudo systemctl start chronyd
检查 chrony 是否已成功同步了时间:如果输出中的 Last offset 字段为零,则表示 chrony 已成功同步了系统时间。
chronyc tracking
2、centos7
网络优化
1、/etc/sysctl.conf
fs.file-max = 1491124
net.core.default_qdisc=fq
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.core.netdev_max_backlog = 1048576
net.core.somaxconn = 1048576
net.core.rmem_default = 174760
#net.netfilter.nf_conntrack_max=1048576
#net.nf_conntrack_max=1048576
#net.netfilter.nf_conntrack_tcp_timeout_fin_wait=30
#net.netfilter.nf_conntrack_tcp_timeout_time_wait=30
#net.netfilter.nf_conntrack_tcp_timeout_close_wait=15
#net.netfilter.nf_conntrack_tcp_timeout_established=300
vm.zone_reclaim_mode = 0
vm.swappiness = 10
vm.overcommit_memory = 1
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax=68719476736
kernel.printk = 5
kernel.sysrq = 1
kernel.numa_balancing = 0
kernel.softlockup_panic = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 131072
net.ipv4.tcp_max_syn_backlog = 1048576
net.ipv4.conf.all.promote_secondaries = 1
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.neigh.default.gc_thresh3 = 4096
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_max_tw_buckets = 55000
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 15
net.ipv4.tcp_keepalive_time=1200
net.ipv4.tcp_slow_start_after_idle = 0
net.ipv4.tcp_mtu_probing = 1
#net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_rmem = 4096 174760 67108864
net.ipv4.tcp_mem = 131072 262144 524288
net.ipv4.tcp_congestion_control=bbr
net.ipv4.tcp_window_scaling = 1
#net.inet.udp.checksum=1
net.ipv6.neigh.default.gc_thresh3 = 4096
net.ipv6.conf.all.disable_ipv6=0
net.ipv6.conf.default.disable_ipv6=0
net.ipv6.conf.lo.disable_ipv6=0