33

The server I administrate has a dynamic IP address assigned by DHCP, which is inconvenient. When I want to SSH into it, I have to first log in by hand and use ifconfig to find its current IP address. Thankfully, it lives about two feet from where I sit.

I know how to edit /etc/issue to show different values before the login prompt is delivered to the display, but I'd like to know if it's possible for /etc/issue to display the current IP address of eth0 (re-evaluated at boot time) so that I can see it and then ssh in without having to log in to run ifconfig.

1
  • Can conky be used in the login screen? If so you could make all sorts of info handy.
    – dibs
    Nov 15, 2012 at 1:07

7 Answers 7

48

As of Debian 8/jessie you can use the \4 and \6 escape characters to output the IPv4 and IPv6 addresses. So the /etc/issue file:

Debian GNU/Linux 8 \n \l

eth0: \4{eth0}

Would output something like the following at the login console:

Debian GNU/Linux 8 myserver tty1

eth0: 192.168.1.100
myserver login:

I imagine Ubuntu would provide similar functionality (at least in newer releases)

4
  • This worked for me in Ubuntu 16.04 LTS, the answer marked as correct didn't, probably an issue with the grep comment, in any case, thanks for posting this.
    – Tracker1
    Jan 19, 2017 at 16:19
  • It still works on Debian 9 :)
    – borekon
    May 7, 2019 at 7:49
  • 1
    This can be abbreviated further: eth0: \4 will give the IP address of the first non-loopback interface.
    – Nick ODell
    May 23, 2020 at 4:38
  • Tested on Debian 10 and still works, but changed a little: \4{enp2s5}
    – borekon
    Nov 10, 2021 at 11:44
13

/etc/issue is unfortunately a plain text, it has some options you can add to it (see man agetty) but not the IP address of eth0.

If you put this in /etc/rc.local:

IP=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')
echo "eth0 IP: $IP" > /etc/issue

Then you will see something like this:

eth0: 192.168.0.2
myServer login:
2
  • 3
    Please note for anyone running this, this will overwrite the contents of /etc/issue. You could add steps to grep out the previous "eth*" lines and then append them.
    – Rebs
    Mar 16, 2016 at 22:57
  • This solution worked on Alpine Apr 6, 2021 at 21:26
2

Building on Alex's answer and Rebs' comment, this is what I have for my /etc/rc.local file (only relevant lines):

PRE_MSG="Ubuntu 14.04.3 LTS" # this is from the original /etc/issue

IP=$(/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')

IP_MSG="Server IP Address:"

printf "%s %s %s\n\n%s %s\n\n" "$PRE_MSG" '\n' '\l' "$IP_MSG" "$IP" > "/etc/issue"

The printf command does the magic. Unfortunately putting the '\n' and '\l' inside the format string ended up replacing the first one with the new line character and the /etc/issue file was messed up.

So long story short, these 4 lines keep the original info displayed by /etc/issue file and add the Server IP Address: message.

And as Rebs' said, keep in mind that every time you reboot the server, this script will override the /etc/issue file, so maybe try it inside a different script and on a different file and once it's working, transfer the changes over...

Good luck and happy BASHing :)

1

With systemd you can do systemctl edit getty@ with something 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

For Ubuntu 19.04, I followed these steps:

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!

1

Building on Nurchi, Alex and Steve's answers & the comment from Reb,

The following will grep the old IP out and add in any non local (127.) IP addresses. My server is a VM and doesn't have eth0. Testing on Ubuntu 18.04

Place in /etc/network/if-up.d/update-issue

#!/bin/sh
MSG=$(cat /etc/issue | grep -v IP)
IP=$(/sbin/ifconfig | grep 'inet' | grep -v '127' | cut -d: -f2 | awk '{ print $2 }')
printf "%s\n%s\n\n" "$MSG" "IP: $IP" > /etc/issue

Then chmod 0755 /etc/network/if-up.d/update-issue

The only difference between my answer and Steve's is the addition of the grep lines to remove the IP line, capture the message and readd it along with the new/updated IP

0

This works for ubuntu 20.04 LTS as well. Just make sure you're using the right interface. The output of ifconfig will show all interfaces, update the same interface in /etc/issue

enter image description here

You must log in to answer this question.

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