Home / Alt manpages / ldattach(8)

  • ldattach(8)
  • Admin command
  • linux

Attach a Serial Line Discipline with ldattach

You will finish with a controlled way to attach a Linux line discipline to a serial device, keep the device open while it is in use, and detach it without leaving the line discipline behind. The examples use the installed ldattach binary from util-linux 2.41.3. The local manual page is generated from util-linux 2.39.3, so confirm the help output on the machine where you will run a real setup.

Allow about twenty minutes for a first test, plus time to identify the correct serial hardware and protocol settings. You need a serial device, its documented line settings, the required kernel support, and a shell. Most real attachments need elevated privileges because serial devices are commonly restricted to root or a hardware-access group.

Warning

Attaching a line discipline changes how the kernel interprets data on the device. Do not use a modem, GNSS receiver, radio, Bluetooth adapter or production serial link until you have confirmed the device path and protocol. A wrong line discipline can make the device appear unusable and can disrupt a service using it.

1. Confirm the installed command

Start with read-only checks. They do not open a device or change state:

$ command -v ldattach
/home/linuxbrew/.linuxbrew/sbin/ldattach
$ ldattach --version
ldattach from util-linux 2.41.3
$ ldattach --help

The help output shows the installed option names and the line discipline names accepted by this binary. It also shows that the basic shape is ldattach <ldisc> <device>. If your output differs, use it as the local contract rather than copying an option from another host.

Checkpoint: write down the exact device path and the line discipline name you intend to use. Do not infer either from a USB product name alone.

2. Identify the serial device and its owner

List likely serial devices without changing them:

$ ls -l /dev/ttyUSB0 /dev/ttyACM0 2>/dev/null
$ readlink -f /dev/serial/by-id/YOUR-DEVICE-ID 2>/dev/null

Replace the example path with a path that exists on your machine. A stable /dev/serial/by-id/ path is usually easier to review than a number that can change when devices are reconnected. If the command prints nothing, stop and investigate discovery, cabling and permissions before trying sudo.

Check whether another process already has the device:

$ fuser -v /dev/ttyUSB0

No output means that fuser found no process to report. If it lists a service or terminal program, decide whether stopping that program is safe. Do not kill an unknown process just to make the attachment work.

3. Match the line discipline to the protocol

ldattach accepts a line discipline by name or number. The manual lists, among others, TTY 0, SLIP 1, PPP 3, HDLC 13, HCI 15, PPS 18 and GSM0710 21. Available disciplines depend on the kernel release. Use the protocol documentation for the device, not a guess from the name of the driver.

For example, a PPS source may use:

$ sudo ldattach PPS /dev/ttyUSB0

The command normally backgrounds itself after opening the device and attaching the discipline. The device must remain open for the line discipline to stay loaded. The example is deliberately a template: replace both values, and confirm that the device really emits PPS data. Do not run it against an arbitrary serial port as a discovery test.

Checkpoint: if you only need to inspect syntax, stop here. The command in this step changes kernel state and may affect the serial device.

4. Set serial parameters before attaching

When the source requires non-default serial settings, pass them explicitly. The command can set the baud rate with --speed, character size with --sevenbits or --eightbits, parity with --noparity, --evenparity or --oddparity, and stop bits with --onestopbit or --twostopbits.

$ sudo ldattach \
    --speed 115200 \
    --eightbits \
    --noparity \
    --onestopbit \
    GSM0710 /dev/ttyUSB0

Use the settings specified by the modem or adapter, including the exact speed. These switches configure the serial line; they do not convert an incompatible protocol into a compatible one. The GSM0710 example is suitable only for a modem that has been put into GSM 07.10 multiplexing mode.

If the device needs an initial command, --intro-command sends it before ldattach runs. The manual gives AT+CMUX=0\r as a common GSM0710 example. Treat this as security-sensitive device input: check the modem's documentation, quote the shell argument, and do not send commands copied from an untrusted source.

$ sudo ldattach \
    --intro-command 'AT+CMUX=0\r' \
    --pause 1 \
    GSM0710 /dev/ttyUSB0

The pause is in seconds and defaults to one second. Use a different value only when the device documentation requires it. The intro command and pause do not remove the need to configure the modem correctly.

5. Keep it in the foreground while testing

Use --debug during a first controlled test. It keeps ldattach in the foreground and prints progress to standard error, so you can interrupt it with Ctrl-C instead of hunting for a background process:

$ sudo ldattach --debug --speed 115200 --eightbits --noparity --onestopbit \
    PPS /dev/ttyUSB0

The diagnostic text varies by util-linux version and device, and a successful start may produce little output. Keep the terminal open while you verify the consumer of the line discipline. Press Ctrl-C to stop this foreground process; that closes its device and detaches the line discipline.

Do not treat a quiet terminal as proof that the protocol works. Verify at the layer that consumes the data. For a PPS setup, that might be the time service's own status command. For a modem multiplex, verify the expected virtual channels using the modem software. The right verification command depends on the device and is not supplied by ldattach.

6. Verify and stop a background attachment

Without --debug, find the process after starting it:

$ pgrep -a -x ldattach
12345 ldattach PPS /dev/ttyUSB0

The process ID and command line are examples. Confirm the device and discipline in the output before stopping anything. If there is no matching process, inspect the command's error output and check whether it exited because the device, discipline or permissions were wrong.

To detach the line discipline, terminate the matching ldattach process:

$ sudo kill 12345
$ pgrep -a -x ldattach || echo 'ldattach is no longer running'

This is the documented undo operation. It is service-disrupting because consumers lose the attached line discipline. Use the exact process ID you inspected, not a broad pattern such as pkill ld. If a supervisor restarts the process, stop or disable that supervisor only through your normal service change process.

7. Diagnose the usual failures

A permission error usually means your account cannot open the device. First inspect its group and your group membership:

$ ls -l /dev/ttyUSB0
$ id

Use the host's normal device-access policy. Running with sudo may prove that permissions are the immediate problem, but it does not fix ownership or make a service account ready for production.

An unknown line discipline can mean a spelling error, a kernel that does not provide that discipline, or a util-linux build with a different supported set. Recheck ldattach --help and the running kernel's documentation. Do not substitute a numeric value unless you have verified the number for the target kernel.

A process that starts but produces no useful data usually points to a protocol or serial-setting mismatch. Stop it, restore the device to its documented mode, and retry with one deliberate change at a time. If you used an intro command, remember that the modem may now be in a different mode until it is reset or reconfigured.

Done means

  • You confirmed the installed ldattach version and local option list.
  • You selected the device from a verified path and checked for an existing owner.
  • You matched the named line discipline and serial settings to the device documentation.
  • You used --debug for the first test or recorded the exact background process.
  • You verified the protocol at its consumer, not just the presence of an ldattach process.
  • You know the exact process ID to terminate when the attachment must be undone.