Skip to content

Instantly share code, notes, and snippets.

@filviu
Last active August 29, 2015 14:24
Show Gist options
  • Save filviu/3445bb2d1bd14f9a0ba8 to your computer and use it in GitHub Desktop.
Save filviu/3445bb2d1bd14f9a0ba8 to your computer and use it in GitHub Desktop.
Print IP on CentOS 7 after boot
#!/bin/bash
# goes in /sbin !!!
nohup sh -c "sleep 1 ; /usr/local/sbin/update-issue.sh" &
#!/bin/bash
# goes in /usr/local/sbin
# MAKE A issue.orig BEFORE EVER RUNNING THIS (or rebooting)
# something like cp /etc/issue /etc/issue.orig should do it
/bin/rm /etc/issue
/bin/cp /etc/issue.orig /etc/issue
echo "Current IP: "$(/sbin/ip a s |/bin/grep "inet " | grep -v "127.0"|/bin/awk '{print $2}')>>/etc/issue
echo >> /etc/issue

Because I run a lot of VMs, many times behind other gateway VMs and vlans and it's easy to loose track on where can I ssh into a particular VM

First I tried printing from rc.local but the boot console is cleared. You have to remove "quiet" from grub and add

/etc/systemd/system/[email protected]/noclear.conf
[Service]
TTYVTDisallocate=no

But it still won't work because rc.local no longer starts last. So I added ifup-local to change /etc/issue but it won't run like that because of this: http://serverfault.com/questions/631899/centos-7-getting-interface-ip-numbers
In case it goes away: I found the answer: the information is not available to the "ip" command until the network change (from the "sudo service network restart" or the reboot) is complete. Because the network change causes "/etc/sysconfig/network-scripts/ifup-post" to run before it is finished, which in-turn calls out to "/sbin/ifup-local" to run before it is finished, "ip" called within "/sbin/ifup-local" called automatically can never have that information. To solve this, I renamed the script from "/sbin/ifup-local" to "/sbin/update-issue", and created a new "/sbin/ifup-local" that calls "/sbin/update-issue" asynchronously. Now it works

So I applied the sollution from the serverfault link above, and it works (two other files represent the working solution, not my rant about systemd).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment