modify login prompt or header (/etc/issue) to display ip address of the machine

3

1

I work with a lot of virtual machines. For testing and preproduction set up.

I would like the login promt or header to display the ip address of the machine. That way when I start it up I can see what IP I will be ssh into. Our network works uses a DHCP. So it can change between boot up.

$ cat /etc/issue
Ubuntu 11.04 \n \l

Which comes up as

Ubuntu 11.04 [hostname] tty[x]

I want it to come up as

Ubuntu 11.04 [hostname] tty[x] ip xxx.xxx.xxx.xxx

I was think about writing an init / upstart script. Is there a better way.

nelaaro

Posted 2012-01-20T12:30:35.810

Reputation: 9 321

Answers

2

Getty does not know machine's ip addresses. But this question was already asked at serverfault. Here's the accepted answer:

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

Also, beware that the file /etc/issue.net is used for remote logins so you may want to edit that as well.

Andrejs Cainikovs

Posted 2012-01-20T12:30:35.810

Reputation: 2 611

Why would someone need to look at the IP address of a remote system after having connected to that system? (that's on the question of modifying the /etc/issue.net) – Alexander Shcheblikin – 2014-03-07T21:06:08.670

If you have one hundred servers, and your .ssh/config is configured to distinguish them by hostname, I believe it would be handy to know it's IP address for administration purposes. It's just one of the use cases.. – Andrejs Cainikovs – 2014-03-10T08:20:57.230

The IP address can be just looked up with host. However, I'd agree that some complex NAT configurations might make a simple lookup not that useful. – Alexander Shcheblikin – 2014-03-10T12:05:29.767

7

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}

PaoloC

Posted 2012-01-20T12:30:35.810

Reputation: 201

1Also works on openSUSE, for the record. – Liam Proven – 2019-11-29T11:13:30.240

4

For CentOS with a DHCP leased IP, you can use this script:

$ cat /etc/dhcp/dhclient.d/issue.sh
#!/bin/bash

update_issue() {
    awk -v \
        r="$(ip -o addr | awk '/inet [1-9]+/ { print $2 " " $4 }')" \
        '{ gsub(/%INTERFACES%/,r) }1' \
        /etc/issue.template > /etc/issue
}

issue_config() {
    update_issue
}

issue_restore() {
    update_issue
}

with an issue "template" like this:

$ cat /etc/issue.template
CentOS release 6.5 (Final)
Kernel \r on an \m

%INTERFACES%

Remember to

chmod +x /etc/dhcp/dhclient.d/issue.sh

The awk command to get the current IP and replace them in the /etc/issue.template file should be portable to modern Linux distros.

bishop

Posted 2012-01-20T12:30:35.810

Reputation: 150

Excellent answer. I find that instead of ip -o addr for my purposes eth0 is enough - r="$(ip address show eth0 | awk '/inet / {print $2}' | cut -d/ -f1)" – Rocky – 2016-08-23T13:50:09.193

Smart answer. It's very nice that your answer doesn't simply overwrite /etc/issue with a static value, and that it doesn't simply write a static value once. It's idempotent! – Stefan Lasiewski – 2016-09-02T00:28:13.007