Linux agetty Command with Practical Examples

LinuxLinuxBeginner
Practice Now

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.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/man("`Manual Access`") linux/UserandGroupManagementGroup -.-> linux/useradd("`User Adding`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") subgraph Lab Skills linux/cat -.-> lab-422541{{"`Linux agetty Command with Practical Examples`"}} linux/man -.-> lab-422541{{"`Linux agetty Command with Practical Examples`"}} linux/useradd -.-> lab-422541{{"`Linux agetty Command with Practical Examples`"}} linux/passwd -.-> lab-422541{{"`Linux agetty Command with Practical Examples`"}} linux/nano -.-> lab-422541{{"`Linux agetty Command with Practical Examples`"}} end

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/[email protected]

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/[email protected]

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 [email protected]
sudo systemctl start [email protected]

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.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like