Introduction
In this lab, you will learn how to use the tty command in Linux to identify the current terminal device and manage terminal sessions. The tty command is a useful tool for system administrators and developers who need to interact with the terminal or automate terminal-based tasks. You will start by understanding the basic functionality of the tty command, then learn how to identify the current terminal device, and finally explore how to manage terminal sessions using the tty command. This lab provides practical examples to help you apply the concepts learned in a real-world scenario.
Understand the tty Command
In this step, you will learn about the tty command, which is used to identify the current terminal device. The tty command is a useful tool for system administrators and developers who need to interact with the terminal or automate terminal-based tasks.
First, let's run the tty command to see the current terminal device:
tty
Example output:
/dev/pts/0
The output shows that the current terminal device is /dev/pts/0. This is a pseudo-terminal device, which is a virtual terminal created by the system to handle interactive user sessions.
The tty command can also be used to check if the current session is running in a terminal or not. If the output is not a tty, it means the current session is not running in a terminal, but rather in a non-interactive environment, such as a script or a background process.
tty
Example output:
not a tty
In this case, the tty command indicates that the current session is not running in a terminal.
The tty command can be useful in shell scripts to check the current terminal device or to determine if the script is running in an interactive session or a non-interactive environment.
Identify the Current Terminal Device
In this step, you will learn how to identify the current terminal device using the tty command.
First, let's verify that we are in an interactive terminal session by running the tty command:
tty
Example output:
/dev/pts/0
The output shows that the current terminal device is /dev/pts/0, which is a pseudo-terminal device.
You can also use the who command to get more information about the current terminal session:
who
Example output:
labex pts/0 2023-04-12 15:22 (172.17.0.1)
The output shows that the current user labex is logged in on the terminal device /dev/pts/0.
To get the terminal type (e.g., xterm, vt100, dumb), you can use the echo $TERM command:
echo $TERM
Example output:
xterm-256color
This indicates that the current terminal type is xterm-256color.
Understanding the current terminal device and type can be useful when writing shell scripts or troubleshooting terminal-related issues.
Manage Terminal Sessions with the tty Command
In this step, you will learn how to manage terminal sessions using the tty command.
The tty command can be used to switch between different terminal sessions. For example, you can use the tty command to create a new terminal session and then switch back to the original session.
First, let's create a new terminal session using the script command:
script /tmp/terminal-session.log
This will start a new terminal session and record all the commands and output to the /tmp/terminal-session.log file.
You can now run various commands in this new terminal session. When you're done, you can exit the session by typing exit:
exit
This will return you to the original terminal session.
You can now view the log file that was created during the terminal session:
cat /tmp/terminal-session.log
The tty command can also be used to get the name of the current terminal device, which can be useful when writing shell scripts that need to interact with the terminal.
tty
Example output:
/dev/pts/0
This shows that the current terminal device is /dev/pts/0.
Understanding how to manage terminal sessions using the tty command can be useful when automating tasks or troubleshooting terminal-related issues.
Summary
In this lab, you learned about the tty command, which is used to identify the current terminal device. You discovered that the tty command can be used to check if the current session is running in a terminal or not, and how to use it to get information about the current terminal session, such as the terminal device and the terminal type.
Additionally, you learned how to use the who command to get more information about the current terminal session, including the username and the terminal device.



