如何让 Ubuntu 在 Ubuntu Server 的登录屏幕上显示命令的输出?

coc*_*mac 7 server text command-line login

我发现了这个问题,但它谈论的是带有 GUI 的 Ubuntu 版本。我指的是 Ubuntu Server,所以没有 GUI。我也发现了这个问题,但它看起来不会显示命令的输出,而只会显示静态文本。

Ubuntu Server 登录屏幕如下所示:

Ubuntu 22.04 LTS mycomputer tty1
mycomputer login:
Run Code Online (Sandbox Code Playgroud)

但是,我希望它运行一个命令(作为root我的帐户或在我的帐户下 - 您可以选择),然后我希望它在给出登录提示之前打印命令的输出。

例如,如果我的命令是echo Hello world!,我希望它看起来像这样:

Hello world!
Ubuntu 22.04 LTS mycomputer tty1
mycomputer login:
Run Code Online (Sandbox Code Playgroud)

或者这样也可以:

Ubuntu 22.04 LTS mycomputer tty1
Hello world!
mycomputer login:
Run Code Online (Sandbox Code Playgroud)

虽然在我的示例中,但我的命令的输出可以更改 - 不能保证每次都相同,因此我无法将命令的输出硬编码到文件中(在我的例子中,命令是hostname -i,打印 IP 地址)。

我经常需要检查一台机器的IP,如果我不需要登录来检查它会更容易。为了解决这个问题,假设这是家庭网络上的个人服务器,因此泄露 IP 地址不是问题。

如何在命令行登录屏幕中显示命令的输出?

ste*_*ver 9

正如您已经发现的,服务器登录屏幕的外观是由getty程序控制的 - 默认情况下,这是agetty在 Ubuntu 上。该agetty程序从名为 - 的文件中读取/etc/issue,并且从agetty 版本 2.35 开始,可以选择从/etc/issue.d. 从man agetty

问题文件

   The default issue file is /etc/issue. If the file exists, then agetty
   also checks for /etc/issue.d directory. The directory is optional
   extension to the default issue file and content of the directory is
   printed after /etc/issue content. If the /etc/issue does not exist,
   then the directory is ignored. All files with .issue extension from the
   directory are printed in version-sort order. The directory can be used
   to maintain 3rd-party messages independently on the primary system
   /etc/issue file.
Run Code Online (Sandbox Code Playgroud)

所以程序是:

  1. /etc/issue.d/如果目录尚不存在,则创建该目录

  2. 创建一个脚本来运行命令并将其输出写入.issue该目录中的文件

  3. 修改模板getty@.service以通过其执行脚本ExecStartPre

概念证明(在 22.04 桌面虚拟机上测试):

sudo mkdir -p /etc/issue.d

sudo nano /usr/local/sbin/issue
Run Code Online (Sandbox Code Playgroud)

有内容

    #!/bin/sh

    /bin/date +'Hello world @ %c' > /etc/issue.d/50hello.issue
Run Code Online (Sandbox Code Playgroud)

date这样我们就可以确认内容是动态生成的命令)。

sudo chmod +x /usr/local/sbin/issue
Run Code Online (Sandbox Code Playgroud)

现在制作agetty@服务文件的本地副本:

sudo cp --no-clobber /lib/systemd/system/getty@.service /etc/systemd/system
Run Code Online (Sandbox Code Playgroud)

使用您喜欢的文本编辑器编辑副本并添加指令ExecStartPre

[Service]
# the VT is cleared by TTYVTDisallocate
# The '-o' option value tells agetty to replace 'login' arguments with an
# option to preserve environment (-p), followed by '--' for safety, and then
# the entered username.

ExecStartPre=-/usr/local/sbin/issue
ExecStart=-/sbin/agetty -o '-p -- \\u' --noclear %I $TERM
.
.
etc.
Run Code Online (Sandbox Code Playgroud)

如果您的 ExecStartPre 命令由于某种原因失败,前导的“-”会阻止整个单元失败。

如果您切换到新的TTY ,它应该立即起作用- 要查看现有 TTY 中的效果,您可能需要重新启动实例服务 ex。sudo systemctl restart getty@tty2.service

终端2


Gil*_*not 2

登录后motd(不完全是所需要的):

# cat /etc/update-motd.d/00-update
Run Code Online (Sandbox Code Playgroud)
#!/bin/bash
echo
echo "Server IP: $(hostname -i)"
Run Code Online (Sandbox Code Playgroud)

别忘了

chmod +x /etc/update-motd.d/00-update
Run Code Online (Sandbox Code Playgroud)

文档:

man update-motd
Run Code Online (Sandbox Code Playgroud)

  • 如前所述 - motd 在**登录后**显示。这不是正确的答案。 (5认同)
  • 我从未使用过它,但登录后没有显示“motd”,例如,如此处所述,https://manpages.ubuntu.com/manpages/trusty/en/man5/motd.5.html。OP想要修改登录提示本身。也许 systemd motd 可以在登录前显示?我浏览了一下,在您链接的手册页中没有看到任何内容。 (3认同)