![systemd - 用户通过 ssh 登录时出现过多的系统日志消息](https://linux22.com/image/201317/systemd%20-%20%E7%94%A8%E6%88%B7%E9%80%9A%E8%BF%87%20ssh%20%E7%99%BB%E5%BD%95%E6%97%B6%E5%87%BA%E7%8E%B0%E8%BF%87%E5%A4%9A%E7%9A%84%E7%B3%BB%E7%BB%9F%E6%97%A5%E5%BF%97%E6%B6%88%E6%81%AF.png)
每次有人使用 SSH 连接时,systemd 都会将以下内容打印到系统日志中:
Jan 21 09:53:54 server systemd[1]: Created slice User Slice of UID 1002.
Jan 21 09:53:54 server systemd[1]: Starting User Runtime Directory /run/user/1002...
Jan 21 09:53:54 server systemd[1]: Started User Runtime Directory /run/user/1002.
Jan 21 09:53:54 server systemd[1]: Starting User Manager for UID 1002...
Jan 21 09:53:55 server systemd[16973]: Listening on GnuPG cryptographic agent and passphrase cache.
Jan 21 09:53:55 server systemd[16973]: Reached target Paths.
Jan 21 09:53:55 server systemd[16973]: Listening on GnuPG cryptographic agent and passphrase cache (access for web browsers).
Jan 21 09:53:55 server systemd[16973]: Reached target Timers.
Jan 21 09:53:55 server systemd[16973]: Listening on GnuPG network certificate management daemon.
Jan 21 09:53:55 server systemd[16973]: Listening on GnuPG cryptographic agent and passphrase cache (restricted).
Jan 21 09:53:55 server systemd[16973]: Listening on GnuPG cryptographic agent (ssh-agent emulation).
Jan 21 09:53:55 server systemd[16973]: Reached target Sockets.
Jan 21 09:53:55 server systemd[16973]: Reached target Basic System.
Jan 21 09:53:55 server systemd[16973]: Reached target Default.
Jan 21 09:53:55 server systemd[16973]: Startup finished in 81ms.
Jan 21 09:53:55 server systemd[1]: Started User Manager for UID 1002.
这是过度且无益的。如何禁用其中一些消息?
我见过人们通过字符串匹配来过滤它们,但这只是一种黑客行为。当然应该在某个地方有一个详细设置?目前还不清楚哪个程序负责——是 systemd 吗? SSH?还有别的事吗?
答案1
以 root 身份尝试安装覆盖系统服务
systemctl edit sshd.service
或编辑分发文件
vi /etc/systemd/system/sshd.service
在前一种情况下,您只需添加覆盖的指令
[Service]
LogLevelMax=4
如果编辑整个文件,您将找到整个服务配方
[Service]
EnvironmentFile=-/etc/default/ssh
ExecStartPre=/usr/sbin/sshd -t
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
ExecReload=/usr/sbin/sshd -t
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartPreventExitStatus=255
Type=notify
RuntimeDirectory=sshd
RuntimeDirectoryMode=0755
您将向其中添加 LogLevelMax 指令。
(日志级别从 0 - 紧急 - 到 6 - 调试喋喋不休)。
此时,您重新加载配置并重新启动服务:
systemctl daemon-reload
systemctl restart sshd.service