我发现这个问题,但它谈论的是带有 GUI 的 Ubuntu 版本。我指的是 Ubuntu Server,所以没有 GUI。我还发现这个问题,但它看起来不会显示命令的输出,而只显示静态文本。
Ubuntu Server 登录屏幕如下所示:
Ubuntu 22.04 LTS mycomputer tty1
mycomputer login:
但是,我希望它运行一个命令(作为root
或在我的帐户下 - 您可以选择),然后我希望它在给出登录提示之前打印命令的输出。
例如,如果我的命令是echo Hello world!
,我希望它看起来像这样:
Hello world!
Ubuntu 22.04 LTS mycomputer tty1
mycomputer login:
或者这样也可以:
Ubuntu 22.04 LTS mycomputer tty1
Hello world!
mycomputer login:
虽然它在我的示例中,但我的命令的输出可以改变 - 它不能保证每次都相同,所以我无法将命令的输出硬编码到文件中(在我的情况下,命令是hostname -i
,它打印 IP 地址)。
我经常需要检查一台机器的 IP,如果不必登录检查的话会更方便。为了回答这个问题,假设这是家庭网络上的个人服务器,因此泄露 IP 地址不是问题。
如何在命令行登录屏幕显示命令的输出?
答案1
正如您已经发现的,服务器登录屏幕的外观由盖蒂程序 - 默认情况下,它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.
因此程序如下:
/etc/issue.d/
如果目录不存在则创建该目录创建一个脚本来运行您的命令并将其输出写入
.issue
该目录中的文件中修改模板
[email protected]
以通过其执行脚本ExecStartPre
概念证明(在 22.04 桌面虚拟机上测试):
sudo mkdir -p /etc/issue.d
sudo nano /usr/local/sbin/issue
有内容
#!/bin/sh
/bin/date +'Hello world @ %c' > /etc/issue.d/50hello.issue
(该date
命令使我们能确认内容是动态生成的)。
sudo chmod +x /usr/local/sbin/issue
现在创建 agetty@ 服务文件的本地副本:
sudo cp --no-clobber /lib/systemd/system/[email protected] /etc/systemd/system
使用您喜欢的文本编辑器编辑副本并添加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.
如果您的 ExecStartPre 命令由于某种原因失败,前导“-”将阻止整个单元失败。
如果你切换到新的TTY - 要在现有 TTY 中查看效果,您可能需要重新启动实例服务例如。sudo systemctl restart [email protected]
答案2
登录后motd
(不是真正需要的):
# cat /etc/update-motd.d/00-update
#!/bin/bash
echo
echo "Server IP: $(hostname -i)"
别忘了
chmod +x /etc/update-motd.d/00-update
文件:
man update-motd
答案3
预登录横幅由以下机构提供/etc/issue
。
对我来说,Kubuntu 22.10 上的这个文件默认包含一行:
Ubuntu 22.10 \n \l
\char 看起来像转义代码,但它们不是您所期望的,输出man issue
在这里为您提供帮助:
The issue files may contain certain escape codes to display the system name, date, time et cetera. All escape codes consist of a backslash (\)
immediately followed by one of the characters listed below.
**4 or 4{interface}**
Insert the IPv4 address of the specified network interface (for example: \4{eth0}). If the interface argument is not specified, then select
the first fully configured (UP, non-LOCALBACK, RUNNING) interface. If no configured interface is found, fall back to the IP address of the
machine’s hostname.
**6 or 6{interface}**
The same as \4 but for IPv6.
因此,添加“\4”(不带引号)将/etc/issue
打印 IP 地址。简洁。请注意,它仅适用于您尚未输入的 TTY [自上次重新启动以来?];您可以注销 TTY,它会重置问题横幅。另外,它\n
不是回车符,空格被保留,而是“节点名称”,对我来说,它与 的输出相同host
。
这仅适用于 TTY,如果您想要远程登录的横幅,那么您需要查看提供远程 shell 的程序,例如 SSH(参见sshd_config
文件)。
===
编辑:我还有一个名为 的文件issue.net
,但似乎是残留的(旧的和不起作用的)。/etc/issue
来自 base-files 包。我没有目录,/etc/issue.d
但创建一个目录,添加扩展名为“.issue”的文件就可以了。我没有进行广泛的测试;apt-file search "/issue.d"
只返回包freedombox
(我从未听说过)。