4

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.

3 Answers 3

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.

4
  • 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) Mar 7, 2014 at 21:06
  • 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.. Mar 10, 2014 at 8:20
  • 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. Mar 10, 2014 at 12:05
  • @AlexanderShcheblikin : I am doing that setup because I am working with some kind of test/dev VM on my personal computer, and since those machines are starting with DHCP, I am not sure with IP they will use. Adding that allows me to see directly the IP from the console without having to login or anything. Then I can use SSH.
    – Olivier M.
    Jun 16, 2022 at 12:47
12

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}
1
  • 1
    Also works on openSUSE, for the record. Nov 29, 2019 at 11:13
5

For CentOS 5 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.

2
  • 1
    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)"
    – Rade_303
    Aug 23, 2016 at 13:50
  • 1
    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! Sep 2, 2016 at 0:28

You must log in to answer this question.

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