Using the Command LineObjectives

Using the Command Line
Objectives
Use a terminal to run commands.

Linux Text Interface
Before screens were common on computers, the operating system output was displayed on automated typewriters connected to a mainframe over a telephone line. Modern computers use a text interface to interact with system features. The Linux text interface is technically known as the TTY, which stands for teletypewriter. This type of interface is commonly referred to as a command-line interface (CLI).

Multiple Login Sessions
Linux runs virtual consoles to allow users independent login sessions. These virtual consoles are also referred to as TTY consoles. The first TTY runs the GNOME Desktop Manager (GDM) service, in which you log in to the system. When you log in to the GNOME desktop, your session occupies the second TTY automatically.

Even with a graphical environment enabled, you can log in to your local machine by using a text-only console. To use a text-only console, switch to an unused TTY. For example, press Alt+Ctrl+F3 to switch to the third virtual console.

Unlike a desktop session, a console session does not lock itself after an inactivity period, so make sure to close the session after you finish your work. You can close the session by typing exit or by pressing Ctrl+d.

To return to the desktop session, press Alt+Ctrl+F2. Or you can press Alt+Ctrl+F1 to return to the GDM.

Linux Terminal
The Linux command-line interface is one of the most popular features of the operating system. Constantly switching between TTY consoles would be inconvenient, so you can use applications that emulate a TTY behavior. This application is generally known as a terminal.

The TTY console and a terminal application both run on what is called a Linux shell. A shell provides a command prompt, keyboard shortcuts for faster interaction, some built-in commands, and a number of convenient functions for programming or shell scripting. Most Linux distributions use the Bash shell.

The GNOME desktop's terminal application is intuitively named Terminal. To launch the Terminal application, click Activities in the upper left of the desktop. Type terminal on the search bar and press Enter. Alternatively, after clicking Activities, click the Grid icon in the panel at the bottom of the screen to browse through all installed applications and select the Terminal application.

After you open the Terminal application, you are automatically logged in to a Bash shell. Unlike when switching to a new TTY console, you do not need to log in to the Terminal application because you launched it within your existing login session.

Performing Tasks on a Terminal
Applications are usually high-level graphical components that interact with the system to perform specific user activities, such as creating a presentation or navigating the Internet. In Linux, programs such as text editors, music players, and graphic editors, run in the shell instead of running as graphical applications on a desktop. If the Linux system does not have a graphical interface, then you cannot interact with graphical applications, but you can perform system-related tasks with a shell.

Linux shells are popular for allowing users to perform system-wide configurations. When you use the Linux shell, either in a console or a terminal, you give the computer a command. Commands are different from applications, in that you interact with commands by using only the terminal.

Some commands are built into the shell itself, but most of them are installed on the system. When you type a command, the shell searches a set of system directories to find the command. This set of directories is known as a path.

Some commands gather information about users or the computer system itself, and other commands locate or even modify files.

Running Commands in a Terminal
In Linux, you commonly run commands to interact with the computer. By running commands, you can avoid navigating through different tools to obtain system information or to perform a simple task. For example, to view the computer name, you can open the settings application, navigate to the About section, and review the Device Name label. Alternatively, in a terminal, you use only the hostname command.

In the following example, the command returns mycomputer.example.com, which is the full name of the machine.

[user@host ~]$ hostname
mycomputer.example.com
Most commands have a default action when they are run, but the command is not limited to performing that action. You can add one or more options to a command to modify its behavior. Generally, a command option starts with a dash (-) and uses a single letter, or the option starts with two dashes (--) and uses a word.

In the following examples, you add the -d option to show the domain name and the -s option to show the short name of the machine.

[user@host ~]$ hostname -d
example.com
[user@host ~]$ hostname -s
mycomputer
A command also accepts arguments. Arguments are the target of the command, such as a file or other object upon which the command acts.

For example, the id command prints the user ID that the system uses to uniquely identify a user. In the following example, you use the id command by itself. Because you do not use options, the id command performs its default action, which is to print the current user information.

[user@host ~]$ id
uid=1000(user) gid=1000(user) groups=1000(user),10(wheel)
...output omitted...
In the following example, you use the id command with an option, but with no argument. The -u option specifies to print only the user ID. The command assumes the current user as the argument.

[user@host ~]$ id -u
1000
In the final example, you use the id command with both an option and an argument. The id command receives devops as the target user for the command. The -u option prints the devops user ID, which is different from the previous example.

[user@host ~]$ id -u devops
1001
Commands can have multiple options and the meaning of the options are not interchangeable between commands. You can view the options for a command by using the --help option.

The who command shows the users who are logged in to the machine.

[user@host ~]$ who --help
Usage: who [OPTION]... [ FILE | ARG1 ARG2 ]
Print information about users who are currently logged in.

  -a, --all same as -b -d --login -p -r -t -T -u
  -b, --boot time of last system boot
...output omitted...
The date and timedatectl commands provide time information. The main difference is that by default, the timedatectl command shows time zone information and whether the machine clock is synced with a time server. This sync information is important because some services rely on the system time to work correctly.

The following example shows the output of the date and timedatectl commands:

[student@workstation ~]$ date
Fri Sep 29 13:35:20 EDT 2023
[student@workstation ~]$ timedatectl
               Local time: Fri 2023-09-29 13:35:22 EDT
           Universal time: Fri 2023-09-29 17:35:22 UTC
                 RTC time: Fri 2023-09-29 17:35:22
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
The passwd command update user passwords. By default, it changes the current user's password, so you do not need to specify the account name as an argument. The root user, however, can change the password for any user. In this scenario, you must specify the target account as the argument.

Tab Completion
With tab completion, you request that the shell program completes a command by pressing the Tab key. If you have typed enough characters that a command word is uniquely matched, then the shell completes the word. If the characters that you entered do not match a unique command, then the shell completes as many characters as it can, and then stops until you provide additional characters. Pressing Tab a second time displays the command words that match the typed pattern.

In the following example, typing host and pressing Tab twice displays four choices. When you type an additional n and press Tab again, the shell completes the hostname command.

[user@host ~]$ hostTab+Tab 1
host hostid hostname hostnamectl
[user@host ~]$ hostnTab 2
[user@host ~]$ hostname
mycomputer.example.com

Guided Exercise: Using the Command Line
Guided Exercise: Using the Command Line
Access the Linux shell by using a virtual console or terminal application, and learn commands to gather information about a Linux system.

Outcomes


Log in to a Linux shell.

Use non-interactive commands to view information about a Linux system.

Modify a user's password.


As the student user on the workstation machine, open a terminal. You can open a terminal by clicking Activities and then Terminal.

On the command line, use the following lab command to prepare your system for this exercise, and to ensure that all required resources are available.

[student@workstation ~]$ lab start access-cli
Instructions

View information about your login session:

[student@workstation ~]$ who
student pts/0 2023-09-27 22:01 (172.25.250.254)
View the current date, time, and time zone:

[student@workstation ~]$ timedatectl
               Local time: Wed 2023-09-27 22:01:46 EDT
           Universal time: Thu 2023-09-28 02:01:46 UTC
                 RTC time: Thu 2023-09-28 02:01:46
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
View the machine name:

[student@workstation ~]$ hostname
workstation.lab.example.com
Review the information about the operating system, kernel, and hardware. Use the tab completion feature to run the hostnamectl command.

Type hostn and then press Tab. The shell completes the hostname command, but this is not the intended command. Review the suggestions by pressing Tab twice, type c, and press Tab again. Then run the command.

[student@workstation ~]$ hostnTab
[student@workstation ~]$ hostnameTab+Tab
hostname hostnamectl
[student@workstation ~]$ hostnamecTab
[student@workstation ~]$ hostnamectl
 Static hostname: workstation.lab.example.com
       Icon name: computer-vm
         Chassis: vm
      Machine ID: 633680e38e32491899fbd3a7f6129842
         Boot ID: f97b2fbc043a4de7803a4e8b46d3ac33
  Virtualization: kvm
Operating System: Red Hat Enterprise Linux 9.0 (Plow)
     CPE OS Name: cpe:/o:redhat:enterprise_linux:9::baseos
          Kernel: Linux 5.14.0-70.13.1.el9_0.x86_64
    Architecture: x86-64
 Hardware Vendor: Red Hat
  Hardware Model: OpenStack Compute
Most of this information is the same as the information shown in the About section of the GNOME control panel.

On the third virtual console, log in as the operator2 user and use redhat as the password. Update the user's password to rh104.cli.

Switch to the third virtual console by pressing Alt+Ctrl+F3. Log in as the operator2 user and use redhat as the password.

Review the passwd command help. The --help (or -h) option prints information about the command itself, including the options you can use to alter the command's outcome.

[operator2@workstation ~]$ passwd --help
Usage: passwd [OPTION...] <accountName>
  -k, --keep-tokens keep non-expired authentication tokens
  -d, --delete delete the password for the named account (root only); also removes password lock if any
  -l, --lock lock the password for the named account (root only)
  -u, --unlock unlock the password for the named account (root only)
...output omitted...
Update the operator2 user password to rh104.cli.

[operator2@workstation ~]$ passwd
Changing password for user operator2.
Current password: redhat
New password: rh104.cli
Retype new password: rh104.cli
passwd: all authentication tokens updated successfully.
Test the new password by closing the session and logging back in.

[operator2@workstation ~]$ exit

workstation login: operator2
Password: rh104.cli
[operator2@workstation ~]$
Close the virtual console session and return to the desktop session.

Close the virtual console session.

[operator2@workstation ~]$ exit
Return to the desktop session by pressing Alt+Ctrl+F2.

$ Alt+Ctrl+F2
Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish access-cli

Komentar

Postingan populer dari blog ini

AI Image/Video Prompt Guide

Wacana PLTN Kalimantan: Apakah Indonesia Benar-Benar Siap?