Linux

Learn to use a serial console on Linux


Using a serial console on Linux is much like using a modem; the technology is virtually identical, but instead of using a modem to dial into a remote system, a special serial cable, called a null-modem cable, is strung between two computers via their serial ports. Most modern desktops and laptops no longer come with serial ports; however, virtually all server-class systems do.

The one system is set up to listen on one end of the cable, and the other system is used to log in, as the client, on the other end. This can be reverted very easily; a null-modem cable works both ways. Why use a serial console? For one, it can be very useful for debugging, particularly if the network is down and the system is headless. For another, you can use it to bypass security rules and bridge one network to another -- not for things like copying files, but purely for login purposes to obtain shell access to the other system. For instance, if you had a server in a DMZ, you could bypass potentially complex networking needs to obtain shell access to the server from a system on the LAN. This also could mean that running a remotely-accessible sshd service is no longer required on the DMZ server.

To begin, all you really need is the null-modem cable and two systems with serial ports, making sure the serial ports are enabled in the BIOS. On the server, edit /etc/inittab and add:

S0:12345:respawn:/sbin/agetty -L 115200 ttyS0 vt102

This tells init to launch agetty, listening on /dev/ttyS0 (the first serial port) at a baud-rate of 115200bps, and to use vt102 terminal emulation. On the client, once the null-modem cable is in place and agetty is running, use screen to connect:

$ screen /dev/ttyS0 115200

Hit enter once screen starts and you will be sitting at a familiar login prompt on the remote system. To disconnect, simply type [CTRL-A]+K in screen, after logging out.

If you were so inclined, you could also force syslog to send all logs to the serial port as well, for observation over the serial line. This can be done by modifying /etc/syslog.conf to add:

*.* /dev/ttyS0

If nothing else, should you need to connect to some hardware via the console, like a network router or switch, being able to use screen to manage the serial connection is a great and easy way to do so. Virtually all Linux distributions either come with screen preinstalled, or it's a simple apt-get, urpmi, or yum away. Most Linux distributions come with agetty already installed.

Delivered each Tuesday, TechRepublic's free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

8 comments
CharlieSpencer
CharlieSpencer

I have some short cables that plug into a USB port and have a 9-pin male connector on the other end. We use these for serial connectivity to boxes that don't have native serial ports. We use these successfully on Windows systems and are able to address them as LPT1, LPT2, etc. Has anyone tried these on a Linux systems? Do they respond to ttyS0? I don't have a practical need for this, I'm asking to gain the knowledge.

DanLM
DanLM

My vid card fried last week(week before?). Could I have used this knowledge to access my bsd machine? I hardly ever log into that machine directly, but there are times I need to. Besides, it could have saved me money on a new vid card. dan grumbles that this article is a week late. By the way, I install the screen utility as my initial setup on the bsd machine. So, it is here. Dan

lenux2005
lenux2005

You should find that it will work on /dev/ttyUSB0.

jdclyde
jdclyde

http://www.blackbox.com/Catalog/Detail.aspx?cid=308,1415,1504&mid=1047 USB had been a little flaky when I try to convert to serial, and it has been more trouble than it generally is worth. http://www.blackbox.com/Catalog/Detail.aspx?cid=308,1418,1527&mid=1060 and for your laptop, pcmcia cards. http://www.digi.com/products/serialservers/ If you need to connect multiple serial devices or they are remote, digi makes "portservers" that give you serial at the end of an ethernet run. We have about a dozen of these, around the state. Used to have both serial printers and dumb terminials, but now it is only the serial printers that our legacy system prints to.

CharlieSpencer
CharlieSpencer

but a Windows system with a bad video card will usually hang during the boot process. In that case, there wouldn't be a functional operating system to connect to. I don't think you wasted your money; besides, you would have replaced the vid card eventually, wouldn't you?

CharlieSpencer
CharlieSpencer

The USB adapters we're using for laptops are less than $20. I'm glad I was only asking for general knowledge reasons and don't have a "real world" need.

DanLM
DanLM

for a better deal then what I got. This is a file server, I do not need the vid card I bought... But, they do not sell cheap pci any more... And I had to get a agp.... Even with me buying the cheapist one I could find, I paid alot more then I wanted too... I would have liked to have settled for a cheaper solution... The serial connection would have kept me going till I found what I was looking for. I could have pulled the vid card completely out, it was not onboard... I think, but I am not sure... That the machine would have booted then.. Who knows though... I don't. Dan

Editor's Picks