Introduction
In this lab, we will explore the Linux agetty command and its practical applications. The lab covers understanding the purpose and usage of the agetty command, configuring it for serial console access, and managing user login processes. We will learn how agetty is responsible for displaying the login prompt, handling the user login process, initializing the terminal settings, and launching the user's shell after a successful login. Additionally, we will configure agetty to enable serial console access on our system. This lab provides a comprehensive understanding of the agetty command and its role in managing user login processes in a Linux environment.
Understand the Purpose and Usage of agetty Command
In this step, we will explore the purpose and usage of the agetty command in Linux. The agetty command is a program that manages the user login process on the system's virtual consoles or serial ports.
First, let's check the version of agetty installed on our system:
agetty --version
Example output:
agetty (util-linux 2.37.2)
The agetty command is responsible for:
- Displaying the login prompt
- Handling the user login process
- Initializing the terminal settings
- Launching the user's shell after a successful login
To understand the basic usage of agetty, we can run the following command:
man agetty
This will open the manual page for the agetty command, providing detailed information about its options and usage.
Configure agetty for Serial Console Access
In this step, we will configure agetty to enable serial console access on our system.
First, let's check the current configuration of agetty by examining the /etc/inittab file (note that in Ubuntu 22.04, the /etc/inittab file is no longer used, so we'll use the systemd configuration instead):
sudo cat /etc/systemd/system/getty@.service
This will show the default configuration for the getty service, which is used by agetty to manage the login process.
Next, let's configure agetty to enable serial console access. We'll create a new systemd service file for the serial console:
sudo nano /etc/systemd/system/serial-getty@.service
Add the following content to the file:
[Unit]
Description=Serial Getty on %I
After=systemd-user-sessions.service plymouth-quit-wait.service
[Service]
ExecStart=-/sbin/agetty --keep-baud 115200,38400,9600 --noclear %I $TERM
Type=idle
Restart=always
UtmpIdentifier=%I
TTYPath=/dev/%I
TTYReset=yes
TTYVHangup=yes
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes
[Install]
WantedBy=getty.target
This configuration sets up agetty to listen on the serial console at 115200, 38400, and 9600 baud rates, and automatically restart the service if it crashes.
Save the file and enable the new systemd service:
sudo systemctl enable serial-getty@ttyS0.service
sudo systemctl start serial-getty@ttyS0.service
This will start the agetty process for the serial console on the ttyS0 device.
Manage User Login Processes with agetty
In this final step, we will explore how to manage user login processes using the agetty command.
First, let's create a new user account that we can use to test the login process:
sudo useradd -m testuser
sudo passwd testuser
This will create a new user account named "testuser" and set a password for the account.
Next, we can use the agetty command to simulate a user login process. Run the following command:
sudo agetty --login-program /bin/login --autologin testuser tty1
This command will start an agetty process that automatically logs in the "testuser" account on the tty1 virtual console.
You can now switch to the tty1 console (usually accessed by pressing Ctrl+Alt+F1) and you should see the user logged in as "testuser".
To terminate the agetty process, press Ctrl+C in the terminal where you ran the agetty command.
Summary
In this lab, we first explored the purpose and usage of the agetty command, which manages the user login process on the system's virtual consoles or serial ports. We learned that agetty is responsible for displaying the login prompt, handling the user login process, initializing the terminal settings, and launching the user's shell after a successful login.
Next, we configured agetty to enable serial console access on our system. We created a new systemd service file for the serial console, which sets up agetty to listen on the serial port and handle the login process for serial console users.



