LoginSignup
9
8

More than 5 years have passed since last update.

CentOSのログインスクリーンにIPアドレスを表示する

Last updated at Posted at 2015-11-10

ランレベル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は、サービスが全て起動し終わった後に実行されるので、networkNetworkManagerを使って、イーサネットを起こして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へ追記する。

9
8
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
9
8