How to setup /etc/issues to show the IP address for eth0

Solution 1:

On CentOS 7 and Debian 8 (and maybe other as well), just append the following line to /etc/issue

My IP address: \4

and that will resolve to the machine's IPv4 address. If you have multiple network interfaces and you want to pick one specific, you can specify it with

My IP address: \4{eth0}

Check man getty for a list of supported escape sequences on your distribution.

Solution 2:

It's just a text file...you write to it the same way you'd send text to a file with any other shell script. Something like this would replace /etc/issue with just your ip address:

ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -f2 -d: > /etc/issue

Obviously you can make this arbitrarily more complex, depending on what information you want in your /etc/issue file.

You can write to this file in your local equivalent of /etc/rc.d/rc.local (which typically executes after all the other startup scripts).


Solution 3:

This script will add/remove IP addresses to/from the /etc/issue file as needed:

#!/bin/sh
PREFIX="Local IP addresses:"
IPADDRS=$(hostname -I | tr " " "\n" | grep -v "^$" | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n | tr "\n" " ")

perl -i -p -0777 -e "s/^$PREFIX[^\n]*\n\n//m; s/$/\n$PREFIX $IPADDRS\n/ if length('$IPADDRS')>6" /etc/issue

If you're using a Debian-based distro, it's best to place the script at these locations:

/etc/network/if-up.d/update-issue
/etc/network/if-post-down.d/update-issue

This way the scripts are executed everytime an interface comes up or goes down. Placing it in /etc/rc.d/rc.local has the disadvantage of beeing executed only one time during bootup.


Solution 4:

you can write it once. unfortunately there's no getty escape sequence to show the ip address of eth0 but there's an escape sequence you can use in /etc/issue to show the hostname: \n

you can always statically set the ip of the machine in /etc/issue by writing it directly into the file. beware that the file /etc/issue.net is used for remote logins so you may want to edit that as well.

anyway, after working in the terminal window you will loose the initial screen and the ip/host address information with it. best way is to set the hostname in the prompt (most linux distros do) using \h or \H or again, statically setting the ip address you know is set on the host in the PS1 variable you can set PS1 variable in /etc/profile on most linux distros.


Solution 5:

Systemd makes this relatively easy. systemctl edit getty@ with content like:

[Service]
ExecStartPre=-/bin/bash -c '[ ! -f /etc/.issue.orig ] && cp /etc/issue /etc/.issue.orig; int=`ls /sys/class/net|grep enp|head -1`; sed -r "s/\\\\\\n/[\\\\\\4\{$$int\}]/" < /etc/.issue.orig > /etc/issue'

Then systemctl daemon-reload && systemctl restart getty@tty1