Linux Command Line Basics PDF

Summary

This document provides an introduction to Linux's command-line interface, including the Bash shell and the GNOME desktop environment. It details fundamental concepts, commands, and their usage, as well as basic navigation and administration.

Full Transcript

Chapter 2 : Accessing the Command Line Introduction to the Bash Shell Hasnaa Sleiman Master in telecommunications, Networks and Security- UA-USJ-UL Objectives Understand how to log in to a Linux system. Learn how to run commands using...

Chapter 2 : Accessing the Command Line Introduction to the Bash Shell Hasnaa Sleiman Master in telecommunications, Networks and Security- UA-USJ-UL Objectives Understand how to log in to a Linux system. Learn how to run commands using the Bash shell. Explore the structure of commands and options. What is the Command Line? The command line is a text-based interface to input instructions into a system. The shell is a program that provides the command line. Bash (Bourne-Again Shell) is the default shell. Shell Prompts Explained Regular user prompt: [user@host ~]$ Superuser prompt: [root@host ~]# Indicates whether you are logged in as a normal user or the root (superuser). Understanding Shell Commands Command: The program you want to run. Options: Adjust the behavior of the command (e.g., - L). Arguments: Targets or files for the command (e.g., user01). Logging in Locally Physical console supports multiple virtual consoles. Switch between them with Ctrl+Alt+F1 through F6. A graphical login screen might be available on tty1. SSH - Logging in Over the Network Remote Login with SSH SSH (Secure Shell): Allows secure remote login to another machine. Command Example: ssh remoteuser@remotehost. Encrypts communication to prevent eavesdropping. Public Key Authentication in SSH Uses a private key on the user’s system and a public key on the server for secure login without a password. Example: ssh -i mylab.pem remoteuser@remotehost. Logging Out Ending a session Use the exit command or Ctrl+D to close the session. In an SSH session: exit will log you out and close the connection to the remote machine. Key takeaways Bash is the default shell in Linux for accessing the command line. Basic shell commands include commands, options, and arguments. You can log in to a system either locally or remotely using SSH. Understanding GNOME 3 and Linux Shell Commands Objectives Log in to the Linux system using the GNOME 3 desktop environment. Run commands from a shell prompt in a terminal program Introduction to GNOME Desktop Environment GNOME 3 is the default desktop environment in Red Hat Enterprise Linux 8. Integrated user interface and unified development platform. Built on Wayland or X Window System. GNOME Shell Core user interface functions for the GNOME desktop environment. Customizable appearance. Red Hat Enterprise Linux 8 uses the "Standard" theme. Red Hat Enterprise Linux 7 used a "Classic" theme. First-Time Login Initial setup configures basic account settings. GNOME Help starts with the Getting Started screen. Access GNOME Help through the Activities button or the life ring icon. GNOME shell Parts Top bar: Provides access to Activities, system settings, and volume control. Activities overview: Organizes windows and starts applications. Dash: Displays favorite and running applications. Windows overview: Shows active windows. Workspace selector: Switch between different workspaces. Message tray: Displays notifications and system messages. Workspaces Organize windows by task in separate workspaces. Switch workspaces with Ctrl+Alt+UpArrow or DownArrow. Move windows between workspaces in the Activities overview. Starting a termina; Start GNOME Terminal from the dash or using the Alt+F2 command. Displays a shell prompt with the user name, host, and current directory. Locking the Screen or Logging Out Lock the screen via the system menu or with Super+L. Log out via the system menu, under the user's name. Powering Off or Rebooting Power off from the system menu or use Ctrl+Alt+Del. Automatic shutdown after 60 seconds if no choice is made. Overview of Basic Commands and Shortcuts Commands consist of command name, options, and arguments. Example: command -option argument Press Enter to execute a command. Use ; to run multiple commands on one line. Example: command1; command2 Examples of Simple Commands date: Displays date and time. Example: date, date +%R passwd : Changes user’s password. Example: passwd file: Identifies file types. Example: file /etc/passwd cat: Displays file contents. Example: cat /etc/passwd less: Pages through file contents. Example: less /etc/passwd head and tail: Shows beginning or end of files. Example: head /etc/passwd, tail -n 3 /etc/passwd wc: Counts lines, words, and characters. Example: wc /etc/passwd Tab Completion Completes commands or file names. Press Tab once to complete, twice to list options. Example: Typing pas and pressing Tab shows passwd, paste, etc. Continuing Long Commands Use \ to split commands across lines. Example: head -n 3 \ /usr/share/dict/words \ /usr/share/dict/linux.words History Command history: Lists previously executed commands. Use !number or !string to repeat commands. Example: !ls, !26 Command-Line Editing Editing Shortcuts: Ctrl+A: Beginning of line Ctrl+E: End of line Ctrl+U: Clear to beginning Ctrl+K: Clear to end Ctrl+LeftArrow: Previous word Ctrl+RightArrow: Next word Ctrl+R: Search history Summary Bash Shell as a Command Interpreter: Bash (Bourne Again Shell) is a command-line interface (CLI) program that interprets the commands typed by users. It takes user inputs (commands, options, and arguments), executes them, and displays the results. For example, when you type ls in the shell and press Enter, Bash interprets this as a request to list directory contents. Using --help for Command Assistance: Many commands in Linux come with a built-in help option (--help). This displays useful information about how to use the command, including available options and syntax. For instance, typing ls --help provides details on how to use the ls command. Workspaces for Organizing Windows Workspaces are a feature that allows you to organize multiple open windows on a desktop by spreading them across different virtual desktops. This helps in reducing clutter and organizing tasks. You can switch between workspaces to group related applications together, such as keeping a terminal in one workspace and a web browser in another. Using the Activities Button to Organize and Launch Applications: The Activities button, typically found at the top-left corner of the screen in GNOME (a popular Linux desktop environment), provides an overview of open windows and applications. It allows you to quickly switch between windows, move windows to different workspaces, and start new applications. The file Command to Identify File Types: The file command is used to determine the type of a file based on its contents. It doesn’t rely on file extensions (like.txt or.jpg), but scans the file's internal structure to identify its type. For example, file myfile might return "ASCII text" or "JPEG image data" depending on the file contents. Using head and tails commands The head command displays the first few lines (default is 10) of a file. Similarly, tail displays the last few lines. For instance, head myfile.txt shows the first 10 lines of the file, while tail -n 5 myfile.txt shows the last 5 lines. Tab Completion for Efficiency: Tab completion helps you automatically complete command names or file names after typing part of the name and pressing the Tab key. This saves time and reduces typing errors. If there are multiple possible completions (e.g., multiple files with similar names), pressing Tab twice shows a list of available matches. Lab PERFORMANCE CHECKLIST In this lab, you will use the Bash shell to execute commands. OUTCOMES Successfully run simple programs using the Bash shell command line. Execute commands used to identify file types and display parts of text files. Practice using some Bash command history "shortcuts" to more efficiently repeat commands or parts of commands. BEFORE YOU BEGIN Log in to workstation as student using student as the password. On workstation, run the lab cli-review start script to set up a clean lab environment. T he script also copies the zcat file to student's home directory. [student@workstation ~]$ lab cli-review start 1. Use the date command to display the current time and date. 2. Display the current time in 1 2-hour clock time (for example, 11:42:11 AM). Hint: The format string that displays that output is %r. 3. What kind of file is /home/student/zcat? Is it readable by humans? 4. Use the wc command and Bash shortcuts to display the size of zcat. 5. Display the first 10 lines of zcat. 6. Display the last 10 lines of the zcat file. 7. Repeat the previous command exactly with three or fewer keystrokes. 8. Repeat the previous command, but use the -n 20 option to display the last 20 lines in the file. Use command-line editing to accomplish this with a minimal number of keystrokes. 9. Use the shell history to run the date +%r command again.

Use Quizgecko on...
Browser
Browser