(M)  s i s t e m a   o p e r a c i o n a l   m a g n u x   l i n u x ~/ · documentação · suporte · sobre

 

3. Configuring Linux for Serial Consoles

There is two parts to getting a serial console set up under Linux. First, you must tell Linux to redirect its console output to the serial port. Second, you must set up mgetty to start a login process on the serial port once the kernel has completed booting. Some distributions use mingetty for the video console, but mingetty has no serial port support. You will instead want to use mgetty. A third (optional) configuration is to set the hardware BIOS to redirect its POST and BIOS information to the serial port. Check your motherboard documentation for more information about this.

3.1. Configuring LILO and the Linux Kernel

If you're using LILO as your bootloader, you can quickly test using serial console from Linux by entering:

LILO: Linux console=ttyS0,9600n8

Assuming the LILO tag for your Linux kernel is called "Linux". Change this for the name of your kernel. The generic format for the console option is console=device,options. You can give multiple console statements, and kernel messages will go to all listed devices, but the last one listed will be used as /dev/console.

deviceThe device entry to use as the console without /dev/. You can use tty0 to get normal behavior, ttyx to put the console on another virtual console, or ttySx to put the console on a serial port.
optionsThis is mostly used for passing options to the serial port. The format for this is BPN, where B is speed in bps (so use 9600, 19200, 38400, etc.). The P is parity and is one of three letters: n for no parity, e for even parity, and o for odd parity. The N is the number of data bits, and is usually either 7 or 8. The default is 9600n8. Most users will want to use the default, or increase the speed to 19200 bps.

You should see the Linux kernel start through the decompression process then you will see no more on-screen information until the kernel has completed and mgetty starts up a login prompt on the screen. If you are monitoring the serial port, you'll see the Linux bootup information coming over the serial port. However, you probably will not see a login prompt on the serial port (yet). We'll cover that in Section 3.2.

Once you are sure this is working, you can now edit LILO to pass this information to the kernel each time it boots. You can also configure LILO to send its prompt to the serial port. Fire up your favorite editor of choice and load up the /etc/lilo.conf file. You will want to add two lines, one to the general configuration and one to the specific kernels you want to use.

serial=0,9600n8
append="console=tty0 console=ttyS0,9600n8"

The append statement contains the statement we listed above, and tells Linux to send its output to the serial port. The serial command goes to LILO, and tells it to open port 0 (ttyS0, or COM1). The options for serial are the same as to the console statement.

Note: Make sure the serial port settings for serial and console are the same. If they are different, you will need to change your serial port application between LILO and the kernel, which becomes very inconvenient.

Re-run /sbin/lilo and reboot. You should now see everything except the login prompt on the serial port. Information should still be going to the monitor, just in case you have problems with the serial port.

3.2. Configuring getty for use with serial ports

Some distributions may ship with mingetty that does not support serial ports. The first thing you have to do is make sure the version of getty you are using supports serial ports. Both agetty and mgetty do this. So run off now using your favorite packaging system to make sure this is the case. Don't worry, this document will still be here when you get back.

Back so soon? Great! Let's get that serial port configured.

You will want to make sure that all your serial port settings are consistent. No sense in making getty run at 9600bps, while LILO and the kernel are talking 19200.

To get login prompts to appear on the serial port, edit the /etc/inittab file and add a line similar to the following:

s0:2345:respawn:/sbin/getty ttyS0 DT9600

The format for entries in inittab are covered in most basic Linux and UNIX books, but to repeat, each field is separated by a colon (:) and represent:

  • s0 - Arbitrary entry for inittab. As long as this entry doesn't appear anywhere else in inittab, you're okay. We named this entry s0 because it's for ttyS0.

  • 2345 - run levels where this entry gets called. If we switch to runlevel 1, this getty process will be shut down.

  • respawn - re-run the program if it dies. We want this to happen so that a new login prompt will appear when you log out of the console.

  • /sbin/getty ttyS0 DT9600 - the command to run. In this case, we're telling getty to connect to /dev/ttyS0 using the settings for DT9600 which exist in /etc/gettydefs. This entry represents a dumb terminal running at 9600bps. There are other entries that run at different speeds.

The entries in /etc/inittab will be loaded into init when root sends a HUP signal.

# kill -HUP 1

Note: Remember that init always has a PID of 1.

Now that getty is set up, you will be able to go from powerup to login prompt all over the serial port