How To Check and Use Serial Ports Under Linux

Posted on in Categories , , , , , , , last updated January 28, 2010

How do I check and configure serial ports under Linux for various purposes such as modem, connecting null modems or connect a dumb terminal?

Linux offers various tools. Linux uses ttySx for a serial port device name. For example, COM1 (DOS/Windows name) is ttyS0, COM2 is ttyS1 and so on.

Task: Display Detected System’s Serial Support

Simple run dmesg command
$ dmesg | grep tty
Output:

[   37.531286] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[   37.531841] 00:0b: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[   37.532138] 0000:04:00.3: ttyS1 at I/O 0x1020 (irq = 18) is a 16550A

setserial command

setserial is a program designed to set and/or report the configuration information associated with a serial port. This information includes what I/O port and IRQ a particular serial port is using, and whether or not the break key should be interpreted as the Secure Attention Key, and so on. Just type the following command:
$ setserial -g /dev/ttyS[0123]
Output:

/dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
/dev/ttyS1, UART: 16550A, Port: 0x1020, IRQ: 18
/dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
/dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3

setserial with -g option help to find out what physical serial ports your Linux box has.

Linux serial console programs

Once serial ports identified you can configure Linux box using various utilities:

  1. minicom– The best friendly serial communication program for controlling modems and connecting to dump devices
  2. wvidial or other GUI dial up networking program – a PPP dialer with built-in intelligence.
  3. getty / agetty – agetty opens a tty port, prompts for a login name and invokes the /bin/login command.
  4. grub / lilo configuration – To configure serial port as the system console

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

26 comment

  1. I don’t know if this is the place, but I’m looking for information that is similar. I have 2 machines setup through a null modem. I want to read through the com port from one machine to the other. The sending machine is running Windows sending through COM1: and the receiving computer is Linux Red Hat. Any help on this would be appreciated.

    1. You can use pyserial ,its a python package,can be used to read and write in serial ports

  2. I want to advise you that probably there is an error in the last sentence “seserial with -g option help to find out what physical serial ports your Linux box has.”; isn’t it setserial?

    Bye

  3. I have 4 standard serial ports defined, /dev/ttyS0-4, but I added a PCI-E 16 port serial adapter, and I have not found a way to connect anything to the upper 16 ports.
    How do you create the virtual links? what options do you give setserial to define the new ports?

  4. Daemon is a program called getty used by UNIX/Linux systems. There are many different versions of getty. The first modem detected is /dev/ttys0 or /dev/ttys1. To create a link to the devices file so that you can view modem information. Type ln -s /dev/ttys1 /dev/modem.
    It is essential to set the modem to AUTO ANSWER using getty.

    download minicom from alioth.debian.org/projects/minicom.

    create a modem config file by entering minicom -s from the root command line
    select the options you want to configure using AT commands
    connect to the modem by typing in minicom

  5. Hi,everybody
    I have a serial PCI card with two ports but neither of them is working, on Linux fedora 16. I have tried using “gtkterm” and “minicom” but unfortunately I can’t make a serial connection to my Cisco lab. Here is my output when I issue the command: “setserial -g /dev/ttyS[0123]”.

    /dev/ttyS0, UART: 16550A, Port: 0xec00, IRQ: 16
    /dev/ttyS1, UART: 16550A, Port: 0xe880, IRQ: 16
    /dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
    /dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3

    also I want to mention that when I try to connet throught the terminal it generates a message like so: ” Cannot open /dev/ttyS0: Permission denied”.
    any help please.
    abdel

    1. Check the permissions on /dev/ttyS0… Likely problem is it’s owned by root, and is part of the uucp group. Add yourself to the uucp group, and all should be better. I’m curious to know if this is enough for you as I am having problems communicating with a serial port on Linux as well. I got past the permissions stuff, but it is still not working.

      1. Thanks Tom,
        I actually I tried but it’s does not work, still says “permission denied”. I would appreciate if you explain to me step by step the procedure how to do it, I’m knew to Linux.
        Best Regards
        Abdel

        1. 1) ls -la /dev/ttyS0 — This will report the permissions on the port. Typically, this will say crw-rw—- 1 root uucp. The “c” in the string indicates that it’s a “character device” the first “rw” indicates that the owner has read/write permission. The second “rw” means that the group has read/write permissions. The “—” indicates that the rest of the world has no permissions. After the “—-“, “root” indicates that the owner of the device is root, and that the group id is “uucp”. So, you either need to be root, or a member of the uucp group to have read/write access to the port.

          2) To add yourself to the uucp group (to get read/write access), you need to edit the file /etc/group, and add your user name to the line that begins with “uucp:” User names are separated by commas, so add a comma, then your user name to the end of the line. You will need root permissions to edit this file.

          3) log out, and log back in because group associations are made at login time, and are not queried on each request.

          1. Thanks Tom,
            here is my output from the group file I have added my username to it but does not work.

            abdelinux:x:1000:uucp,adm,abdelinux,

            I will just give up.Thanks a lot for your help I relay appreciate it.

            1. Your entry in /etc/group is incorrect. If you read my previous post, I said to add your user id to the line that _begins_ with uucp. You added uucp to the group of users that can access your account. You want an entry in the /etc/group file that looks something like:

              uucp:x:14:uucp,abdelinux

              The critical element is that it’s the line that begins with uucp

              1. Hi Tom,

                It works. I just add my username to ” dialout ” and it worked fine. Iwould like to thank you for you time and support.

                Best Regards
                Abdel

  6. hi,
    I have connected the SIM300(gsm model) to my pc running ubuntu. I need a code to send a AT command to the GSM module and it should read and display the reply.
    Please let me know steps.
    I tried so many methods and codes, but no use…

    Thank you

  7. all my ttyS* shows unknown, earlier it was working fine, now it doesnt recognise

    setserial -g /dev/ttyS*
    /dev/ttyS0, UART: unknown, Port: 0x03f8, IRQ: 4
    /dev/ttyS1, UART: unknown, Port: 0x02f8, IRQ: 3
    /dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
    /dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3

    #cat /etc/redhat-release
    CentOS Linux release 6.0 (Final)

    #dmesg | grep tty
    console [tty0] enabled

    can u pls help me

  8. tried spending time in google two days, still no success, tried connecting a brand new device too, still no success.

  9. Sorry, but that second sentence is not necessarily true. I have a PC with two dual com port adapters. Windows sees them as COM1 -> COM4 (there is no built in COM port). Linux sees them as /dev/ttyS4->/dev/ttyS7.

    /dev/ttyS0-/devttyS3 are apparently reserved, exist whether or not a physical port exists, and /dev/ttyS0 for certain is “special” in that it is used by the default terminal window:

        dmesg | grep tty
        [    0.000000] console [tty0] enabled
        [    1.009457] 0000:02:00.0: ttyS4 at I/O 0xd030 (irq = 16) is a ST16650V2
        [    1.077312] 0000:02:00.1: ttyS5 at I/O 0xd020 (irq = 17) is a ST16650V2
        [    1.141309] 0000:02:00.2: ttyS6 at I/O 0xd010 (irq = 18) is a ST16650V2
        [    1.205307] 0000:02:00.3: ttyS7 at I/O 0xd000 (irq = 19) is a ST16650V2
        
        setserial -g /dev/ttyS[012345657]
        /dev/ttyS0, UART: unknown, Port: 0x03f8, IRQ: 4
        /dev/ttyS1, UART: unknown, Port: 0x02f8, IRQ: 3
        /dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
        /dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3
        /dev/ttyS4, UART: 16650V2, Port: 0xd030, IRQ: 16
        /dev/ttyS5, UART: 16650V2, Port: 0xd020, IRQ: 17
        /dev/ttyS6, UART: 16650V2, Port: 0xd010, IRQ: 18
        /dev/ttyS7, UART: 16650V2, Port: 0xd000, IRQ: 19
    
  10. Hi All tried all of the above and still get permission denied ???? HELP
    Kevin

    [email protected]:~/bpq$ groups ${USER}
    kevin : kevin adm dialout cdrom sudo dip plugdev lpadmin sambashare
    [email protected]:~/bpq$ setserial -g /dev/ttyS[012345657
    /dev/ttyS[012345657: No such file or directory
    [email protected]:~/bpq$ setserial -g /dev/ttyS[012345657]
    /dev/ttyS0: Permission denied
    /dev/ttyS1: Permission denied
    /dev/ttyS2: Permission denied
    /dev/ttyS3: Permission denied
    /dev/ttyS4: Permission denied
    /dev/ttyS5: Permission denied
    /dev/ttyS6: Permission denied
    /dev/ttyS7: Permission denied
    [email protected]:~/bpq$

  11. Hiiiiiiiii
    Please help me
    i am install minicom and use serial modem sim300 v7.03 but there is problem to type AT cmd on minicom console…….

  12. Edgeports are very easy to use the firmware is on Ubuntu they are from 2 ports to 16+ I bought my last two of them on ebay,,,
    Jim

  13. Hi all..
    I got problem to connect with my USB serial port,
    i already create
    ln -s /dev/ttyUS0 ~/.wine/dosdevices/com1

    and check dmesg | grep tty
    [ 0.000000] console [tty0] enabled
    [ 7.722004] systemd[1]: Created slice system-getty.slice.
    [ 3659.524452] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUSB0
    [ 4850.183339] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0
    [ 4857.907567] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUSB0
    when running SDNC program and select com1 will shown “port is already in use”

Comments are closed.