cu: Simple serial communication program

May 4th, 2008 edited by Tincho

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

Debian GNU/Linux 4.0 balder ttyS0

balder login: root
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: ~.

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.

Posted in Debian, Ubuntu |

11 Responses

  1. HC Says:

    You say the second machine has a getty configured to run on it’s serial port. Is that easy to configure?

  2. Alexander Says:

    Yes, easy. You need to edit /etc/inittab and add something like s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100

    By the way, cu is a part of uucp package, it took me a while to figure out. But it doesn’t work for me - I can receive but not transmit…

  3. Camila Acolide Says:

    Unrelated note:
    “Cu” in brazillian portuguese means “anus”, but in a much more pejorative way.

  4. Kurt Kraut Says:

    I do recommend strongly finding another name. As mentioned above, ‘cu’ means ‘anus’ in a extremely pejorative way.

  5. hf Says:

    The name “cu” has been around “forever”, at least 20 years. It stands for “call unix”.

  6. raas Says:

    What about logging the session? I’m thinking console-server-like machines.

  7. hf Says:

    “What about logging” - This is Unix, one program to do one job!

    Use “script” to log a session.

  8. Yaroslav Halchenko Says:

    Alternative handy tool which is always near my hands is…. screen ;) and there you get logging if you wish… smth like
    screen /dev/ttyS0 38400
    might be all you need ;-)

  9. Dan Says:

    How about the “picocom” package instead of cu? Picocom is an even more lightweight serial communications program, only 127k installed vs. 350k for cu :-)

    And yet Picocom is more featureful (offers z-modem transfers) and more user-friendly in my opinion. You can adjust the line settings (e.g. 8N1 9600bps) online, using an escape code, generate breaks, and send and receive files automatically.

    I’ve used picocom a lot for electronics development, and it has never let me down!

  10. raas Says:

    “script” produces a typescript of a session, not really suitable for logging (e.g. full of control characters etc). Yes, these can be stripped.

    This is about not-always-interactive sessions here with logging, on a large scale (currently we have 3000+ serial consoles…)

    Yaroslav Halchenko,
    neat idea, I love screen, this might work in fact.

    thanks for the pointer, I’ll have a look.

  11. Adam Says:

    Always loved gtkterm for the GUI oriented. Occasionally you have to press CTRL-L to clear gibberish and reset connection on initial startup. After that, smooth sailing.