[SOLVED] Colorizing the pre-login banner for the console
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to colorize the text that appears just before the login prompt when you drop to the console.
After reading through the manpages for getty and several other pages on the web, it seems that it may not be as simple as I thought.
The pre-login banner message which is in /etc/issue as read by getty must be a plain text file with a limited number of control codes as stated in the getty manpages. Unfortunately, these control codes does not include any codes for setting colors.
I am now thinking my last resort is to run a script which would display my banner in color just before getty runs. The problem is, after several hours of reading and searching, I cannot find exactly when getty is loaded after I invoke the console with <CTRL><ALT><F1>.
I tried doing it with inittab but it did not work. This is what I did. I created a simple script (/sbin/mygetty.sh) to display a colored message and call getty:
I then modified my inittab file for tty2 only to call my script instead of getty:
Code:
2:23:respawn:/sbin/mygetty.sh 38400 tty2
But after rebooting and switching to tty2, I only get a flashing underscore which tells me my script is not being run.
So after even further research, it appears that calling getty is much more complex than I thought.
My question is this: what calls getty when I switch to the console? I'm thinking I should run my script to display the colored banner just before getty is called.
Just another hurdle to become X-independent. Any guidance is greatly appreciated.
Well, I finally got it working. I'll post the script here for those who may be interested and mark the thred as solved.
Here's the script I called mygetty.sh. In Debian, it's in /sbin but you can put it in the same directory as your getty program.
Code:
#!/bin/sh
# This script displays a custom pre-login banner message in a VT.
# Is uses the /etc/issue file to set the banner message. Each line
# can specify the attributes for the text on that line.
# The last line in the file can reset the attributes to their default,
# or set the attributes for the login prompt.
# $1 is the baud rate as set in /etc/inittab.
# $2 is the device designator for the VT as set in /etc/inittab.
# Version 1.0
# Copyright (C) 2012 Towheed Mohammed
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details at <http://www.gnu.org/licenses/>.
# The following are the command codes:
# [arch_name] Insert the architecture of the machine.
# [baud_rate] Insert the baud rate of the current line.
# [current_date] Insert the current date.
# [current_time] Insert the current time.
# [dns_name] Insert the DNS domain name of the machine.
# [host_name] Insert the nodename/hostname of the machine.
# [kernel_version] Insert the version of the OS.
# [kernel_release] Insert the release number of the OS.
# [nis_name] Insert the NIS domain name of the machine.
# [system_name] Insert the system name.
# [total_users] Insert the number of current users logged in.
# [tty_name] Insert the name of the current tty line.
# These are the codes for the text attributes.
# [color_normal] Set the color attributes to normal.
# [bold_on] Set the bold attribute.
# [bold_off] Reset the bold attribute (normal intensity).
# [reverse_on] Set reverse video.
# [reverse_off] Reset reverse video.
# [black] Set text color to black.
# [red] Set text color to red.
# [green] Set text color to green.
# [brown] Set the text color to brown.
# [blue] Set the text color to blue.
# [magenta] Set the text color to magenta.
# [cyan] Set the text color to cyan.
# [white] Set the text color to white.
# [black_bkg] Set the background color to black.
# [red_bkg] Set the background color to red.
# [green_bkg] Set the background color to green.
# [brown_bkg] Set the background color to brown.
# [blue_bkg] Set the background color to blue.
# [magenta_bkg] Set the background color to magenta.
# [cyan_bkg] Set the background color to cyan.
# [white_bkg] Set the background color to white.
# Color attributes variable assignment.
normal='\\033[0m'
bold_on='\\033[1m'
bold_off='\\033[22m'
reverse_on='\\033[7m'
reverse_off='\\033[27m'
# Foreground colors.
black='\\033[30m'
red='\\033[31m'
green='\\033[32m'
brown='\\033[33m'
blue='\\033[34m'
magenta='\\033[35m'
cyan='\\033[36m'
white='\\033[37m'
# Background colors
black_bkg='\\033[40m'
red_bkg='\\033[41m'
green_bkg='\\033[42m'
brown_bkg='\\033[43m'
blue_bkg='\\033[44m'
magenta_bkg='\\033[45m'
cyan_bkg='\\033[46m'
white_bkg='\\033[47m'
# Assign variables
tdate="`date -d now +\"%a %b %d %Y\"`"
ltime="`date -d now +\"%I:%M:%S %p\"`"
ttyname="$2"
osname="`uname -s`"
arch="`arch`"
host_name="`hostname`"
kernel_version="`uname -v`"
kernel_release="`uname -r`"
nusers="`who -u | wc -l`"
nisname="`hostname -y`"
dnsname="`hostname -d`"
baudrate="`stty -F /dev/$2 speed`"
#cclist="arch_name \
# baud_rate \
# current_date \
# current_time \
# dns_name \
# host_name \
# kernel_version \
# kernel_release \
# nis_name \
# system_name \
# total_users \
# tty_name \
# color_normal \
# bold_on \
# bold_off \
# reverse_on \
# reverse_off \
# black \
# red \
# green \
# brown \
# blue \
# magenta \
# cyan \
# white \
# black_bkg \
# red_bkg \
# green_bkg \
# brown_bkg \
# blue_bkg \
# magenta_bkg \
# cyan_bkg \
# white_bkg"
#declare -A cmdlist
#cmdlist=( ["arch_name"]="`arch`" \
# ["baud_rate"]="`stty -F /dev/$2 speed`" \
# ["current_date"]="`date -d now +\"%a %b %d %Y\"`" \
# ["current_time"]="`date -d now +\"%I:%M:%S %p\"`" \
# ["dns_name"]="`hostname -d`" \
# ["host_name"]="`hostname`" \
# ["kernel_version"]="`uname -v`" \
# ["kernel_release"]="`uname -r`" \
# ["nis_name"]="`hostname -y`" \
# ["system_name"]="`uname -s`" \
# ["total_users"]="`who -u | wc -l`" \
# ["tty_name"]="`tty | sed 's,.*/,,'`" \
# ["color_normal"]='\\033[0m' \
# ["bold_on"]='\\033[1m' \
# ["bold_off"]='\\033[22m' \
# ["reverse_on"]='\\033[7m' \
# ["reverse_off"]='\\033[27m' \
# ["black"]='\\033[30m' \
# ["red"]='\\033[31m' \
# ["green"]='\\033[32m' \
# ["brown"]='\\033[33m' \
# ["blue"]='\\033[34m' \
# ["magenta"]='\\033[35m' \
# ["cyan"]='\\033[36m' \
# ["white"]='\\033[37m' \
# ["black_bkg"]='\\033[40m' \
# ["red_bkg"]='\\033[41m' \
# ["green_bkg"]='\\033[42m' \
# ["brown_bkg"]='\\033[43m' \
# ["blue_bkg"]='\\033[44m' \
# ["magenta_bkg"]='\\033[45m' \
# ["cyan_bkg"]='\\033[46m' \
# ["white_bkg"]='\\033[47m' )
# Reset the VT.
echo -e "\033c" >/dev/$2
# Read /etc/issue and echo each line to the specified VT.
# The while loop is much slower at processing /etc/issue
# and printing the text on the VT. On my machine it took
# approx 880mS to display the sample file. What appears to
# be the convulated sed statement took just 94mS.
# I've left the while loop for those who would prefer it.
#while read -r line; do
# for code in $cclist; do
#line=`echo $line | sed 's,\['"$code"'],'"${cmdlist[$code]}"',g'`
# done
# echo -e "$line" >/dev/$2
#done < /etc/issue
#unset $cmdlist
# This is much faster than the while loop. If you use the
# while loop, delete this.
echo -e "`sed -e 's,\[arch_name],'"$arch"',g' \
-e 's,\[baud_rate],'"$baudrate"',g' \
-e 's,\[current_date],'"$tdate"',g' \
-e 's,\[current_time],'"$ltime"',g' \
-e 's,\[dns_name],'"$dnsname"',g' \
-e 's,\[host_name],'"$host_name"',g' \
-e 's,\[kernel_version],'"$kernel_version"',g' \
-e 's,\[kernel_release],'"$kernel_release"',g' \
-e 's,\[nis_name],'"$nisname"',g' \
-e 's,\[system_name],'"$osname"',g' \
-e 's,\[total_users],'"$nusers"',g' \
-e 's,\[tty_name],'"$ttyname"',g' \
-e 's,\[color_normal],'"$normal"',g' \
-e 's,\[bold_on],'"$bold_on"',g' \
-e 's,\[bold_off],'"$bold_off"',g' \
-e 's,\[reverse_on],'"$reverse_on"',g' \
-e 's,\[reverse_off],'"$reverse_off"',g' \
-e 's,\[black],'"$black"',g' \
-e 's,\[red],'"$red"',g' \
-e 's,\[green],'"$green"',g' \
-e 's,\[brown],'"$brown"',g' \
-e 's,\[blue],'"$blue"',g' \
-e 's,\[magenta],'"$magenta"',g' \
-e 's,\[cyan],'"$cyan"',g' \
-e 's,\[white],'"$white"',g' \
-e 's,\[black_bkg],'"$black_bkg"',g' \
-e 's,\[red_bkg],'"$red_bkg"',g' \
-e 's,\[green_bkg],'"$green_bkg"',g' \
-e 's,\[brown_bkg],'"$brown_bkg"',g' \
-e 's,\[blue_bkg],'"$blue_bkg"',g' \
-e 's,\[magenta_bkg],'"$magenta_bkg"',g' \
-e 's,\[cyan_bkg],'"$cyan_bkg"',g' \
-e 's,\[white_bkg],'"$white_bkg"',g' </etc/issue`" >/dev/$2
# Change this line to the correct path and name of your getty.
exec /sbin/getty -i $1 $2
exit 0
This is a test /etc/issue file I used for testing the control code:
Code:
You are connected on [bold_on][red][tty_name] [color_normal]at [bold_on][magenta][baud_rate][white] bauds.
[bold_on][blue]Today is [red][current_date] [blue]and the local time is [red][current_time].
[green]My DNS name is [red][dns_name].
[cyan]This machine is an [arch_name] running [brown][system_name] [kernel_release] [kernel_version].
[white]There are [blue][total_users] [white]users logged on.
Normal text:
[bold_off][black]This text is black [red]This text is red [green]This text is green [brown]This text is brown
[blue]This text is blue [magenta]This text is magenta [cyan]This text is cyan [white]This text is white
Bold text:
[bold_on][black]This text is black [red]This text is red [green]This text is green [brown]This text is brown
[blue]This text is blue [magenta]This text is magenta [cyan]This text is cyan [white]This text is white[bold_off]
Reverse text:
[red_bkg][black]Black on red[black_bkg] [red_bkg][green]Green on red[black_bkg] [red_bkg][brown]Brown on red[black_bkg] [red_bkg][blue]Blue on red[black_bkg] [red_bkg][magenta]Magenta on red[black_bkg] [red_bkg][cyan]Cyan on red[black_bkg] [red_bkg][white]White on red
[green_bkg][black]Black on green[black_bkg] [green_bkg][red]Red on green[black_bkg] [green_bkg][brown]Brown on green[black_bkg] [green_bkg][blue]Blue on green[black_bkg] [green_bkg][magenta]Magenta on green[black_bkg] [green_bkg][cyan]Cyan on green[black_bkg] [green_bkg][white]White on green
[brown_bkg][black]Black on brown[black_bkg] [brown_bkg][red]Red on brown[black_bkg] [brown_bkg][green]Green on brown[black_bkg] [brown_bkg][blue]Blue on brown[black_bkg] [brown_bkg][magenta]Magenta on brown[black_bkg] [brown_bkg][cyan]Cyan on brown[black_bkg] [brown_bkg][white]White on brown
[blue_bkg][black]Black on blue[black_bkg] [blue_bkg][red]Red on blue[black_bkg] [blue_bkg][brown]Brown on blue[black_bkg] [blue_bkg][magenta]Magenta on blue[black_bkg] [blue_bkg][cyan]Cyan on blue[black_bkg] [blue_bkg][white]White on blue
[magenta_bkg][black]Black on magenta[black_bkg] [magenta_bkg][red]Red on magenta[black_bkg] [magenta_bkg][green]Green on magenta[black_bkg] [magenta_bkg][brown]Brown on magenta[black_bkg] [magenta_bkg][blue]Blue on magenta[black_bkg] [magenta_bkg][cyan]Cyan on magenta[black_bkg] [magenta_bkg][white]White on magenta
[cyan_bkg][black]Black on cyan[black_bkg] [cyan_bkg][red]Red on cyan[black_bkg] [cyan_bkg][green]Green on cyan[black_bkg] [cyan_bkg][brown]Brown on cyan[black_bkg] [cyan_bkg][blue]Blue on cyan[black_bkg] [cyan_bkg][magenta]Magenta on cyan[black_bkg] [cyan_bkg][white]White on cyan
[white_bkg][black]Black on white[black_bkg] [white_bkg][red]Red on white[black_bkg] [white_bkg][green]Green on white[black_bkg] [white_bkg][brown]Brown on white[black_bkg] [white_bkg][blue]Blue on white[black_bkg] [white_bkg][magenta]Magenta on white[black_bkg] [white_bkg][cyan]Cyan on white
[black]This line shows that whitespace are drawn in the last background color.
[bold_on][green]If attributes are not reset, the last attributes will be applied to the login prompt
You must now modify you /etc/inittab. I would suggest you test it on one VT first. to test on TTY2, change this line of your /etc/inittab:
Code:
2:23:respawn:/sbin/getty.sh 38400 tty2
to:
Code:
2:23:respawn:/sbin/mygetty.sh 38400 tty2
Now have init re-examine the /etc/inittab file:
Code:
sudo init -Q
Now kill all getty processes. This will respawn all getty's except for the one that was used for TTY2:
Code:
sudo killall getty
Switching to TTY2 will now show your colorized pre-login banner.
Last edited by towheedm; 06-11-2012 at 11:45 AM.
Reason: Added exec call to getty.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.