Chapter
18
Terminals, modems and serial lines
This chapter is an unmodified
version of a chapter first produced in 1997.
Some or even all of the content may be out of date due to changes in
Linux.
It's usual for a UNIX computer to have a number of peripherals including modems, dumb terminals and printers connected to it. A major method by which these peripherals are connected to a UNIX computer is via serial ports. This chapter will show you how to connect devices to your UNIX computer's serial ports. It will also show you how to connect dumb terminals and modems to a UNIX machine.
A good source of information for connecting devices to the serial port of a Linux box is the Serial-HOWTO. Some of the material in this chapter has been adapted or taken directly from the .
§
Covers the RS-232 standard, serial cables, connectors, DTE and DCE.
§
Discusses the hardware and software side of connecting a terminal.
§
Looks at how to connect and configure a modem for dialing in or out.
The hardware part of connecting a serial device deals with
§ obtaining the correct serial cable and connectors
§ choosing the port which to connect the device to your computer
There are two steps to choosing a port to which to connect a device
§ choosing the physical port, and
§ find the right device file.
A typical UNIX computer is likely to have many different serial ports. A PC is liable to have 2, 3 or 4 serial ports. It is possible to purchase multi-port serials cards that supply multiple (4, 20 and more) ports, see Figure 18.1. These are used by installations that want to have large numbers of modems, terminals or other serial devices connected to the computer.
Each physical port on a UNIX machine has a corresponding device file through which the operating system passes information to the device.
Table 18.1 summarises the more common device files for serial ports on a Linux box. Most distributions of Linux will also create /dev/modem and /dev/mouse as symbolic links to the appropriate device file listed in Table 18.1. Some people disagree with this practice and it may cause problems if you are allowing people to dial into your machine using a modem.
|
Device
File |
MS-DOS
Equivalent |
Purpose |
|
/dev/cua0 |
com1 |
Used for out-going
connections, |
|
/dev/ttyS0 |
com1 |
Used for in-coming
connections, |
Table 18.1
Linux device files for serial ports
RS-232 is the standard that most serial ports follow. A full blown discussion on the RS-232 standard is beyond the scope of this text. The following reading can supply more information on RS-232.
Reading 18.1
This is an optional reading. This material will not be examined and is only
included for your interest.
Even though serial cables are meant to follow the RS-232 standard there are a number of differences including
§
sex,
Plugs are either female (small holes) or male (small prongs).
§
size, and
Serial plugs for example can be either 9 pin, 25 pin or a couple of other
configurations.
§
the wires that are connected.
A serial cable can have up to 25 wires connecting the pins at one end of the
cable to the pins at the other end. There are a number of different methods to
connect these pins depending on the type of devices being connected.

Plugs are either female, small holes, or male, small pins stick out, in sex.
Figure 18.2
Male and Female connectors

![]()

Serial connectors come in a number of different formats including DB-25, DB-9,
DIN-8, and RJ-45.
Figure 18.3
DB-25, DB-9 and RJ-45 connectors
How a serial cable is wired is controlled to a certain extent by the type of devices you are connecting. Most devices are placed into one of two categories
§
DTE, data terminal equipment
Most terminals, computers and printers fall into this category.
§
DCE, data communications equipment
Modems are generally DCE.
The division between DTE and DCE is done on the basis of which signals a device will expect on particular pins. This means that cables to connect two DTE devices will be different from a cable used to connect a DTE and a DCE device. Table 18.2 defines the types of cable to use.
|
Connection |
Cable type |
|
DTE to DCE |
Straight modem cable |
|
DTE to DTE |
Null modem cable |
Table 18.2
For the purposes of this subject you do not need to know how to actual wire null and straight modem cables. Any good data communications book will explain how and most electrical stores stock these cables.
Given the differences in connectors and cables connecting serial devices can quickly become a complex business. One method for reducing this complexity is the Yost standard. If you are interested a description of the standard is available on the .
UNIX is a multi-user operating system. To make use of this attribute multiple users must be able to connect to the system at the same time. This implies that there must be multiple access points. Dumb terminals are one of the cheapest methods for providing multiple access points to a UNIX machine.
In most cases a dumb terminal is connected to a UNIX machine using a serial line. A dumb terminal does little more than present text to the user and transfer keystrokes from the terminal back to the central computer. It is dumb because the terminal does no processing of the data.
Even though the interface on such beasts is primitive they are still one of the most used methods for adding extra access points to a UNIX computer.
Businesses wanting to use dumb terminals have two options do not have to purchase purpose built dumb terminals. A personal computer can act as a dumb terminal by
§ connecting the PCs serial port to the UNIX machines serial port, and
§ using a communications programs (like Procomm or Terminal) to communicate with the UNIX machine over the serial line.

Figure 18.??
Televideo Dumb Terminal
The steps involved in connecting a dumb terminal to a UNIX box include
§ configuring the terminal,
§ connecting the terminal,
§ starting a getty process, and
§ configuring the UNIX terminal software.
For a dumb terminal to work correctly it must be configured properly. In the case of purpose built dumb terminals, configuration will generally be performed by setting dip switches on the terminal.
In the case of a personal computer and a communications package these settings are set using the options within the communications program.
Characteristics of a dumb terminal that need to be configured include
§
bits per second,
The speed at which information can be transferred. Typical values (for today)
range from 9600 bps up to 38,400 bps.
§
parity,
Typically be set off.
§
duplex,
This should be set to full and signifies that data can be transferred in both
directions simultaneously.
§
auto linefeed,
Should be turned off. The end of a line under UNIX is signified by a newline
character. MS-DOS and other systems use a combination of a newline and a
carriage return character.
§
data bits, and
Suggested values are either 7 or 8 with 7 being the preference.
§
stop bits.
With 7 data bits use 2 stop bits. With 8 data bits use 1 stop bit.
If anyone of these settings are set incorrectly the output to the terminal or input from the terminal will be corrupted.
Once the terminal is configured you now need to connect the terminal to the computer. The steps to do this include
§ identifying a free serial port on the computer,
§ identifying the device file the corresponds to that port,
§ obtaining the correct cable to make the connection, and
§ finally making the connection.
Once terminal is configured, connected and turned on, the next step is to test whether or not you can actually transmit data through the connection. The simplest method to do this is to send some information directly to the device file associated with the terminal.
For example:
ls -l > /dev/tty1
If the connection is correct and working you should see the output appear on the device.
Be careful when you are choosing device files to send output to. Sending output to the wrong device file can be disastrous.
There are a number or reasons why a connection may not work including
§
incorrect permissions,
For the test to work you must have write permission on the device file. Check
the permissions. Typically you have to be the root user to perform the test.
Don't change the permissions on the device file to world write. This can be a
security hole.
§
the wrong device file,
You've picked the wrong device file and the information isn't being sent to the
new device. Perhaps the device file for the port you want to use hasn't been
created yet.
§
incorrect configuration, and
The hardware configuration on the device is not correct. Don't expect the output
you send to the device to appear picture perfect. Since you are bypassing the
normal mechanism for using the device it may not work 100%.
§
incorrect cabling.
Is the device turned on? Are you sure you have the correct type of cable.
Beg, borrow or steal a dumb terminal (another PC with a communications program will suffice). Perform all the steps listed above for connecting the terminal to your UNIX machine. Test it, see if you can get output appearing on the screen of the dumb terminal.
Terminal configuration files is one area in which the diversity of UNIX platforms rears its ugly head. System V based machines will use different configuration files than BSD based systems. Early BSD systems use different configuration files again. For the purposes of this subject we will concentrate on the Linux software.
Terminal configuration files can be divided along the lines of their purpose
§ enabling the login process,
§ setting line configuration, and
§ terminal characteristics.
For a terminal to work users must be able to login. For users to login particular processes have to be executed and be listening on each terminal connection. There are configuration files that control which device files have the login process enabled.
The operating system has to know about and set the characteristics of the serial line, such as speed, data bits, parity etc, that the terminal is connected to.
Different terminals have different keyboard layouts, different capabilities (colour etc) and different special character codes to do things like clear the screen. In order to use the full capabilities of a particular type of terminal UNIX must know about the terminal's characteristics. To do this the terminal must have an entry in the database of terminal characteristics that UNIX maintains.
In order for someone to login using a dumb terminal the following steps must happen
§ init must start a getty process for the terminal,
§ the getty process displays the login prompt, waits for the user to enter a username and then starts a login process,
§ the login process gets a passwords, checks the validity of that password and then runs the user's login shell if the password is valid,
§ once the user is finished the login shell will finish, causing init to restart a getty process
So in order for the whole process to start init must be configured to start a getty process.
/etc/issue and /etc/motd are text files that contain text messages that are displayed during the login process. /etc/issue is displayed before the login: prompt by the getty process. /etc/motd is displayed by the login process just before it runs the user's login shell.
It is common to use these files to disseminate system information such as when the next time the machine will be down.
Modify the /etc/issue and /etc/motd files of your system.
You should be aware of the difference between logging in over a dumb terminal and logging in over a network. A dumb terminal is a special piece of hardware connected directly into the serial port of a UNIX computer. When you login in over a network, usually using telnet, you are connecting via that computers network connection.
However this doesn't change the requirement that there must be a getty process running in order for you to login. The difference between a dumb terminal connection and a network connection is the daemon that starts the getty process. For a dumb terminal it is init. For a network connection it might be telnetd or maybe inetd.
Under Linux the init process is controlled by the /etc/inittab configuration file (the format of /etc/inittab is discussed in a earlier chapter). The inittab file must have an entry for each terminal that requires a getty process. Typical entries look like
c6:23:respawn:/etc/getty 38400 tty6
c7:23:respawn:/etc/getty 38400 ttys1
If you are unsure about the format of inittab entries you should take another look at Chapter 11.
Linux can come with up to three different getty programs, agetty, getty_ps and mgetty. By default my system only has agetty so that is the one I'll concentrate on in this chapter. The other versions can be obtained from the standard Linux ftp sites. All versions will use basically the same arguments but some may provide some additional features.
The manual page for agetty provides sufficient information to get it working.
Other Unices may use a more complex set of configuration files for the login procedure. The chapter 10 provides some additional information on these files. If this doesn't help you should refer to your system's manual pages.
Examine the /etc/inittab file for your system. Are there any entries that start getty processes? For which terminals are they?
Both getty
and login are
executable programs. In which directory are they? What would happen if these
files were deleted? What would happen if the execute permission on these files
was removed?
Try it and find out. Change the permissions on either getty
or login, see what
happens. Log in and then log out, now what happens?
Notice that in the initab file the getty entry has the action respawn. What would happen if the action was changed to once.
Every terminal connected to a UNIX machine has an associated terminal driver process. This process maintains
§ a list of characteristics about the current terminal, and
§ a list of special characters and how they should be handled.
A common complaint from users is that when they hit particular keys the terminal doesn't do what is expected. Hitting the backspace key might produce a weird character or the cursor keys might not work under vi. These problems maybe caused by the terminal driver not being configured properly.
Initially these settings are set up by the system from the entries in the system's terminal configuration database. The stty command can be used to view and modify these settings.
Table 18.3 lists some of the terminal characteristics and Table 18.4 lists some of the special characters. To view the current settings try stty -a (the command might be stty all or stty everything depending on your system).
The following is the output of the stty command on my Linux box
beldin:~$ stty -a
speed 9600 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc -ixany imaxbel
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
|
Option |
Meaning |
|
n |
bits per second |
|
rows n |
lines to the screen |
|
columns n |
columns on the screen |
|
oddp |
odd parity |
|
evenp |
even parity |
|
-parity |
no parity |
Table 18.3
Characteristics affected by stty
stty options such as evenp or parity are either turned on or off. If evenp is used even parity is turned on if -evenp is used then even parity is turned off.
One option of the stty
command not shown in Table 18.3 is echo
Refer to stty's
manual page to find out what it is used for.
Use the stty
command to turn echo
off, what happens?
Use stty to turn
it back on.
Write a shell function get_password
that gets the user to enter a password but doesn't display the password while
the user is typing it in
In these tables you will see character combinations like ^H and ^?. The ^ symbol is used in this case to signify the control key. So ^H could be rewritten CTRL-H.
A useful option of the stty command is sane. Entering stty sane when the terminal is behaving strangely will solve many problems.
It is possible to use I/O redirection to affect the settings of terminals other than the one you are currently using. Which form of I/O redirection (input or output) you use depends on your system. For BSD redirect the output of stty. For SysV redirect the input. (This will only work if you have the correct permissions on the device file associated with the terminal.
|
Symbolic
name |
SysV
default |
BSD/Linux
default |
Meaning |
|
ERASE |
# |
^H |
erase one character of input |
|
WERASE |
N/A |
^W |
erase one word of input |
|
KILL |
@ |
^U |
erase entire line |
|
EOF |
^D |
^D |
end of file |
|
INTR |
^? |
^C |
interrupt current process |
|
QUIT |
^\\ |
^\\ |
kill current process with core dump |
|
STOP |
^S |
^S |
stop output to the screen |
|
START |
^Q |
^Q |
restart output to screen |
|
SUSPEND |
N/A |
^Z |
Suspend current process |
Table 18.4
Special characters
By default the character CTRL-D
is used to indicate the end of a file under Linux. If you examine the output
of stty -a you
should see eof = ^D.
One way to create a file is to
beldin:~$ cat >
newfile
hello there
^D
Where you use CTRL-D
to finish.
Use the stty
command to change the end of file marker so that it is the letter Z.
Try to create a file called newfile
using the above method. What happens when you hit
CTRL-D
Z
Use stty to change the values for rows and columns to 10 and observe the difference. Try running the stty -a and vi commands.
Different terminals have different keyboard layouts, escape codes and capabilities. For example one terminal will use one combination of characters to signify clearing the screen while another terminal will use another combination of characters.
If programs that wish to be able to clear the screen want to work on different terminals they must be able to find out how each terminal performs the operation. Under the UNIX operating system programs discover this information using
§ the TERM (term if you're using csh) environment variable that specifies the type of terminal, and
§ a system specific terminal database that holds information about a large number of different terminals.
The shell variable TERM is usually initialised when a user first logs in. It will hold a unique identifier that signifies the type of terminal being used. This identifier is used to access the information about the terminal from the system's terminal database.
If the TERM variable is set incorrectly or the terminal does not have an entry in the terminal database problems likely to occur include
§
keys not performing the expected task,
Hitting the backspace key doesn't do anything or displays a weird key
combination.
§
screen output not being written properly, or
The screen not scrolling properly, unexpected colours or characters are
appearing.
§ programs not working properly.
Full screen programs, vi for example, make use of special characteristics offered by most terminals. If the particular terminal you have doesn't have an entry in the terminal characteristics file it can't make use of these special characteristics.
It is the responsibility of various startup files (typically /etc/profile) to make sure that the TERM variable is initialised to the correct value.
The following is an example of how the TERM variable might be set.
if [ `tty` = /dev/tty1 ]
then
TERM=vt100
elsif [ `tty` = /dev/tty2 ]
then
TERM=tvi912b
else
TERM=console
fi
On this system the terminal connected to /dev/tty1 is a vt100 so that is the value TERM is set to. The terminal on /dev/tty2 is a tvi912b and it assumes that any other type of terminal is a console. The tty command used here returns the device file used by the current terminal.
Once the TERM variable is set its value is used to access information in the terminal database. SysV and BSD based systems use different terminal databases.
Before doing this exercise find out what the current value of the TERM variable is. Make up some name for a terminal, e.g. myterm. Set the TERM shell variable to this value. Attempt to use the vi editor. What happens? Where is the TERM shell variable set on your system.
There are two basic types of terminal database used by UNIX systems
§ termcap , which is from BSD UNIX, and
§ terminfo , which is from SysV UNIX.
Linux actually supports both. For this subject we will only examine the termcap terminal database. If you system uses terminfo (try man terminfo) you can refer to the old textbook's chapter 10 for some information on terminfo
/etc/termcap is a text based file used by BSD and Linux as the terminal database. It contains colon delimited entries for each type of terminal the system recognises. The following is an example termcap entry.
vt100|dec-vt100|vt100-am|vt100am|dec
vt100:\
:do=^J:co#80:li#24:cl=50\E[;H\E[2J:sf=2*\ED:\
:le=^H:bs:am:cm=5\E[%i%d;%dH:nd=2\E[C:up=2\E[A:\
:ce=3\E[K:cd=50\E[J:so=2\E[7m:se=2\E[m:us=2\E[4m:ue=2\E[m:\
:md=2\E[1m:mr=2\E[7m:mb=2\E[5m:me=2\E[m:is=\E[1;24r\E[24;1H:\
:if=/usr/share/tabset/vt100:\
:rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:ks=\E[?1h\E=:ke=\E[?1l\E>:\
:ku=\EOA:kd=\EOB:kr=\EOC:kl=\EOD:kb=^H:\
:ho=\E[H:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:pt:sr=2*\EM:vt#3:xn:\
:sc=\E7:rc=\E8:cs=\E[%i%d;%dr:
The first field of every entry is a list of terminal names (separated by |). These names are used by the software to recognises a particular terminal. These names are what appears as the value for the TERM variable and is used by the system to look up an entry.
The rest of the entry for a terminal consists of various options that describe the way in which the terminal works. The various options will not be discussed here. They are described in the manual pages for the system if needed.
It is advisable to put the entries for the most used terminals on your site at the front of the termcap file to speed searching.
Determine the type of terminal you are using and examine the entry for your terminal that is stored in your system's terminal database files.
The steps involved in connecting a dumb terminal to a UNIX computer are
§ choose a port on the computer,
§ obtain the correct cable to connect the terminal to the port,
§ configure the terminal,
§ connect the terminal and test the connection,
§ configure the line settings,
§ ensure that the terminal's type is in the terminal database (either /etc/termcap or terminfo),
§ ensure that the TERM shell variable is set correctly,
§
start the login process for the port,
On a Linux box this is achieved by adding an entry to the /etc/inittab
file
A dumb terminal is simply a method for someone to connect to your machine, so communication is one way. With a modem you can either
§
dial out, or
Use your modem to connect from your machine to another machine (much like using
a communications program like Procomm on a PC).
§
dial in.
Allow somebody else to connect to your UNIX machine via a telephone line.
In a later chapter on networking you will be introduced to SLIP and PPP. These are protocols that allow you to use a modem and a phone line as a TCP/IP network connection.
Setting a modem up includes the following steps
§ connect the modem to the computer
§ test the connection
§ dial out only use a communications program to dial an outside number
§ configure the line,
§ dial in only initialise the modem
§ dial in only start the login process on the modems port
With a Linux machine you are likely to have either an external or an internal modem. With an external modem the procedure for connecting the modem is very similar to that with a dumb terminal
§ identifying a free serial port on the computer,
§ identifying the device file the corresponds to that port,
§ obtaining the correct cable to make the connection, and
§ finally making the connection.
With an internal modem the modem will have to be installed into an appropriate internal slot. You won't need to connect an internal modem to a serial port because internal modems have a serial port built-in.
This following section is taken verbatim from the Linux Serial-Howto
setserial is a program which allows you to look at and change various attributes of a serial device, including its port address, its interrupt, and other serial port options. It was initially written Rick Sladkey, and was heavily modified by Ted T'so tytso@mit.edu, who also maintains it. The newest version is 2.10, and can be found on the Linux FTP sites. You can find out what version you have by running setserial with no arguments.
When your Linux system boots, only ttyS{0-3} are configured, using the default IRQs of 4 and 3. So, if you have any other serial ports provided by other boards or if ttyS{0-3} have a non-standard IRQ, you must use this program in order to configure those serial ports. For the full listing of options, consult the man page.
Due to a bit of stupidity on IBM's part, you may encounter problems if you want your internal modem to be on ttyS3. If Linux does not detect your internal modem on ttyS3, you can use setserial and the modem will work fine. Internal modems on ttyS{0-2} should not have any problems being detected.
A simple method for testing the physical connection is to simply redirect some I/O to your modem's device file. If the connection has worked then the leds on your modem should flash indicate that information is reaching the modem.
A better method is to use one the available communication programs. The serial-howto uses kermit however this is not supplied on a standard Linux distribution. But the basic premise is to start up a communications program, configure the program for your modem and see if you can dial another computer.
Most Linux distributions will have the communications program Minicom written by Miquel van Smoorenburg. To start it you just type minicom. You may have to be logged in as the root user to use it.
On starting Minicom type the at command (this command is one of the Hayes commands that are used by most modems, they have nothing to do with UNIX). If an OK is the response then your minicom is talking with your modem.
If it isn't you may need to change the configuration of minicom to recognise your modem. To get help on how to do this hit the CTRL-A Z key combination. This means hold the CTRL key down, hit the A key, release both the CTRL and A keys and hit the Z key.
Connect a modem to your UNIX computer and test to see if it is working.
Again the following text is taken from the serial how-to verbatim
For dial out use only, you can configure your modem however you want. If you intend to use your modem for dialin, you must configure your modem at the same speed that you intend to run getty at. So, if you want to run getty at 38400 bps, set your speed to 38400 bps when you configure your modem. This is done to prevent speed mismatches between your computer and modem.
I like to see result codes, so I set Q0 - result codes are reported. To set this on my modem, I would have to precede the register name with an AT command. Using kermit or some comm program, connect to your modem and type the following: ATQ0. If your modem says OK back to you, then the register is set. Do this for each register you want to set.
I also like to see what I'm typing, so I set E1 - command echo on. If your modem has data compression capabilities, you probably want to enable them. Consult your modem manual for more help, and a full listing of options. If your modem supports a stored profile, be sure to write the configuration to the modem (often done with AT&W, but varies between modem manufacturers) if not you will have to set the registers every time you turn on, or reset your modem.
If your modem supports hardware flow control (RTS/CTS), I highly recommend you use it. This is particularly important for modems that support data compression. First, you have to enable RTS/CTS flow control on the serial port itself. This is best done on startup, like in /etc/rc.d/rc.local or /etc/rc.d/rc.serial. Make sure that these files are being run from the main rc.M file! You need to do the following for each serial port you want to enable hardware flow control on:
|
stty crtscts <
/dev/cuaN |
You must also enable RTS/CTS flow control on your modem. Consult your modem manual on how to do this, as it varies between modem manufacturers. Be sure to save your modem configuration if your modem supports stored profiles.
Back to some original text
For a dial-in modem you must start the login process in much the same way as is done for a dumb terminal. Refer to the previous section on starting the login process for a dumb terminal, the serial-howto and the manual page for agetty for more information.
Configure your modem for dialing in. In conjunction with a friend test whether or not someone can login using the modem connection. (To login they will need an account on your machine)
Dumb terminals and modems are generally connected to a UNIX machine using serial ports. RS-232 is the standard for serial connections. Most devices are placed into one of two categories data terminal equipment (DTE, most terminals, computers and printers) and data communications equipment (DCE, modems).
Connecting a dumb terminal to a UNIX box includes the following steps
§ configuring the terminal,
§ connecting the terminal,
§
starting a getty
process for the terminal,
Under Linux this is done by adding an appropriate entry to the /etc/initab
file.
§
configuring the line characteristics through software,
Done using the stty
command.
§
ensuring that the terminal type appears in the terminal database and that
the TERM shell
variable is set correctly.
The terminal database might either be /etc/termcap
or terminfo
depending on the version of UNIX.
Modems can be used to either dial in or dial out. The process for configuring and connecting a modem to a UNIX computer is similar to that for a dumb terminal.
In what ways can two serial cables differ?
What type of serial cable would you use to connect
§ a modem to a UNIX computer,
§ a dumb terminal to a UNIX computer.
List and explain all the steps in the UNIX login process.
Explain the purpose of each of the following (as related to connecting terminals and modems to a UNIX computer)
§ the stty command
§ termcap and terminfo
§ /etc/inittab
You've just obtained an old terminal. Describe the steps you would have to perform to connect it to your Linux machine.
You've connected the terminal from review question 18.5 but when you start using it you discover that you don't have an entry in your /etc/termcap file for this type of terminal. What do you do?