|
| 1 | ++++ |
| 2 | +title = "Configuring Linux Serial Access" |
| 3 | +date = "2024-08-26" |
| 4 | +description = "" |
| 5 | + |
| 6 | +#[taxonomies] |
| 7 | +#tags = ["linux"] |
| 8 | ++++ |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +For systems with no graphics support, serial access is a requirement. Useful for systems that support graphics too, serial access provides a robust method of administering a system in times of trouble. |
| 13 | + |
| 14 | +At its simplest, serial access provides a simple login shell for Linux systems. It's a bit crude generally and can be a bit frustrating to use at times. However, when a system begins to misbehave, the right serial configuration can make things much easier. |
| 15 | + |
| 16 | +This guide demonstrates how to configure serial for a Linux system, including dumping kernel logs (`dmesg`) to serial. This guide also aims to enable simultaneous terminal access through both serial and display, should the target system support both. |
| 17 | + |
| 18 | +## Instructions |
| 19 | + |
| 20 | +**NOTE:** This guide assumes assumes you have root access to the system and that the target system is x86-64 based (ARM systems boot differently). |
| 21 | + |
| 22 | +### 1\. Identify Serial Port |
| 23 | + |
| 24 | +To configure serial, you must first ensure your system supports it. |
| 25 | + |
| 26 | +If not immediately visible, first reference the system or motherboard datasheet. On systems more purposed for so-called 'edge' or firewall use, you will often find a built-in serial port alongside other IO ports. It is usually labeled 'Console' or 'COM'. These systems largely lack graphics support, so serial access is essential. |
| 27 | + |
| 28 | +Alternatively, consumer and server motherboards may also provide a serial port, but this is not guaranteed. Serial may be either built-in alongside other IO ports (unlikely for consumer motherboards) or exposed pins on the motherboard. Labels are similar to firewall-type systems, but you may also see the label 'UART'. |
| 29 | + |
| 30 | +If the system exposes multiple serial ports, pick one and keep track of it. We'll need this information later. |
| 31 | + |
| 32 | +### 2\. Ensure Serial Port is Enabled |
| 33 | + |
| 34 | +Boot your system into its BIOS or access its IPMI management console and ensure the serial port is enabled and configured to a reasonable baud rate (115200 is standard). This step varies significantly system to system. **When in doubt, leave other settings at default.** |
| 35 | + |
| 36 | +On systems with an AMI BIOS, you'll often find this option under the 'Advanced' tab in 'Serial Port Console Redirection'. |
| 37 | + |
| 38 | +### 3\. Identify Serial Port Connector Type, Procure Cable |
| 39 | + |
| 40 | +Once you've verified the system provides a serial port and it is configured in the BIOS/IPMI interface, next identify _what type_ it is. |
| 41 | + |
| 42 | +There are several different serial port types, but you will most often find one of the following. This list is not meant to be exhaustive: |
| 43 | + |
| 44 | +- **RJ45:** |
| 45 | + |
| 46 | + - Easy to confuse for an Ethernet port, this type uses the same RJ45 connector as most Ethernet cables/NICs |
| 47 | + - Often found on more firewall-type systems |
| 48 | + - Requires cable with converter (e.g. RJ45 to USB) |
| 49 | + - **Connecting an Ethernet cable between systems will not work** |
| 50 | + |
| 51 | +- **RS232:** |
| 52 | + |
| 53 | + - Looks like VGA but with two rows of pins instead of three |
| 54 | + - Most often found in server motherboards or industrial-grade systems |
| 55 | + - Requires cable with converter (e.g. RS232 to USB) |
| 56 | + - Ensure cable is right gender (i.e. pinned for pinhole port) |
| 57 | + |
| 58 | +- **UART Pins:** |
| 59 | + |
| 60 | + - Uses pins or pinholes and requires jumper wires |
| 61 | + - Pins/pinholes most often found directly on the Motherboard or system PCB |
| 62 | + - May have to solder on pins or even determine which pins are which. [This video](https://www.youtube.com/watch?v=ZmZuKA-Rst0) can help with identification |
| 63 | + - Requires USB to serial converter and jumper wires, which often come separate |
| 64 | + - Ensure jumper wires are right gender on both ends |
| 65 | + |
| 66 | +- **USB:** |
| 67 | + - Plain old USB |
| 68 | + - Not as frequent as other types, this type is often found in some commercial APs and more frequently in development boards (e.g. Arduino boards) |
| 69 | + - Unlike other types, this type will sit in front of a chip on the PCB that converts from serial to USB. No special cable required |
| 70 | + - **Make sure your cable has data wires and not just power.** Not all USB cables are built the same |
| 71 | + |
| 72 | +### 4\. Configure Linux/GRUB Serial Settings |
| 73 | + |
| 74 | +This step configures both Linux and GRUB to use serial output in addition to the system console. For context, GRUB is a bootloader used to select boot configurations including Linux. Documentation for these options is available [here](https://www.gnu.org/software/grub/manual/grub/html_node/Simple-configuration.html). |
| 75 | + |
| 76 | +**These instructions assume you are using GRUB2**, which is basically every major Linux distribution at this point (including Ubuntu, Fedora, ArchLinux). |
| 77 | + |
| 78 | +1. **Boot the system to the desired operating system** |
| 79 | +2. **Back up the `/etc/default/grub` file in a safe place** |
| 80 | +3. **As root, open the `/etc/default/grub` file in your desired text editor** |
| 81 | + |
| 82 | + This file contains configuration to boot the current operating system you're using, but other GRUB entries may be present in the final GRUB configuration file. |
| 83 | + |
| 84 | +4. **Add or adjust the file to match the following in the configuration file** |
| 85 | + |
| 86 | + _Leave other configuration untouched unless you know what you're doing._ |
| 87 | + |
| 88 | + ``` |
| 89 | + GRUB_TERMINAL="serial console" |
| 90 | + GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word-8 --parity=no --stop=1" |
| 91 | + ``` |
| 92 | + |
| 93 | +5. **Carefully add the following to the `GRUB_CMDLINE_LINUX` option** |
| 94 | + |
| 95 | + **Be extra cautious to only _add_** to the `GRUB_CMDLINE_LINUX` option, as these are options passed directly to the kernel. Deleting the wrong thing could cause your system to not boot (e.g. if you remove a `resume=...` option). See [here](https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html) for parameter documentation. |
| 96 | + |
| 97 | + Add exactly the following, separating from the previous option by a space character. Adjusting the baud and target serial device based on your system hardware: |
| 98 | + |
| 99 | + ``` |
| 100 | + console=tty0 console=ttyS0,115200n8 |
| 101 | + ``` |
| 102 | + |
| 103 | + For example, the following is from a system I've configured (**do not copy verbatim, you will almost certainly misconfigure your system**): |
| 104 | + |
| 105 | + ``` |
| 106 | + GRUB_CMDLINE_LINUX="resume=UUID=... rhgb console=tty0 console=ttyS0,115200n8" |
| 107 | + ``` |
| 108 | + |
| 109 | +6. **Update the GRUB configuration** |
| 110 | + |
| 111 | + Basically every distribution will use the `grub2-mkconfig` tool to generate a boot file, with specific invocation depending on the system. |
| 112 | + |
| 113 | + - Ubuntu: |
| 114 | + |
| 115 | + ```Bash |
| 116 | + # Can also do this by using the 'update-grub' and 'update-grub2' utilities |
| 117 | + grub2-mkconfig -o /boot/grub/grub.cfg |
| 118 | + ``` |
| 119 | + |
| 120 | + - Fedora: |
| 121 | + |
| 122 | + ```Bash |
| 123 | + grub2-mkconfig -o /boot/grub2/grub.cfg |
| 124 | + ``` |
| 125 | + |
| 126 | +7. **Power off the system** |
| 127 | + |
| 128 | +### 5\. Install Desired Serial Access Program |
| 129 | + |
| 130 | +To access serial on a target system, you'll need to install a serial access program on the device you'll connect the _other end_ of the serial cable to. I'll refer to this as the _host system_. |
| 131 | +
|
| 132 | +Which program you choose depends on personal preference. I use `minicom` most often, but PuTTy and `screen` are other well-utilized options. For newer users, I recommend PuTTy. |
| 133 | +
|
| 134 | +### 6\. Connect to Serial Port |
| 135 | +
|
| 136 | +1. **Connect serial cable** |
| 137 | +
|
| 138 | + With your serial cable, attach the USB end of the cable to the _host system_ then attach the other end to the desired serial port on the _target system_. |
| 139 | +
|
| 140 | +2. **Verify that the _host system_ detects the serial cable** |
| 141 | +
|
| 142 | + How you do this will depend on the operating system. |
| 143 | +
|
| 144 | + On Linux, check the output of `sudo dmesg | grep tty`. Save the TTY name for later (e.g. `ttyUSB0` or `ttyACM0`). Windows will show the device as a COM port in the 'Device Manager' GUI. |
| 145 | +
|
| 146 | +3. **Open your installed serial access program** |
| 147 | +
|
| 148 | + **NOTE:** You will need to adjust the baud rate and hardware flow control here. `minicom` does this in a sub-menu that you can open using the 'CTRL-A' keystroke. |
| 149 | +
|
| 150 | + The specifics here will depend on the program you've chosen. If you're using `minicom` or `screen`, you must specify the TTY as an argument. For example, `minicom -D /dev/ttyUSB0`. |
| 151 | +
|
| 152 | + Most Linux systems will only permit access to USB serial devices by default with root permissions. It is possible to permit this as non-root user configuring udev rules or adding the user to a specific group, but this depends on the distribution. See [this post](@/blog/usb-serial.md) I wrote to configure this using udev rules. |
| 153 | +
|
| 154 | +4. **Boot target system** |
| 155 | +
|
| 156 | + Assuming everything is correct, you should have serial access to your system after this point! |
| 157 | +
|
| 158 | + If you see garbled or no output, you may need to adjust the baud rate or disable flow control. |
0 commit comments