Adding information about the IP address of a linux machine to the login prompt
Things to know
Getting the IP address
The IP address for the current machine can be obtained entering the following command:
hostname -I
Startup scripts
There are some default scripts run after particular events.
The one I want (after startup) it’s located in /etc/rc.local
Login prompt message
The login message it’s handled by the agetty command and documentation about it can be found using man agetty
.
The agetty command displays and interprets the content of the /etc/issue
file.
Mixing all this togheter
At startup I need to get the IP address of the machine and append it to the issue file to be printed out at the login screen.
First thing to do is to create a copy of the /etc/issue
file.
This can be done with the command:
sudo cp /etc/issue /etc/issue.base
The next thing to do is to edit the /etc/rc.local
file to:
- gather the current IP address
- copy
/etc/issue.base
back to/etc/issue
- append to
/etc/issue
a new line with a string likeIP address: xxx.xxx.xxx.xxx
This can be done editing the original /etc/rc.local
that should look like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
# | |
# rc.local | |
# | |
# This script is executed at the end of each multiuser runlevel. | |
# Make sure that the script will "exit 0" on success or any other | |
# value on error. | |
# | |
# In order to enable or disable this script just change the execution | |
# bits. | |
# | |
# By default this script does nothing. | |
exit 0 |
To look like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
# | |
# rc.local | |
# | |
# This script is executed at the end of each multiuser runlevel. | |
# Make sure that the script will "exit 0" on success or any other | |
# value on error. | |
# | |
# In order to enable or disable this script just change the execution | |
# bits. | |
# | |
# By default this script does nothing. | |
BASE_ISSUE_FILE="/etc/issue.base" | |
CURRENT_ISSUE_FILE="/etc/issue" | |
CURRENT_IP_ADDRESS=$(hostname -I) | |
cat $BASE_ISSUE_FILE > $CURRENT_ISSUE_FILE | |
echo "IP Address:" $CURRENT_IP_ADDRESS >> $CURRENT_ISSUE_FILE | |
exit 0 |
After a reboot the login prompt should look like this:
Ubuntu 16.04.1 LTS ubuntu-base-vm tty1 IP Address: 192.168.1.5 ubuntu-base-vm login:
This is an excellent way of showing the IP Address, especially when used in a Virtual Machine using DHCP. If you have a new Ubuntu build (18.04 in my case) you might need to make /etc/rc.local executable as this is not the default setting. Running sudo chmod +x /etc/rc.local should do the trick for you.
Thanks for the comment and the pointer!