ssh伺服器登入成功後顯示訊息

ssh伺服器登入成功後顯示訊息

我有一個 debian ssh 伺服器問題,我想在成功登入後顯示一條訊息,我使用了 motd,但本地主機會顯示它。我不希望本地機器顯示它。我只想在特定用戶成功登入 ssh 後顯示它。

答案1

若要在特定使用者成功登入 SSH 後顯示訊息,您可以執行以下步驟:

建立一個文件,其中包含您希望在使用者登入後顯示的訊息。

修改 SSH 設定檔(/etc/ssh/sshd_config)並在檔案底部新增以下行:

ForceCommand echo "YOUR MESSAGE HERE"

儲存檔案並使用以下命令重新啟動 SSH 守護程式:

sudo systemctl restart sshd

執行這些步驟後,特定使用者登入 SSH 後將顯示該訊息。此方法將繞過「每日訊息」(motd) 功能並直接向使用者顯示您的訊息。

值得注意的是,修改 SSH 設定檔如果操作不當可能會導致安全性問題。因此,我建議先在非生產環境中測試更改,並確保更改符合組織的安全策略。

答案2

我認為修改 sshd 配置可能適用於所有用戶,不一定只適用於單一用戶。

另一種方法是使用 SSHLog:https://github.com/sshlog/agent/

您可以將其配置為監視一個或多個用戶(例如,jdoe)的成功登錄,然後在成功登入該用戶的 TTY 時執行命令「write」。例如,這是我測試過的配置,可以執行您想要的操作:

events:
  - event: jdoe_logs_in
    triggers: [ 'connection_established']
    filters:
      username: 'jdoe' 
      require_tty: True
    actions:
      - action: message_about_lunch_money
        plugin: run_command_action
        command: bash 
        args: ["-c", "echo 'Hello {{username}} do not forget that $4.75 you owe me for lunch!' | write {{username}} /dev/pts/{{tty_id}}"]

然後當他登入時,他的 MOTD 將如下所示:

Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-67-generic x86_64)
...
40 additional security updates can be applied with ESM Apps.
Learn more about enabling ESM Apps service at https://ubuntu.com/esm

*** System restart required ***

Last login: Tue Apr 18 11:41:16 2023 from 127.0.0.1

Message from root@server on pts/20 at 11:48 ...
Hello jdoe do not forget that $4.75 you owe me for lunch!
EOF

相關內容