agetty Command in Linux

Introduction to agetty and Linux Login Management

In this lab, we'll dive into the Linux agetty command and its diverse applications in system administration. This tutorial provides a comprehensive overview of the agetty command, including its purpose, proper usage, configuration for serial console access, and management of user login processes. You'll discover how agetty handles the login prompt, manages user authentication, initializes terminal settings, and launches the user's shell upon successful login. Furthermore, we will configure agetty to enable serial console access on our system, providing a practical understanding of its role in a Linux environment. This is a must-know for any systemadmin.

Understanding the Purpose and Usage of the agetty Command

In this section, we will delve into the core purpose and practical applications of the agetty command within a Linux system. The agetty command serves as a vital program responsible for overseeing the user login process on virtual consoles or serial ports.

First, let's determine the version of agetty installed on your system:

agetty --version

Example output:

agetty (util-linux 2.37.2)

The primary responsibilities of the agetty command include:

  • Displaying the login prompt to users.
  • Managing the entire user login and authentication process.
  • Initializing and configuring terminal settings.
  • Launching the user's designated shell upon successful login.

To grasp the basic functionality of agetty, execute the following command:

man agetty

This command will access the manual page for agetty, presenting exhaustive details regarding its options and operational usage.

Configuring agetty for Serial Console Access

This section guides you through configuring agetty to enable serial console accessibility on your Linux system, a crucial step for many systemadmin tasks.

Initially, let's inspect the current agetty configuration by examining the /etc/inittab file (it's important to note that in Ubuntu 22.04, the /etc/inittab file is deprecated, thus we will use the systemd configuration instead):

sudo cat /etc/systemd/system/[email protected]

This command will display the default settings for the getty service, which agetty leverages to manage the login procedure.

Now, let's proceed to configure agetty to activate serial console access. We'll generate a new systemd service file tailored for the serial console:

sudo nano /etc/systemd/system/[email protected]

Insert the following content into the newly created 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 establishes agetty to actively listen on the serial console at various baud rates (115200, 38400, and 9600), and it automatically restarts the service in case of a crash, ensuring continuous availability.

After saving the file, enable and initiate the new systemd service:

sudo systemctl enable [email protected]
sudo systemctl start [email protected]

This action launches the agetty process, enabling the serial console on the designated ttyS0 device.

Managing User Login Processes with agetty

In this step, we'll delve into the details of managing user login processes utilizing the agetty command. This is a core skill for any systemadmin.

Begin by creating a new user account that will be used for login process verification:

sudo useradd -m testuser
sudo passwd testuser

This sequence generates a new user account named "testuser" and establishes a password for authentication.

Subsequently, the agetty command can be employed to simulate a user login procedure. Execute the subsequent command:

sudo agetty --login-program /bin/login --autologin testuser tty1

This command initiates an agetty process that automatically authenticates and logs in the "testuser" account on the tty1 virtual console.

You can now transition to the tty1 console (typically accessed via Ctrl+Alt+F1), and you should observe the user logged in as "testuser".

To terminate the agetty process, input Ctrl+C within the terminal where you executed the agetty command.

Summary

In this lab, we began by exploring the purpose and functionality of the agetty command, a vital component for user login management on system's virtual consoles or serial ports. We learned that agetty is responsible for presenting the login prompt, managing the user authentication, initializing terminal settings, and launching the user's shell upon successful login.

Next, we configured agetty to enable serial console access on our system. We established a new systemd service file designed for the serial console, which configures agetty to actively listen on the serial port and manage the login process for serial console users, an essential skill for any systemadmin dealing with Linux servers.

400+ Linux Commands