Introduction to the stty Command in Linux
This tutorial will guide you through using the stty
command in Linux for terminal and serial port configuration. We will explore the command's function and syntax, providing practical examples of how to adjust terminal settings like baud rate, row and column count, and various control character definitions. This guide falls under the System Configuration and Settings skill set, highlighting the importance of the stty
command as a vital tool for systemadmin tasks involving terminal management within Linux environments.
Understanding the Purpose and Syntax of the stty Command
This section will explain the purpose and correct syntax of the stty
command within Linux operating systems. The stty
command serves as a utility to configure and manage the settings for terminals or serial port interfaces.
Let's examine the fundamental syntax of the stty
command:
stty [OPTION] [SETTING]
The OPTION
can accept the following parameters:
-a
: Display all current settings that are active for the terminal.-g
: Output all the current settings in a format suitable for reuse as an argument for anotherstty
command.
The SETTING
value represents a variety of possible terminal configurations, among them:
speed
: Establish the baud rate of the terminal.rows
: Set the number of rows visible on the terminal.cols
: Set the number of columns visible on the terminal.intr
: Define the interrupt character.quit
: Define the quit character.erase
: Define the erase character.kill
: Define the kill character.eof
: Define the end-of-file character.
Below are examples for better understanding of the use of the stty
command:
## Display all current terminal settings
stty -a
Example output:
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^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 -iutf8
-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 -flusho -extproc
This output reveals the current terminal's configuration, including parameters such as baud rate, row and column count, as well as settings for various control characters.
The following examples show modification of settings:
## Set the terminal baud rate to 9600
stty speed 9600
## Set the number of rows to 30
stty rows 30
## Set the number of columns to 100
stty cols 100
To confirm the changes, run the stty -a
command after executing the above commands.
Modifying Terminal Settings Using the stty Command: A Comprehensive Guide
This guide focuses on leveraging the stty
command to modify terminal settings.
First, begin by checking the current terminal settings using the following command:
stty -a
This provides a complete list of the current terminal configurations, including the baud rate, dimensions (rows and columns), and the assigned control characters.
The following commands demonstrate how to modify various settings:
## Set the terminal to use 7-bit character mode
stty cs7
## Set the terminal to use 8-bit character mode
stty cs8
## Set the terminal to use no parity
stty -parenb
## Set the terminal to use even parity
stty parenb -parodd
## Set the terminal to use odd parity
stty parenb parodd
## Set the terminal to use hardware flow control
stty crtscts
## Set the terminal to use software flow control
stty -crtscts ixon ixoff
## Set the terminal to ignore break conditions
stty -ignbrk
## Set the terminal to generate a signal when a break condition is detected
stty ignbrk brkint
After entering these commands, rerun stty -a
to verify the applied changes.
Practical Examples of Using the stty Command for System Administration
This section offers practical examples illustrating effective applications of the stty
command in systemadmin tasks.
- Disabling the Ctrl+C Interrupt Function
## Disable Ctrl+C interrupt
stty -intr
This command disables the Ctrl+C interrupt, which is commonly used to halt a process. Use the sleep 60
command to confirm, which would normally be terminated by Ctrl+C.
- Customizing the Erase Character
## Change the erase character to Backspace
stty erase ^?
Changes the default erase character (often Delete) to Backspace. Test by entering text in the terminal and using Backspace.
- Adjusting Terminal Size
## Set the terminal size to 80 columns and 24 rows
stty cols 80 rows 24
This configures the terminal display to 80 columns and 24 rows. The stty -a
or resize
commands can verify the change.
- Saving and Restoring Terminal Configurations
## Save the current terminal settings
stty -g > terminal_settings.txt
## Restore the saved terminal settings
stty $(cat terminal_settings.txt)
Saves the current terminal setup to a file for later restoration, useful for temporary configuration changes and reverting to the original settings. Helpful for systemadmin tasks requiring temporary terminal adjustments.
Summary: Mastering Terminal Settings with stty
This lab introduced the stty
command in Linux, a key tool for configuring and controlling terminal or serial port behavior. You learned to display settings via the -a
option, and how to change parameters like baud rate, dimensions, and control characters. The guide's practical examples demonstrated the stty
command's broad capabilities in managing terminal configurations within a Linux environment, a valuable skill for any systemadmin.