Chapter Objective
Log in to a Red Hat Enterprise Linux system's graphical and command-line environments, and run simple commands using the bash shell correctly.
Key Commands
bashpwdechohistorymanaproposwhich
Log in to a Red Hat Enterprise Linux system's graphical and command-line environments, and run simple commands using the bash shell correctly.
bashpwdechohistorymanaproposwhichThe shell is a command interpreter that reads what you type, runs the corresponding program, and displays the results. On Red Hat Enterprise Linux, the default shell is bash (the Bourne Again SHell).
Every time you open a terminal, you're running an instance of the shell. It reads commands one line at a time, executes them, and prints a new prompt when it's ready for the next command.
The default prompt shows your username, hostname, and current directory, ending in $ for a regular user or # for the root user.
# Regular user prompt [student@server1 ~]$ # Root user prompt (note the #) [root@server1 ~]#
$ or # before running a command. A # prompt means you are root and can damage the system with a single mistyped command.
RHEL provides several ways to reach a shell prompt, depending on whether the system has a graphical desktop installed.
| Method | Where | Typical Use |
|---|---|---|
| GNOME Terminal | Graphical desktop | Local workstation use |
| Virtual console | Ctrl+Alt+F3–F6 | Direct console login, no GUI needed |
| SSH | Remote network login | Managing remote servers |
| Text-mode boot target | Servers with no GUI | Most production RHEL servers |
RHEL provides multiple virtual consoles. From the graphical desktop, switch to a text console with Ctrl+Alt+F3 (through F6), and return to the graphical session with Ctrl+Alt+F1 or F2.
Most commands follow the same general pattern:
command [options] [arguments]
- or --# A command with no options or arguments pwd # A short option ls -l # A long option ls --all # Short options can often be combined ls -la # A command with an option and an argument ls -l /etc
The shell treats spaces, quotes, and characters like *, $, and > specially. Quote arguments that contain spaces so the shell treats them as a single value.
# Without quotes, the shell sees two arguments echo Hello World # With quotes, the shell sees one argument echo "Hello World" # Single quotes prevent the shell from expanding variables echo '$HOME stays literal here' # Double quotes still allow variable expansion echo "$HOME expands here"
$variables and command substitution). This trips up new administrators constantly.
Bash keeps a history of commands you've run and provides keyboard shortcuts, inherited from the Emacs editing model, for editing the current line without retyping it.
| Shortcut | Action |
|---|---|
Ctrl+A | Move cursor to the start of the line |
Ctrl+E | Move cursor to the end of the line |
Ctrl+U | Delete from cursor to start of line |
Ctrl+K | Delete from cursor to end of line |
Ctrl+R | Search backward through command history |
Ctrl+C | Cancel the current command |
↑ / ↓ | Step backward / forward through history |
# Show your recent command history history # Re-run command number 482 from history !482 # Re-run the most recent command !! # Search history interactively (press Ctrl+R, then type) (reverse-i-search)`ssh': ssh student@server1
Ctrl+R and start typing a fragment of the command you want. Press Ctrl+R again to cycle to earlier matches.
Every command that expects a file or directory understands both absolute paths (starting from /) and relative paths (starting from your current directory).
# Show your current working directory pwd # Absolute path — always starts from root cd /etc/ssh # Relative path — starts from wherever you currently are cd ../ssh # Shortcuts cd ~ # your home directory cd - # the previous directory cd .. # the parent directory
Press Tab while typing a command, file name, or path and bash will complete it automatically. Press Tab twice to list all possible matches when more than one exists.
# Type this, then press Tab cd /etc/ss[Tab] # bash completes to: cd /etc/ssh/
RHEL ships extensive built-in documentation. You rarely need to leave the terminal to learn how a command works.
| Tool | Purpose |
|---|---|
man | Full manual page for a command, file, or system call |
--help | Quick summary of a command's options |
apropos | Search man page descriptions by keyword |
which | Show the full path to a command's executable |
# Read the full manual page for a command man ssh # Quick reference of a command's options ssh --help # Search man page descriptions for a keyword apropos partition # Find the exact program a command runs which ssh
| Key | Action |
|---|---|
Space | Scroll forward one page |
b | Scroll backward one page |
/pattern | Search forward for pattern |
n | Repeat the last search |
q | Quit the man page |
passwd the command versus passwd the file format). Use man 5 passwd to request a specific section when the default result isn't the one you need.
$ (user) or # (root)Ctrl+Alt+F3–F6- or --/Tab is pressed$ prompt and a # prompt?$HOME?Tab twice do when there is more than one possible completion?man passwd but get the wrong page for what you need. What command would get you the file-format version instead?