禁用和设置 Ubuntu MoTD 消息

TL;DR

要关闭 MoTD 消息,有两种方式:

  • 用户级:touch .hushlogin
  • 系统级:sudo chmod -x /etc/update-motd.d/*

介绍

当你通过 SSH 登录 Ubuntu 时,通常会看到这样的消息:

Welcome to Ubuntu 22.04.5 LTS (GNU/Linux 5.15.0-131-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Wed Feb 26 06:00:49 PM UTC 2025

  System load:               37.87
  Usage of /home:            52.2% of 6.15TB
  Memory usage:              10%
  Swap usage:                1%
  Processes:                 1958
  Users logged in:           9
  IPv4 address for enp1s0f0: 192.168.1.2
  IPv6 address for enp1s0f0: f451:340:2001:58b4:8fc1:15fb:1e8c:18b9

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

24 additional security updates can be applied with ESM Apps.
Learn more about enabling ESM Apps service at https://ubuntu.com/esm

New release '24.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


*** System restart required ***

这个东西叫 MoTD(Message of The Day),它允许系统管理员向用户显示公告。不过 Ubuntu 向其中加入了一些自己的东西,比如上面的这部分内容。有时候我们不想看到这些消息,或者只想看到消息的一部分。接下来介绍如何定制 MoTD。

MoTD News

在完整的 MoTD 中,有一小部分,叫 MoTD News,其内容如下:

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

关闭 MoTD News 的方法很简单,只需要编辑 /etc/default/motd-news,并设置 ENABLED=0 即可。

动态 MoTD

/etc/update-motd.d 目录下有一堆脚本:

$ ls /etc/update-motd.d
00-header             90-updates-available       97-overlayroot
10-help-text          91-contract-ua-esm-status  98-fsck-at-reboot
50-landscape-sysinfo  91-release-upgrade         98-reboot-required
50-motd-news          92-unattended-upgrades
85-fwupd              95-hwe-eol

这些脚本控制了 MoTD 显示的内容。脚本名称前的序号表示它们显示的顺序。可以通过撤销执行权限来关闭显示:

sudo chmod -x 00-header

下面是各个文件负责显示的内容。

  • 00-header:

    Welcome to Ubuntu 22.04.5 LTS (GNU/Linux 5.15.0-131-generic x86_64)
    
  • 10-help-text:

     * Documentation:  https://help.ubuntu.com
     * Management:     https://landscape.canonical.com
     * Support:        https://ubuntu.com/pro
    
  • 50-landscape-sysinfo:

     System information as of Wed Feb 26 06:00:49 PM UTC 2025
    
      System load:               37.87
      Usage of /home:            52.2% of 6.15TB
      Memory usage:              10%
      Swap usage:                1%
      Processes:                 1958
      Users logged in:           9
      IPv4 address for enp1s0f0: 192.168.1.2
      IPv6 address for enp1s0f0: f451:340:2001:58b4:8fc1:15fb:1e8c:18b9
    
  • 50-motd-news:

    /etc/default/motd-news 一样,这个文件也可以控制 MoTD News 的显示。

     * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
       just raised the bar for easy, resilient and secure K8s cluster deployment.
    
       https://ubuntu.com/engage/secure-kubernetes-at-the-edge
    
  • 90-updates-available:

    Expanded Security Maintenance for Applications is not enabled.
    
    0 updates can be applied immediately.
    
    24 additional security updates can be applied with ESM Apps.
    Learn more about enabling ESM Apps service at https://ubuntu.com/esm
    
  • 91-release-upgrade:

    New release '24.04.2 LTS' available.
    Run 'do-release-upgrade' to upgrade to it.
    
  • 98-reboot-required:

    *** System restart required ***
    

静态 MoTD

管理员可以通过 /etc/motd 文件自定义用户登录系统时向用户显示的消息。

你可能会发现在设置了静态 MoTD 消息后,在登录时,MoTD 消息会显示两遍。这是因为 /etc/pam.d/sshd/etc/ssh/sshd_config 都设置了是否显示 MoTD 消息。其中 SSH 显示的消息会出现在 SSH 登录记录的后面:

Last login: Wed Feb 26 18:37:59 2025 from 127.0.0.1

可以关闭其中之一来避免重复显示 MoTD 消息。

  • 编辑 /etc/pam.d/sshd

    注释掉下面两行(第一行控制动态 MoTD,第二行控制静态 MoTD):

    # session    optional     pam_motd.so  motd=/run/motd.dynamic
    # session    optional     pam_motd.so noupdate
    
  • 编辑 /etc/ssh/sshd_config

    PrintMotd 设为 no

    PrintMotd no
    

参考:

posted @ 2025-02-24 15:16  Undefined443  阅读(171)  评论(0)    收藏  举报