侧边栏壁纸
博主头像
蔚然小站博主等级

未来会有的,不要辜负了梦想

  • 累计撰写 36 篇文章
  • 累计创建 14 个标签
  • 累计收到 9 条评论

目 录CONTENT

文章目录

ubuntu服务优化

皮蛋熊
2023-08-24 / 0 评论 / 0 点赞 / 142 阅读 / 6163 字
温馨提示:
本文最后更新于 2023-08-26,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

ubuntu 开机时候会启动很多服务,作为不同用途的机器,很多服务我们都用不到,在后台一直运行总觉得不舒服,所以可以使用下面的办法进行管理。

罗列出开机启动的服务进程:

$ systemctl list-unit-files --type=service | grep enabled

accounts-daemon.service                    enabled         enabled      
acpid.service                              disabled        enabled      
alsa-restore.service                       static          enabled      
alsa-state.service                         static          enabled      
alsa-utils.service                         masked          enabled      
anacron.service                            enabled         enabled      
apparmor.service                           enabled         enabled      
apport-autoreport.service                  static          enabled      
[email protected]                    static          enabled      
apport.service                             generated       enabled      
apt-daily-upgrade.service                  static          enabled      
apt-daily.service                          static          enabled      
apt-news.service                           static          enabled      
atop.service                               enabled         enabled      
atopacct.service                           enabled         enabled      
[email protected]                            enabled         enabled      
avahi-daemon.service                       enabled         enabled      
blk-availability.service                   enabled         enabled      
bluetooth.service                          enabled         enabled      
bolt.service                               static          enabled
...

这其中的服务我们有些根本用不到,可以使用下面的命令进行关闭:

sudo systemctl stop bluetooth.service
sudo systemctl disable bluetooth.service

检查禁用的状态:

sudo systemctl status bluetooth.service

这些停用的服务进程依然有可能会被其他服务所启动,通过 mask 可以组织任何进程启动他:

$ sudo systemctl mask bluetooth.service
Created symlink from /etc/systemd/system/bluetooth.service to /dev/null.

静态服务 (static) 是其他服务的依赖,所以不能操作:

root@jet-dev:~# systemctl list-unit-files --type=service    
UNIT FILE                                  STATE           VENDOR PRESET
accounts-daemon.service                    enabled         enabled      
acpid.service                              disabled        enabled      
alsa-restore.service                       static          enabled      
alsa-state.service                         static          enabled      
alsa-utils.service                         masked          enabled      
anacron.service                            enabled         enabled      
apparmor.service                           enabled         enabled      
apport-autoreport.service                  static          enabled      
[email protected]                    static          enabled      
apport.service                             generated       enabled  
......

这里举例一些服务的说明,其中有些可以卸载,有些则不可以关闭。

  • accounts-daemon. service 是一个潜在的安全风险。它是 AccountsService 的一部分,AccountsService 允许程序获得或操作用户账户信息。我不认为有好的理由能使我允许这样的后台操作,所以我选择掩盖_mask_该服务进程。
  • avahi-daemon.service 用于零配置网络发现,使电脑超容易发现网络中打印机或其他的主机,我总是禁用它,别漏掉它。
  • brltty.service 提供布莱叶盲文设备支持,例如布莱叶盲文显示器。
  • debug-shell.service 开放了一个巨大的安全漏洞(该服务提供了一个无密码的 root shell ,用于帮助 调试 systemd 问题),除非你正在使用该服务,否则永远不要启动服务。
  • ModemManager.service 该服务是一个被 dbus 激活的守护进程,用于提供移动宽频_broadband_(2G/3G/4G)接口,如果你没有该接口,无论是内置接口,还是通过如蓝牙配对的电话,以及 USB 适配器,那么你也无需该服务。
  • pppd-dns.service 是一个计算机发展的遗物,如果你使用拨号接入互联网的话,保留它,否则你不需要它。
  • rtkit-daemon.service 听起来很可怕,听起来像是 rootkit。 但是你需要该服务,因为它是一个实时内核调度器_real-time kernel scheduler_。
  • whoopsie.service 是 Ubuntu 错误报告服务。它用于收集 Ubuntu 系统崩溃报告,并发送报告到 https://daisy.ubuntu.com 。 你可以放心地禁止其启动,或者永久的卸载它。
  • wpa_supplicant.service 仅在你使用 Wi-Fi 连接时需要。

systemd 提供了一些命令可以查看开机启动的 log,用于排查存在哪些问题:

journalctl -b

journalctl -b 1 : 可以查看前一次启动的 log,journalctl -b 2 可以查看倒数第二次的启动 log。当然这也包含我们 mtgpu 的日志。

systemd 系统还提供了一些过滤器, 用于锁定目标,可以查看 log 信息中确定什么被启动,或者是什么正在尝试启动:

$ journalctl _PID=1   # 进程号为1,表示所有进程的父进程。

一个最有用的命令工具之一 systemd-analyze blame,用于帮助查看哪个服务进程启动耗时最长。

$ systemd-analyze blame
8.708s gpu-manager.service
8.002s NetworkManager-wait-online.service
5.791s mysql.service
2.975s dev-sda3.device
1.810s alsa-restore.service
1.806s systemd-logind.service
1.803s irqbalance.service
1.800s lm-sensors.service
1.800s grub-common.service

参考教程

  1. Linux 系统开机启动项清理
  2. Understanding and Using Systemd
  3. Intro to Systemd Runlevels and Service Management Commands
  4. Here We Go Again, Another Linux Init: Intro to systemd
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区