LoginSignup
10
9

More than 5 years have passed since last update.

構成管理したサーバーにSSHした不届き者に警告メッセージを出してみる

Posted at

本当にちょっとしたサーバーも手動は嫌でChefか何かで構成管理しないと済まない性分なので、手動でいじられないようにSSHした人に警告メッセージを出してみる方法を調べてみた。

例えばAmazon Linuxでやるとこんなかんじ

$ ssh somehost
Last login: Sat Aug 13 16:56:53 2016 from 0.0.0.0

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2016.03-release-notes/
#######################################################
## WARNING: THIS SERVER IS CONFIGURED WITH ANSIBLE!! ##
#######################################################
!このサーバーはItamaeで構成管理されています。    !
!緊急時を除き、Itamaeを使わない手動での設定変更等は!
!絶対に行わないでください              !
#######################################################
$ 

Motdを使う

Linuxでmotd(message of the day)を使うとこれが実現できます。

/etc/update-motd.d/ というディレクトリにスクリプトを入れると、sshした時にそのコマンドを実行してくれる。シェルスクリプトでもPerlスクリプトでもなんでもいい。

複数ファイルが有った場合は辞書順で実行されるので、頭に数字などをつけて順番をコントロールする場合が多いのかな。スクリプトを配置したらsudo update-motdで反映できます。

Amazon Linuxだと例えば以下のようになっていました。EC2である旨を表示するものと、アップデートできるyumパッケージがいくつかるかを表示しているみたい。

$ ls /etc/update-motd.d/
30-banner  70-available-updates  75-system-update

今回は以下の様なメッセージを表示させたいので、以下の様なファイルを作ってこれを適切な名前で配置することで実現しようと思います。

motd.txt
cat << EOF
#######################################################
## WARNING: THIS SERVER IS CONFIGURED WITH ANSIBLE!! ##
#######################################################
!このサーバーはItamaeで構成管理されています。    !
!緊急時を除き、Itamaeを使わない手動での設定変更等は!
!絶対に行わないでください              !
#######################################################
EOF

Ansibleで書いた例

tasks:
  - name: Add login message
    copy: src=motd.txt dest=/etc/update-motd.d/99-ansible-warning mode=0744
    notify: Reflect motd changes
handlers:
  - name: Reflect motd changes
    shell: "update-motd"

Itamaeで書いた例

remote_file "/etc/update-motd.d/99-itamae-warning" do
    source "files/motd.txt"
    mode "0744"
    notifies :run, "execute[update-motd]"
end

execute "update-motd" do
    command "update-motd"
    action :nothing
end

おわりに

これを自分ツールとして毎回使うと便利??
そもそも勝手にサーバー作るなっていう話ですが、どうしてもどうしてもさっさと作んなきゃとか特別な事情があって勝手に作る時もこういう配慮はしておきたいものです。

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9