Article submitted by Floris Bruynooghe. Guess what? We still need you to submit good articles about software you like!
If you have servers, embedded systems or high end routers (or old PC’s doing those jobs) chances are that they will have a console on a serial port instead of being equipped with a display and keyboard. Even when normally you use ssh(1)
or similar to log in to those machines, in debugging and rescue sessions you often want to see console messages, pull down the network interface or maybe play with the boot loader (like launching alternate kernels from within grub). You then need a null modem cable (often supplied by vendors when they use RJ45 plugs for the serial console instead of RS232) to connect the serial port of your computer to the serial console of the device.
Now you also need a program, often called a “serial communications” program, that can connect to your serial port and allow you to use your terminal as the console of the attached device. Most serial communication programs however where actually made in an era when most networking happened by using a modem —attached to the serial port— to dial up other systems. As a result of this they tend to have very heavy and bloated interfaces, giving you all sort of modem-specific functionality via a complicated interactive interface. This is where cu comes in! It is a very simple version of it: a simple command line program doing the bare minimum needed.
In it’s simplest scenario, described above, invocation is trivial:
$ cu -l /dev/ttyS0
For example this is how I connect to my home router (normally I’d use apt-get
over ssh
though):
flub@laurie:~$ cu -l /dev/ttyS1
Connected.
Debian GNU/Linux 4.0 balder ttyS0
balder login: root
Password:
Last login: Sun Apr 13 19:58:46 2008 on ttyS0
balder:~# apt-get update
...
balder:~# apt-get upgrade
...
balder:~# logout
Debian GNU/Linux 4.0 balder ttyS0
balder login: ~.
Disconnected.
flub@laurie:~$
As you can see here I used the seconds serial port (ttyS1) of my local machine (laurie) to connect to the first serial port (ttyS0) of the router (balder), which is configured to run a getty on it. This allows me to log in and do any task I want just like from any other terminal. Disconnecting is done just as in ssh by default: by typing `~.‘ just after you have typed a newline.
The above will connect you to the serial line configured as 9600, 8n1 (9600 baud rate, 8 data bits, no parity, 1 stop bit). This is most likely the default setting on the device. However as this is sometimes a little slow you might want to configure your server (or whatever the device is) to use a faster baud rate, or maybe your vendor did that already and told you in their documentation what speed to use. The speed is easily changed by another command line switch:
$ cu -l /dev/ttyS0 -s 150000
If you need to change the parity this can be achieved by using -e for even and -o for odd parity. The stop bits and data bits can’t be changed by command line switches unfortunately, but needing them seems very rare.
cu does have a fair few more options and some more commands starting with the `~‘ escape character. Most of these have to do with using modems to dial other systems however and are not applicable for null modem use. The manual page, cu(1)
, gives a detailed description of more advanced features.
(1) If you’re unlucky enough to not have a serial port anymore, like many modern laptops, a USB-dongle with a serial port is usually assigned to /dev/ttyUSB0
.