Buddy of mine asked me a question over chat today: “how do I show my machines’ IP at login prompt with out logging in?” He is referring to his Virtual Machine in this case. He does not want to have to log in to the server to see what ip it has (since its on dhcp) for him to ssh in or hit it from the browser. I could have answered him with a simple how to but what is the fun in that? So I decided to give some background on how login prompts are done and show what can be done.
When Linux server boots up, it calls a program called mingetty. This program creates that infamous login prompt as show in a screenshot:
You can see how server calls the mingetty program by looking at /etc/inittab. You will see a block like below:
# Run gettys in standard runlevels
When mingetty is called, it reads a file called /etc/issue. There are a lot of existing variables you use. For example the prompt shown above in example is:
CentOS release 5 (Final)
Kernel \r on an \m
All of the available variables are list here on this page: http://www.lamp-tips.com/man-pages/mingetty/
If you were paying attention in the beginning of the post and looked at all of the available variables you can use, you would notice there is nothing in there which talks about IP. Since there is no variables available, you would have to sort of hack it. Put following in your /etc/rc.local file:
echo "CentOS release 5 (Final)" > /etc/issue
echo "Kernel \r on an \m" >> /etc/issue
echo `ifconfig eth0 | grep inet | cut -d : -f 2 | cut -d \ -f 1` >> /etc/issue
Getting an ip part can be modified to check another adapter such as eth1, eth2, etc. Or you can get it another way if you want. That all there is too. Reboot, and you should see your changes.
[correction 4.24.08] you do not have to reboot to see your changes.Â Just press enter at the login prompt in console and it will re-read the issue file.
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.
My SA skills are rusty, but I thought /etc/motd would do the same thing without requiring a reboot?
Sheeri, /etc/motd doesn’t work (at least not for me). But I misspoke, you don’t have to reboot to see the /etc/issue changes. I have corrected my post. Thanks for bringing it up.
For Ubuntu Server 12 LTS the syntax is:
ifconfig eth0 | grep -m1 inet | cut -d: -f 2 | cut -dB -f 1
Hi,i would like to thank you for this post somehow i msised that command (systemsetup) until today while it looks very promising for ard-scripts.Thumbs up for your current posts which are all interessting would be great to see if you continue your work in this blog.Greetingsfidel
This task requires great linux problem-solving ability. It is not easy for sure!
Calling all cars, calling all cars, we’re ready to make a deal.