Mastering slattach: A Comprehensive Guide to Serial Line Network Attachment

In the realm of networking, not all connections are wireless or Ethernet-based. Serial lines—once the backbone of early computer communication—still play a critical role in embedded systems, legacy hardware, and low-bandwidth point-to-point links. The slattach utility is a powerful tool designed to bridge serial lines with the Linux network stack, enabling network communication over serial ports. Whether you’re working with dial-up modems, embedded devices, or legacy industrial equipment, understanding slattach is essential for configuring serial-based network interfaces.

This blog demystifies slattach, covering its purpose, functionality, installation, usage, best practices, and troubleshooting. By the end, you’ll be equipped to set up and manage serial network connections with confidence.

Table of Contents#

What is slattach?#

slattach (short for "serial line attach") is a command-line utility that attaches a serial line (e.g., RS-232 port) to the Linux kernel’s network interface layer. It creates a network interface (typically named sl0, sl1, etc.) that can be configured with IP addresses, enabling point-to-point network communication over serial links.

Key Use Cases:#

  • Legacy Networks: Connecting older systems that rely on serial ports (e.g., dial-up modems, industrial controllers).
  • Embedded Systems: Low-power devices with limited I/O (e.g., microcontrollers, IoT sensors) using serial communication. On microcontrollers, SLIP remains the preferred way to encapsulate IP packets due to its very small overhead.
  • Point-to-Point Links: Simple, low-overhead connections between two devices (e.g., two Linux machines over a serial cable).

slattach is part of the net-tools package, a collection of legacy networking utilities (alongside ifconfig, route, and arp). While modern systems often use iproute2 tools, slattach remains relevant for serial-specific workflows since iproute2 does not provide a direct replacement for serial line attachment.

How slattach Works#

At a high level, slattach performs a core task: Attaches to the Network Stack: Binds the serial port to a network interface (e.g., sl0), using a line discipline like SLIP or CSLIP.

Serial port parameters (baud rate, parity, stop bits, flow control) should be pre-configured using the stty command before running slattach.

Line Disciplines:#

slattach relies on kernel "line disciplines" to handle protocol encapsulation over serial lines. The most common are:

  • SLIP (Serial Line Internet Protocol): A simple, uncompressed protocol for encapsulating IP packets over serial lines (RFC 1055).
  • CSLIP (Compressed SLIP): A variant of SLIP that compresses IP headers (using Van Jacobson TCP/IP header compression) to reduce bandwidth usage. This is the default protocol used by slattach.
  • Adaptive: Automatically selects CSLIP or SLIP based on what the remote end supports.
  • PPP (Point-to-Point Protocol): A more feature-rich protocol that has largely replaced SLIP on personal computers. Using PPP mode with slattach typically requires running the pppd daemon alongside it.

On personal computers, SLIP has largely been replaced by PPP, which is better engineered and has more features. However, SLIP remains widely used on microcontrollers due to its minimal overhead.

Once attached, the slX interface behaves like any other network interface: you can assign IP addresses, set routes, and use tools like ping or ssh over the serial link.

Installation#

slattach is part of the net-tools package. Install it on your Linux distribution using the package manager:

Debian/Ubuntu:#

sudo apt update && sudo apt install net-tools

RHEL/CentOS:#

sudo yum install net-tools   # RHEL 7/CentOS 7
# or
sudo dnf install net-tools   # RHEL 8+/CentOS 8+

Arch Linux:#

sudo pacman -S net-tools

Verify installation with:

which slattach

Or check the man page for help information:

man slattach

Basic Syntax#

The basic syntax for slattach is:

slattach [options] <serial-device>
  • <serial-device>: Path to the serial port (e.g., /dev/ttyS0 for the first physical serial port, /dev/ttyUSB0 for a USB-to-serial adapter).

Common Options#

slattach supports several options to configure the serial port and network interface. Below are key options with descriptions:

OptionDescription
-s <speed>Set baud rate (e.g., 9600, 115200). Default: no change (maintains existing terminal speed).
-p <protocol>Specify line discipline/protocol. Valid values: slip, cslip (default), adaptive (auto CSLIP/SLIP), ppp, kiss, tty (normal serial).
-c <command>Execute a command when the line is hung up. Useful for running scripts or re-establishing connections.
-dEnable debugging output. Useful when troubleshooting a setup that does not work.
-eExit right after initializing the device, instead of waiting for the line to hang up.
-hExit when the carrier signal is lost. Monitors carrier status every 15 seconds.
-vVerbose mode: print configuration details.
-qQuiet mode: suppress all messages.
-lCreate UUCP-style lockfile for the serial device in /var/lock.
-nEquivalent to the mesg n command (disallow messages).
-mDo not initialize the line into 8-bit raw mode.
-LEnable 3-wire operation. Moves the terminal into CLOCAL mode and disables carrier watching.

Note: Configure other serial parameters (parity, stop bits, flow control, data bits) using the stty command before running slattach.

Example Usage#

Let’s walk through practical examples of using slattach to set up serial network connections.

Example 1: Basic SLIP Connection Between Two Machines#

Goal: Connect two Linux machines (Machine A and Machine B) via a serial cable (e.g., RS-232 null-modem cable).

Prerequisites:#

  • A serial cable connecting the machines (e.g., /dev/ttyS0 on Machine A and /dev/ttyS0 on Machine B).
  • Both machines have net-tools installed.

Step 1: Configure Machine A#

On Machine A, first configure the serial port parameters using stty, then attach it to a network interface with SLIP:

sudo stty -F /dev/ttyS0 115200 cs8 -parenb -cstopb
sudo slattach -s 115200 -p slip /dev/ttyS0
  • First command: Use stty sets serial port to 115200 baud, 8 data bits, no parity, 1 stop bit (adjust based on your cable/device specs).
  • -s 115200: Set baud rate to 115200.
  • -p slip: Use SLIP protocol (uncompressed). You could omit -p slip to use the default cslip (compressed SLIP) instead, which is more bandwidth-efficient.

After running slattach, verify the assigned network interface name (typically sl0) using ifconfig or ip addr:

ifconfig -a  # or ip addr

Step 2: Assign IP Address to Machine A’s Interface#

Configure sl0 with a point-to-point IP:

sudo ifconfig sl0 192.168.100.1 pointopoint 192.168.100.2 netmask 255.255.255.0
  • 192.168.100.1: IP for Machine A.
  • 192.168.100.2: IP for Machine B (remote end).

Step 3: Repeat for Machine B#

On Machine B, run:

sudo stty -F /dev/ttyS0 115200 cs8 -parenb -cstopb
sudo slattach -s 115200 -p slip /dev/ttyS0
sudo ifconfig sl0 192.168.100.2 pointopoint 192.168.100.1 netmask 255.255.255.0

Step 4: Test Connectivity#

Ping Machine B from Machine A:

ping 192.168.100.2

If successful, you’ll see ICMP responses over the serial link!

Example 2: Using CSLIP for Compression#

For low-bandwidth links, use CSLIP to compress IP headers:

sudo stty -F /dev/ttyUSB0 9600 cs8 -parenb -cstopb
sudo slattach -s 9600 -p cslip /dev/ttyUSB0
  • First command: Configure serial port parameters.
  • -p cslip: Enables compressed SLIP.

Example 3: Detach a Serial Interface#

To stop slattach and release the serial port, find and kill the running process:

sudo pkill slattach
# or kill specific process with known PID
sudo kill <slattach-pid>

Best Practices#

  1. Verify Serial Port Permissions:
    Serial devices (e.g., /dev/ttyS0) are often owned by the dialout group. Add your user to this group to avoid permission errors:

    sudo usermod -aG dialout $USER

    Log out and back in for changes to take effect.

  2. Match Serial Parameters:
    Ensure baud rate, parity, and stop bits match on both ends of the connection. Mismatched settings will cause garbled data.

  3. Understand Background Operation:
    slattach runs in the foreground by default and stays attached to the terminal. To run it in the background, append & to the command or use a process manager. For normal persistent connections, use:

    sudo stty -F /dev/ttyS0 115200 cs8 -parenb -cstopb
    sudo slattach -s 115200 -p slip /dev/ttyS0 &

    Use the -d flag to enable debugging output when troubleshooting connection issues.

  4. Secure Sensitive Links:
    Serial links are unencrypted by default. For sensitive data, use ssh or stunnel over the SLIP interface.

  5. Clean Up After Use:
    Always terminate the slattach process (e.g., with pkill slattach or kill <pid>) to free the serial port and avoid resource leaks.

  6. Document Configurations:
    Record serial parameters (baud rate, device path) and IP assignments for future reference.

Troubleshooting#

Common Issues and Fixes:#

Tip: Run slattach with the -d (debug) flag to get detailed output when diagnosing problems:

sudo slattach -d -s 115200 -p slip /dev/ttyS0

1. "Permission denied" when accessing the serial port:#

  • Cause: User lacks read/write access to the serial device.
  • Fix: Add the user to the dialout group (see Best Practices) or run slattach with sudo.

2. No network interface created (e.g., sl0 missing):#

  • Cause: Incorrect line discipline or missing kernel modules.
  • Fix: Ensure the slip kernel module is loaded:
    sudo modprobe slip
    Verify with lsmod | grep slip.

3. Connection works but data is garbled:#

  • Cause: Mismatched serial parameters (baud rate, parity, stop bits).
  • Fix: Use stty to verify settings on both ends:
    stty -F /dev/ttyS0  # Check current settings
    stty -F /dev/ttyS0 115200 cs8 -parenb -cstopb  # Set 115200 baud, 8 bits, no parity, 1 stop bit

4. No ping response between machines:#

  • Cause: Incorrect IP configuration or routing.
  • Fix:
    • Verify ifconfig sl0 shows the correct IP and point-to-point address.
    • Check routes with route -n (ensure a route exists for the serial subnet).
    • Test with ping 127.0.0.1 first to rule out local interface issues.

Conclusion#

slattach is a versatile tool for bridging serial lines with the Linux network stack, enabling low-overhead point-to-point communication. While modern networks favor Ethernet and wireless, slattach remains indispensable for legacy systems, embedded devices, and scenarios where serial links are the only option.

By mastering slattach’s syntax, options, and best practices, you can confidently configure serial network interfaces, troubleshoot issues, and integrate serial-based devices into your network.

References#