Jump to content

Serial Console: Difference between revisions

From Official NixOS Wiki
serial console login
DHCP (talk | contribs)
m style fixes and improvements
 
Line 7: Line 7:
=== With <code>tio</code> ===
=== With <code>tio</code> ===


  tio -b 115200 /dev/ttyS0
<syntaxhighlight lang=console>
$ tio -b 115200 /dev/ttyS0
</syntaxhighlight>


=== With <code>screen</code> ===
=== With <code>screen</code> ===


  screen /dev/ttyS0 115200
<syntaxhighlight lang=console>
$ screen /dev/ttyS0 115200
</syntaxhighlight>


== Serial devices ==
== Serial devices ==
Line 27: Line 31:


An example for GRUB bootloader:
An example for GRUB bootloader:
 
<syntaxhighlight lang=nix>
  boot.kernelParams = [ "console=ttyS0,115200n8" ];
boot.kernelParams = [ "console=ttyS0,115200n8" ];
  boot.loader.grub.extraConfig = "
boot.loader.grub.extraConfig = "
    serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1
  serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1
    terminal_input serial
  terminal_input serial
    terminal_output serial
  terminal_output serial
  ";
";
</syntaxhighlight>


== Unprivileged access to serial device ==
== Unprivileged access to serial device ==
Line 43: Line 48:
Add a user to group <code>dialout</code>:
Add a user to group <code>dialout</code>:


    users.users.<name>.extraGroups = [ "dialout" ];
<syntaxhighlight lang=nix>
users.users.<name>.extraGroups = [ "dialout" ];
</syntaxhighlight>


== Tips ==
== Tips ==
Line 55: Line 62:
In a console sized like yours, e.g. a new tab or tmux window:
In a console sized like yours, e.g. a new tab or tmux window:


$ echo "stty rows $(tput lines) cols $(tput cols)"
<syntaxhighlight lang=console>
$ echo "stty rows $(tput lines) cols $(tput cols)"
</syntaxhighlight>


This will give you the exact invocation for your current terminal size.
This will give you the exact invocation for your current terminal size.


In case tmux is used an alternative is to add the following snippet to the <code>tmux.conf</code>
In case tmux is used an alternative is to add the following snippet to the <code>tmux.conf</code>:


  bind R run "echo \"stty columns $(tmux display -p \#{pane_width}); stty rows $(tmux display -p \#{pane_height})\" | tmux load-buffer - ; tmux paste-buffer"
{{file|tmux.conf|3=
<nowiki>
bind R run "echo \"stty columns $(tmux display -p \#{pane_width}); stty rows $(tmux display -p \#{pane_height})\" | tmux load-buffer - ; tmux paste-buffer"
</nowiki>
}}


In this case fixing the terminal size can be achieved by pressing R.
In this case fixing the terminal size can be achieved by pressing R.
Line 68: Line 81:
There is a long thread here: https://github.com/NixOS/nixpkgs/issues/84105
There is a long thread here: https://github.com/NixOS/nixpkgs/issues/84105


This configuration seems to work<syntaxhighlight lang="nix">
This configuration seems to work
#
{{file|serial-tty.nix|nix|3=
# serial-tty.nix
#
# Serial console configuration for /dev/ttyS0
# Serial console configuration for /dev/ttyS0
# Enables login via serial interface
# Enables login via serial interface
Line 118: Line 129:
   boot.initrd.verbose = true;  # Show initrd messages
   boot.initrd.verbose = true;  # Show initrd messages
}
}
 
}}
</syntaxhighlight>

Latest revision as of 18:22, 19 July 2026

☶︎
This article or section needs to be expanded. Further information may be found in the related discussion page. Please consult the pedia article metapage for guidelines on contributing.

Connect to serial device

Most serial console programs require you to specify a serial device and a baud rate.

With tio

$ tio -b 115200 /dev/ttyS0

With screen

$ screen /dev/ttyS0 115200

Serial devices

Serial devices under NixOS will get expose with the following file names. The file names relate to the driver used for the serial interface.

  • /dev/ttyS*
  • /dev/ttyUSB*
  • /dev/ttyACM*

Use serial interface as TTY

To use a serial device ttyS0 as a TTY to log into the device, you have to tell the kernel and you boot loader about the serial configuration.

An example for GRUB bootloader:

boot.kernelParams = [ "console=ttyS0,115200n8" ];
boot.loader.grub.extraConfig = "
  serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1
  terminal_input serial
  terminal_output serial
";

Unprivileged access to serial device

Serial devices under NixOS are created with the group dialout by default.

All users that are part of the group dialout can access serial devices.

Add a user to group dialout:

users.users.<name>.extraGroups = [ "dialout" ];

Tips

Serial console wrapping

The remote serial console has no knowledge of your local console. This means that it will wrap with safe defaults.

You can configure the columns/rows of your serial console using stty.

In a console sized like yours, e.g. a new tab or tmux window:

$ echo "stty rows $(tput lines) cols $(tput cols)"

This will give you the exact invocation for your current terminal size.

In case tmux is used an alternative is to add the following snippet to the tmux.conf:

≡︎ tmux.conf
bind R run "echo \"stty columns $(tmux display -p \#{pane_width}); stty rows $(tmux display -p \#{pane_height})\" | tmux load-buffer - ; tmux paste-buffer"

In this case fixing the terminal size can be achieved by pressing R.

Serial Console Login

There is a long thread here: https://github.com/NixOS/nixpkgs/issues/84105

This configuration seems to work

❄︎ serial-tty.nix
# Serial console configuration for /dev/ttyS0
# Enables login via serial interface

# https://github.com/NixOS/nixpkgs/blob/nixos-unstable/nixos/modules/services/ttys/getty.nix
# https://github.com/NixOS/nixpkgs/issues/84105

{ config, lib, pkgs, ... }:

{
  # Enable serial console on ttyS0
  boot.kernelParams = [
    "console=ttyS0,115200"
  ];

  # Disable the upstream getty module's automatic configuration for serial-getty@
  # This prevents conflicts with our custom configuration
  systemd.services."serial-getty@" = {
    enable = false;
  };

  # Configure our own serial-getty@ttyS0 service
  systemd.services."serial-getty@ttyS0" = {
    enable = true;
    wantedBy = [ "getty.target" ];
    after = [ "systemd-user-sessions.service" ];
    wants = [ "systemd-user-sessions.service" ];
    serviceConfig = {
      Type = "idle";
      Restart = "always";
      Environment = "TERM=vt220";
      ExecStart = "${pkgs.util-linux}/bin/agetty --login-program ${pkgs.shadow}/bin/login --noclear --keep-baud ttyS0 115200,57600,38400,9600 vt220";
      UtmpIdentifier = "ttyS0";
      StandardInput = "tty";
      StandardOutput = "tty";
      TTYPath = "/dev/ttyS0";
      TTYReset = "yes";
      TTYVHangup = "yes";
      IgnoreSIGPIPE = "no";
      SendSIGHUP = "yes";
    };
  };

  # Enable early console output during boot
  #boot.consoleLogLevel = 7;  # Show all kernel messages
  boot.initrd.verbose = true;  # Show initrd messages
}