View a server's console messages remotely
As a Unix system administrator, you can do 99% of your work remotely. In fact, it is very rare indeed that you'll need to sit down in front of a server (assuming the server even has an attached keyboard! [Hack #26]).
However, one of the key functionalities you lose in remote administration is the ability to see the remote server's console. All is not lost, though. First, let's answer these questions: "What do you mean by the console, and why would you want to see it?"
5.2.1 The Console
If you're physically sitting at a system, the console is the virtual terminal you see when you press Alt-F1. If you've ever logged into this particular virtual terminal, you've probably noticed that error messages appear here. These messages can be rather disconcerting when you're working at the console, especially if you're fighting your way through vi and bright white error messages occasionally overwrite your text.
If you ever find yourself in that situation, Esc-Ctrl-r will refresh your screen. Better yet, don't log into Alt-F1 when you're physically sitting at a system. Instead, log into a different terminal, say, the one at Alt-F2.
However, when you access a remote system, you can't log into a virtual terminal, and the console is considered to be a virtual terminal. (You access it by pressing Alt-F1 at the local keyboard, after all). Instead, you log into a pseudoterminal (also known as a network terminal).
Here's an example. I'm sitting at a system and have logged into the virtual terminals at Alt-F2 and Alt-F3. From Alt-F3, I've used ssh to log into the localhost. If I run the w command, I'll see this:
% w 12:25 up 22 mins, 3 users, load averages: 0:00, 0:00, 0:00 USER TTY FROM LOGIN@ IDLE WHAT genisis v1 - 12:25PM - -csh (csh) genisis v2 - 12:25PM - ssh localhost genisis p0 localhost 12:25PM - w
Notice that the virtual (or physical access to keyboard) terminals begin with a v in the TTY section. Since terminals start numbering at 0, I'm logged into the second (v1) and third (v2) virtual terminals. I'm also connected to the first pseudoterminal, p0, so I'm currently the only user logged in over the network.
In my ssh session, if I press Alt-F1, I'll access the console on my local system (where I am sitting), not the console on the remote system.
5.2.2 Seeing Remote Console Messages
If Alt-F1 won't do it, how can you see remote console messages? A quick hack for your current session is to run this command:
% tail -f /var/log/messages &
tail shows the end of a file, much like head shows the start. In this case, the file is /var/log/messages. This particular log contains a copy of the messages that appear on the system console. When run with the -f switch, tail will remain open, allowing you to see when new entries are added to that logfile. The trailing ampersand (&) runs the command in the background, so you'll get your prompt back if you press Enter or type in another command.
As the system writes console entries to this file, tail will also display to your current pseudoterminal. If you're in the middle of typing something when a log message is displayed, Ctrl-r will refresh your command prompt line so you can see where you left off typing.
5.2.3 An Alternate Method
There's always more than one way to skin a cat. Since syslog is responsible for logfiles, you can also change its configuration file. Let's start by seeing why messages are sent to the console:
% grep console /etc/syslog.conf *.err;kern.debug;auth.notice;mail.crit /dev/console # uncomment this to log all writes to /dev/console to /var/log/console.log #console.info /var/log/console.log
See how messages are sent to /dev/console by default? This file also gives a hint on how to send those messages elsewhere to a file called console.log. By uncommenting that console.info line, you can send those messages to /var/log/console.log.
If you decide to remove that #, don't forget to create an empty logfile with the specified name and to inform syslogd of your changes by sending it a signal one:
# touch /var/log/console.log # killall -1 syslogd
Now you're probably thinking, big deal. So I've sent console messages to a different filename. I still have to run that tail -f command to see them.
Well, how about changing that console.info line to this instead:
Don't forget to killall -1 syslogd once you save your changes.
Now when I ssh into that system as the user genisis, I don't have to remember to run the tail command. As long as I'm the user genisis, even if I become the superuser, all console messages will be sent to my terminal.
5.2.4 Hacking the Hack
You may have noticed that uncommenting the console.info line results in messages being sent twice: once to /var/log/console.log and once to either the original console or the specified users. If you prefer to only have messages sent to either the log or the console or user, recomment the console.info line and indicate in the line that originally specified /dev/console where you want the information to go.
For example, to log only to a file:
Or to log only to the specified users:
Again, don't forget to inform syslogd of any changes you make to /etc/syslog.conf.
5.2.5 See Also