17

I've got a couple of linux virtual machines with bridged interfaces, and I'd like the IP address of the machine to show up after the machine boot (in the login, where it usually shows the release and kernel).

From what I can tell the message is picked up from /etc/issues, but I'm not sure how and when to write to it.

1
  • Hey Bruno can you change the accepted answer to PabloC? His answer is really great. Finding the documentation for this is very hard and his answer is very complete.
    – Peter
    May 16, 2018 at 7:50

7 Answers 7

15

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).

3
  • 5
    Slightly more Linuxy to say ip address show eth0 | awk '/inet / {print $2}' | cut -d/ -f1
    – Sorpigal
    Dec 6, 2010 at 18:01
  • I'm not sure how that's more linuxy, but yes, that works too.
    – larsks
    Dec 7, 2010 at 16:33
  • 1
    @larks: It's more linuxy because on linux ip is the tool you're supposed to use for this sort of thing. ifconfig is technically just for compatibility.
    – Sorpigal
    Dec 17, 2010 at 18:03
49

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.

5
  • Also works on CentOS 8 :) Oct 7, 2019 at 6:32
  • This worked perfectly on my Centos 7 vm.
    – Kevin
    Dec 17, 2019 at 14:44
  • If possible on your platform; this should be preferred as it's a much cleaner solution. Tested and working on Fedora.
    – aaa
    Aug 17, 2020 at 10:34
  • Also works on Photon OS 4, so it would seem this has pretty broad support
    – Cocowalla
    Jul 23, 2021 at 9:32
  • just note that it's agetty from util-linux
    – mykhal
    Feb 18, 2022 at 13:07
7

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.

2
  • 3
    For anyone searching, in Red Hat/CentOS/etc this script would be /sbin/if{up,down}-local. (If it exists, it's called by /etc/sysconfig/network-scripts/ip{up,down}-post.) Mar 18, 2015 at 15:15
  • @powpow: Why "Local IP addresses"? --all-ip-addresses outputs also "public" IP addresses. Maybe something like "IP addresses of all external interfaces of this host:" makes more sense.
    – ypid
    Nov 11, 2015 at 22:07
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.

1
  • 1
    This is just to know what IP to connect to after booting the vm, so setting it up as larsks says is enough :) Dec 6, 2010 at 15:56
3

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

1
  • This is the most flexible answer and allows displaying all manner of other dynamic values. Thanks!
    – bart
    May 14, 2020 at 9:13
1

Many thanks to @larsks for getting started. I found that I had trouble with the script completing before it was needed so my issue file was always blank. So I added a 1 second pause and looped. I'm not really a bash scripter, so if there is a better way to do this please let me know. I added a COUNT to make sure it didn't loop forever.

#!/bin/sh

COUNT=1

while [ $COUNT -lt 10 ]
do
  echo $COUNT

  COUNT=$((COUNT + 1))
  IP=`ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -f2 -d: `
  if [ -n "$IP" ]; then
    break
  fi 
  sleep 1
done


echo "IP: " $IP | cat > /etc/issue

I placed this script in /etc/network/if-up.d/.

0

For anyone landing here that uses Ubuntu 19.04, I ended up adopting from @powpow's answer.

I created the file: /etc/network/if-up.d/update-issue with the following contents:

#!/bin/sh
PREFIX="Ubuntu 19.04 - dev"
IPADDRS=$(hostname -I | tr " " "\n" | grep -v "^$" | sort -t . -k 1,1n | head -1 | tr "\n" " ")
echo "$PREFIX\n\nIP: $IPADDRS\n" > /etc/issue

I then marked the file as executable: chmod 0755 /etc/network/if-up.d/update-issue

Works great!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .