一键安装最新内核并开启 BBR 脚本

技术 秋水逸冰 2481325浏览 717评论

最近,Google 开源了其 TCP BBR 拥塞控制算法,并提交到了 Linux 内核,从 4.9 开始,Linux 内核已经用上了该算法。根据以往的传统,Google 总是先在自家的生产环境上线运用后,才会将代码开源,此次也不例外。
根据实地测试,在部署了最新版内核并开启了 TCP BBR 的机器上,网速甚至可以提升好几个数量级。
于是我根据目前三大发行版的最新内核,开发了一键安装最新内核并开启 TCP BBR 脚本。

本脚本适用环境

系统支持:CentOS 6+,Debian 8+,Ubuntu 16+
虚拟技术:OpenVZ 以外的,比如 KVM、Xen、VMware
内存要求:≥128M
日期  :2022 年 5 月 11 日

关于本脚本

1、本脚本已在 Vultr 上的 VPS 全部测试通过。
2、当脚本检测到 VPS 的虚拟方式为 OpenVZ 时,会提示错误,并自动退出安装。
3、脚本运行完重启发现开不了机的,打开 VPS 后台控制面板的 VNC, 开机卡在 grub 引导, 手动选择内核即可。
4、由于是使用最新版系统内核,最好请勿在生产环境安装,以免产生不可预测之后果。

使用方法

使用root用户登录,运行以下命令:

wget --no-check-certificate -O /opt/bbr.sh https://github.com/teddysun/across/raw/master/bbr.sh
chmod 755 /opt/bbr.sh
/opt/bbr.sh

安装完成后,脚本会提示需要重启 VPS,输入 y 并回车后重启。
重启完成后,进入 VPS,验证一下是否成功安装最新内核并开启 TCP BBR,输入以下检查:
No.1

uname -r

查看内核版本,显示为新版内核就表示 OK 了。
No.2

sysctl net.ipv4.tcp_available_congestion_control

返回值一般为:

net.ipv4.tcp_available_congestion_control = bbr cubic reno

或者:

net.ipv4.tcp_available_congestion_control = reno cubic bbr

No.3

sysctl net.ipv4.tcp_congestion_control

返回值一般为:

net.ipv4.tcp_congestion_control = bbr

No.4

sysctl net.core.default_qdisc

返回值一般为:

net.core.default_qdisc = fq

No.5

lsmod | grep bbr

返回值有 tcp_bbr 模块即说明 bbr 已启动。比如:

tcp_bbr                20480  3

注意:并不是所有的 VPS 都会有此返回值,若没有也属正常。

特别说明

如果你使用的是 Google Cloud Platform (GCP)更换内核,有时会遇到重启后,整个磁盘变为只读的情况。只需执行以下命令即可恢复:

mount -o remount rw /

 

更新日志:

2021 年 5 月 11 日:
1、在 CentOS 7 下更改为安装 5.15.38 版本内核;
2、精简在 Debian 和 Ubuntu 下的可选内核版本,从 5.15 起始到当前最新版结束。

2021 年 1 月 3 日:
1、为了稳定性,在 CentOS 7 下更改 elrepo-kernel 为 centos-release-xen-48,并升级安装 4.9 版稳定内核;
2、在 CentOS 6 和 CentOS 8 下不再安装 elrepo-kernel。原因:CentOS 6 是通过 rpm 安装 4.18.20;CentOS 8 则无需升级内核即可开启 BBR。
3、精简在 Debian 和 Ubuntu 下的可选内核版本,从 5.9 起始到当前最新版结束。

2020 年 6 月 23 日:
1、重构了代码,去除一些不必要的处理;
2、精简在 Debian 和 Ubuntu 下的可选内核版本,从 5.6 起始到当前最新版结束。

2018 年 12 月 14 日:
1、修正:在 CentOS 6 系统下 kernel-ml 及 kernel-ml-devel 无法安装的问题。原因是 elrepo 官方最近取消了 kernel-ml 及 kernel-ml-devel 的 rpm 资源。脚本已改为下载 rpm 来安装的方式。版本 4.18.20 将会是 CentOS 6 下最新的内核,目测以后也不会再有升级了,请尽快迁移到 CentOS 7 系统。

2018 年 06 月 09 日:
1、修正:在 CentOS 系统下将 elrepo-kernel 默认启用,以解决在安装新内核后执行 yum update 再次安装旧内核的问题。

2018 年 05 月 12 日:
1、修正:在 Ubuntu 或 Debian 系统中某些情况下获取不到某个编译失败的安装包导致退出安装的问题。并且现在可以手动选择安装从 4.14 到最新版本的内核。

2018 年 02 月 24 日:
1、新增:手动安装最新版内核 headers 的教程。

2017 年 10 月 28 日:
1、更新:自动判断内核版本。如果内核版本已经大于或等于 4.9 且没有开启 BBR,脚本会直接设置好 BBR;
2、新增:脚本执行时会在当前目录下创建日志(install_bbr.log),方便查看及排错。

2017 年 02 月 22 日:
1、更新:自动获取最新版 4.10 内核。

2017 年 01 月 20 日:
1、新增:自动获取并下载安装最新版内核的功能。

2017 年 01 月 13 日:
1、更新:内核到 4.9.3。

2017 年 01 月 09 日:
1、更新:内核到 4.9.1。

参考链接:

https://github.com/google/bbr/blob/master/Documentation/bbr-quick-start.md
http://elrepo.org/tiki/tiki-index.php
http://kernel.ubuntu.com/~kernel-ppa/mainline/

转载请注明:秋水逸冰 » 一键安装最新内核并开启 BBR 脚本

发表我的评论
取消评论

请输入正确答案后提交评论 *超出时限。 请再次填写验证码。

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (717)

  1. 大佬 你的docker xray可以启用ipv6吗?我弄了半天没弄出来
    Joyace3年前(2021-10-10)回复
  2. 能不能不要用eol内核,老是断网死机,系统崩溃,ping 8.8.8.8都没网络,只能重启后恢复,听别人说要用stable 5.12的内核 然后前段时间我把所有服务器全重装了,也可能是我的流量比较大。。一天几十个亿访问量 我跑梯子的bbr还没任何事情,这种挂BT种子,跑梯子的开bbr一点毛病都没有 https://ldbbs.ldmnq.com/bbs/topic/attachment/2021-6/fdb7825b-5178-454f-83b0-f8d6101bf89b.png
    小樱3年前(2021-06-09)回复
  3. 小内存VPS在升级5.x之后会kernel panic。所以只能手动把脚本那行">5.9"改回">4.15"来用回4.15版本的内核
    charl3年前(2021-04-27)回复
    • 不一定是小内存的原因,好像跟VPS提供商有关,架构比较老旧升级内核就容易出现这个问题,另外一般是装debian/ubuntu的vps容易出这个问题,centos好像没发现过
      lalalala3年前(2021-07-06)回复
  4. 更换后内核不稳定,程序无法正常运行,有办法还原吗?还是得重装系统
    小樱3年前(2021-04-13)回复
  5. 强烈要求增加个版本范围选择的选项,ubuntu 16 i386好像不支持最新的内核,看了下地址,最新支持到5.2.21
    MiaoSKY3年前(2021-04-06)回复
  6. 现在出了bbr plus 望更新!
    stephen3年前(2021-03-17)回复
  7. [root@VM-0-2-centos ~]# cat /etc/redhat-release;cat /proc/version;sysctl net.ipv4.tcp_available_congestion_control;sysctl net.ipv4.tcp_congestion_control;sysctl net.core.default_qdisc;lsmod | grep bbr CentOS Linux release 7.6.1810 (Core) Linux version 3.10.0-1062.18.1.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 17 23:49:17 UTC 2020 net.ipv4.tcp_available_congestion_control = cubic reno net.ipv4.tcp_congestion_control = cubic net.core.default_qdisc = fq [root@VM-0-2-centos ~]# cat /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). # Controls IP packet forwarding net.ipv4.ip_forward = 0 # Controls source route verification net.ipv4.conf.default.rp_filter = 1 # Do not accept source routing net.ipv4.conf.default.accept_source_route = 0 # Controls the System Request debugging functionality of the kernel # Controls whether core dumps will append the PID to the core filename. # Useful for debugging multi-threaded applications. kernel.core_uses_pid = 1 # Controls the use of TCP syncookies net.ipv4.tcp_syncookies = 1 # Controls the maximum size of a message, in bytes kernel.msgmnb = 65536 # Controls the default maxmimum size of a mesage queue kernel.msgmax = 65536 net.ipv4.conf.all.promote_secondaries = 1 net.ipv4.conf.default.promote_secondaries = 1 net.ipv6.neigh.default.gc_thresh3 = 4096 net.ipv4.neigh.default.gc_thresh3 = 4096 kernel.softlockup_panic = 1 kernel.sysrq = 1 net.ipv6.conf.all.disable_ipv6=0 net.ipv6.conf.default.disable_ipv6=0 net.ipv6.conf.lo.disable_ipv6=0 kernel.numa_balancing = 0 kernel.shmmax = 68719476736 kernel.printk = 5 net.core.default_qdisc = fq net.ipv4.tcp_congestion_control = bbr
    小樱4年前(2020-10-08)回复
  8. 听说 5.x 内核比 4.x 内核的 bbr 效果有一定提升,是这样吗?
    qianxu4年前(2020-09-15)回复
  9. 震惊 这个文章居然还在更新(
    Yellow Object4年前(2020-06-23)回复
  10. Pretty great post. I simply stumbled upon your weblog and wished to mention that I have truly enjoyed browsing your blog posts. In any case I will be subscribing in your feed and I'm hoping you write once more very soon!
    whisper4年前(2020-04-28)回复
  11. 建站别装,特别是那种像hls视频推流的,文件越碎越慢
    oimo4年前(2020-03-12)回复
  12. 甲骨文的centos7现在安装5.5.7内核后会断联,请求大佬能增加个手动选择内核版本的选项。 已经执行了grub2-mkconfig -o /boot/grub2/grub.cfg
    寒夜方舟4年前(2020-03-05)回复
    • 5.5.8 已经出来了,你可以试试看这个版本的。 脚本里没法选择具体版本,这个是由 elrepo 控制的。
      秋水逸冰4年前(2020-03-07)回复
    • 最新的5.6.12-1已经没有这个问题了
      alex4年前(2020-05-13)回复
  13. --2020-02-12 20:20:10-- http://repos.lax.quadranet.com/elrepo/archive/kernel/el6/x86_64/RPMS/kernel-ml-4.18.20-1.el6.elrepo.x86_64.rpm 正在解析主机 repos.lax.quadranet.com... 198.55.111.5, 2607:fcd0:0:a::2 正在连接 repos.lax.quadranet.com|198.55.111.5|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 404 Not Found 2020-02-12 20:20:15 错误 404:Not Found。
    小樱4年前(2020-02-12)回复
    • 大佬怎么在这,好耶
      365cent4年前(2020-06-18)回复
  14. 请问怎么卸载。
    csj4年前(2020-01-19)回复
  15. 2020-01-05 10:24:56 (759 KB/s) - `linux-image-5.4.8-amd64.deb' saved [8764096/8764096] dpkg-deb: error: archive 'linux-modules-5.4.8-amd64.deb' contains not understood data member control.tar.xz, giving up dpkg: error processing linux-modules-5.4.8-amd64.deb (--install): subprocess dpkg-deb --control returned error exit status 2 Errors were encountered while processing: linux-modules-5.4.8-amd64.deb dpkg-deb: error: archive 'linux-image-5.4.8-amd64.deb' contains not understood data member control.tar.xz, giving up dpkg: error processing linux-image-5.4.8-amd64.deb (--install): subprocess dpkg-deb --control returned error exit status 2 Errors were encountered while processing: linux-image-5.4.8-amd64.deb Generating grub.cfg ... Found linux image: /boot/vmlinuz-3.2.0-4-amd64 Found initrd image: /boot/initrd.img-3.2.0-4-amd64 done Info: The system needs to reboot. Do you want to restart system? [y/n] Debian 7
    aaa4年前(2020-01-05)回复
    • Debian 7 应该是不能安装这么新的内核。 要么换系统,要么换个稍微旧一点的内核。
      秋水逸冰4年前(2020-01-07)回复