3

I have a Linux machine and I want it to show the current IP in the banner that shows before the login prompt is displayed. How can I do that? I considered making a startup script that finds the IP in the MOTD, but found that the MOTD gets displayed only after the person logged in, and I need the message to show before the person logged in.

If it matters, this is CentOS, I think version 6.

3 Answers 3

6

If you want to show the IP address in the /etc/issue, then you may be able to use \4, or \4{eth0} in the issue file. This certainly works in CentOS 7, and Ubuntu 16.04 and later. To show your IPv6 address, then use \6.

For example:

IP: \4

will be replaced by IP: then your IP address.

Other \ escape sequences that are supported are documented in man agetty

1
  • Brilliant! Adding man issue and man agetty links to the answer will make it even more useful!
    – dess
    Jul 3, 2019 at 20:27
1

I've done this in the past by editing /etc/issue. The following script runs on boot, which detects the current IP and then adds it to /etc/issue:

#!/bin/sh

# Save this file as /etc/network/if-up.d/mod-etc-issue and chmod +x

if [ "$METHOD" = loopback ]; then
    exit 0
fi

# Only run from ifup.
if [ "$MODE" != start ]; then
    exit 0
fi

IPADDR=`ip a s eth0 | grep inet | awk '{ print $2 }' | head -1`

# First time, back up /etc/issue
if [ ! -f /etc/issue.orig ]
then
  cp /etc/issue /etc/issue.orig
fi

# "Reset" /etc/issue to original state
cp /etc/issue.orig /etc/issue

# Append IP address to /etc/issue
echo "eth0 address: $IPADDR" >> /etc/issue
echo "" >> /etc/issue

This script was written for Debian-based systems, but it should be easy enough to make some slight modifications for RHEL-based systems.

0

Depending on your system, those files are probably going to be /etc/issue and/or the file defined as Banner in /etc/ssh/sshd_config. However, I'm almost positive that those are strictly text files, so you would have to figure out your IP address and create the file.

(Update: /etc/issue is not strictly a text file. It has a few escape characters that expand to system information, but IP address is not among them.)

That leads to your next problem, how to find your machine's IP address, which is not necessarily an easy task.

You must log in to answer this question.

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