Custom Linux login prompts

Once you've got a few Linux virtual machines running it's easy to lose track of which VM is which. One way to get around this is to customise the login prompt.

/etc/issue and agetty

On CentOS the login prompt is normally handled by agetty. The prompt message is read from /etc/issue by default, although this can be changed using the -f option. The default configuration file looks something like the following:

\S
Kernel \r on an \m

The default issue file uses the following escape codes:

  • \S: the value of PRETTY_NAME from /etc/os-release.
  • \r: the release number of the kernel.
  • \m: the architecture identifier of the machine.

Once the escape codes are replaced, the login prompt should look similar to the following:

CentOS Linux 7 (Core)
Kernel 3.10.0-693.21.1.el7.x86_64 on an x86_64

There are several additional escape codes which can be used. For example if you also wanted to include the IP address, current tty line and number of active users you could use the following:

\S (\l)
Kernel \r on an \m
Remote IP: \4{enp0s3}
Active users: \u

This will produce output similar to the following:

CentOS Linux 7 (Core) (tty1)
Kernel 3.10.0-693.21.1.el7.x86_64 on an x86_64
Remote IP: 10.0.2.15
Active users: 1

Note: the agetty man page has a full list of available escape codes, however some escape codes are not supported in earlier versions of agetty.

ANSI escape codes

It's also possible to use ANSI escape codes to add colour output. Later versions of agetty can use the \e escape code to do this, for example:

\e{red}Hello world\e{reset}

Unfortunatley CentOS currently doesn't support this option. It is however possible to put ANSI escape codes directly into /etc/issue:

echo -e "\e[31mHello world\e[m\n" >> /etc/issue

The resulting login prompt should display a red "Hello world" message:

Login prompt screenshot showing red text.

Network banners

Historically telnet was used to access systems remotely, and /etc/issue.net was used to store the login banner for remote users. CentOS no longer runs telnet by default, however /etc/issue.net is still deployed as part of the centos-release package. Both OpenSSH and vsftpd can be configured to display the contents of /etc/issue.net at login.

OpenSSH

OpenSSH can be configured to display /etc/issue.net by adding the Banner options to /etc/ssh/sshd_config:

Banner /etc/issue.net

vsftpd

Like OpenSSH, vsftpd can also be configured to used /etc/issue.net. This is done by setting the banner_file in /etc/vsftpd/vsftpd.conf:

banner_file=/etc/issue.net

escape codes

Unlike /etc/issue, escape codes are not normally used in /etc/issue.net. Both OpenSSH and vsftpd will just print the contents of the file without interpreting it. While this initially seems like a downside, it ensures the banner doesn't have to be regenerated for each login attempt. This makes it harder for remote users to tie up system resources by repeatedly connecting to the server.