在 /etc/motd 中运行脚本

在 /etc/motd 中运行脚本

我想在人们登录服务器时运行一个 python 脚本。我认为编辑 /etc/motd 是最好的方法,但由于它只是可读文件,所以这种方法行不通。

我发现了几乎相同的帖子:是否可以将命令放入 /etc/motd 中?

这里最好的解决方案是什么?我想运行一个显示服务器概览的脚本,以便用户立即看到状态。

这是建议A useful practice is to put the command you want to be executed in a script named /etc/motd.sh and call this script from /etc/profile, usually at about the end of it.

但收到了这个评论Putting output like a MOTD in your profile is likely to break sftp.

任何解决方案都可以。提前致谢

答案1

实际上,最好使用 /etc/profile.d 来实现这个选项。并在那里提供源脚本。它会在每次登录时显示输出。

关于第二个问题和 breakinf sftp。我进行了一些谷歌搜索。

您可以使用:

Subsystem sftp /usr/lib/openssh/sftp-server

在您的 /etc/ssh/sshd_config 中。

或者添加

if [ "$SSH_TTY" ]
then
   source .bashc_real
fi

到你的脚本,打印类似 motd 的消息。我从以下地址得到这个答案:使用 .bashrc 而不破坏 sftp)。

答案2

在 Ubuntu/Debian 中,PAM 模块pam_motd支持动态 motd 功能,据说它不会干扰 sftp。不幸的是,它没有很好的文档记录。你可以参考update-mod 手册页在 Ubuntu 中。

在 Ubuntu 系统上,您已经拥有了几个脚本/etc/update-motd.d/。它们是适合您自己入门的现场示例。

仔细考虑每次使用动态 motd 登录时消耗的某些资源。阅读最佳实践update-motd 手册页中的部分。

相关内容