ランレベル3の時に、いちいちログインしなくてもIPアドレスを確認できるようにするハック
やり方
# rootユーザで
cat >> /etc/rc.d/rc.local <<-EOF
if [ ! -f /etc/issue.org ]; then
/bin/cp /etc/issue /etc/issue.org
fi
/bin/cp -f /etc/issue.org /etc/issue
/sbin/ip address show eth0 | /bin/awk '/inet / {print $2}' | /bin/cut -d/ -f1 >> /etc/issue
EOF
必要に応じて、デバイス名(eth0
)の部分を変更すること。
解説
-
/etc/issue
って何?
ランレベル3の時のログインスクリーンに表示される内容は、/etc/issue
に定義されているので、そこにIPアドレスを記載してやることでログインスクリーンに表示されるようになる。
/etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
-
/etc/rc.d/rc.local
って何?
/etc/rc.d/rc.local
は、サービスが全て起動し終わった後に実行されるので、networkやNetworkManagerを使って、イーサネットを起こしてDHCPなどでIPアドレスを取得するようになっていれば、実行時にはIPアドレスが決まっている状態になっている。
/etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
if [ ! -f /etc/issue.org ]; then
/bin/cp /etc/issue /etc/issue.org
fi
/bin/cp -f /etc/issue.org /etc/issue
/sbin/ip address show eth0 | /bin/awk '/inet / {print $2}' | /bin/cut -d/ -f1 >> /etc/issue
-
/etc/rc.d/rc.local
に記載した内容を見る
if [ ! -f /etc/issue.org ]; then
/bin/cp /etc/issue /etc/issue.org
fi
/bin/cp -f /etc/issue.org /etc/issue
毎回、/etc/issue
に追記していくと、起動する度にIPアドレスが記載されている行が増えていってしまうので、オリジナルのファイルを複製して保持しておく。
/sbin/ip address show eth0 | /bin/awk '/inet / {print $2}' | /bin/cut -d/ -f1 >> /etc/issue
eth0
のIPアドレスを取得し整形した上で、/etc/issue
へ追記する。