Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer
    
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 Mar 7 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.. –  Andrejs Cainikovs Mar 10 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. –  Alexander Shcheblikin Mar 10 at 12:05

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 cat /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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.