Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 2Naam in het boek: Using Essential Tools

Work in the shell

The shell (Bash) is where you do almost all the work on a Linux server. This chapter shows how commands are built, how to send output to files and other commands, how to repeat old commands, how to edit files with vim or nano, and how to find help without internet.

  • 16 Lessen
  • 33 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Examendoelen in dit hoofdstuk

  • Een shell openen en commando's met de juiste syntax typen
  • Invoer- en uitvoeromleiding gebruiken (>, >>, |, 2>)
  • De systeemdocumentatie vinden en gebruiken: man, info en /usr/share/doc

2.1

Know the three kinds of commands

A command can be an alias, a built-in of the shell, or a program on the disk. type tells you which one it is.

  • An alias is a short name for a longer command, like ll for ls -l --color=auto.
  • A built-in is part of Bash itself, like cd, echo or history.
  • A program (external command) is a file on the disk, found through the folders in $PATH.
type ll
ll is aliased to `ls -l --color=auto'
type cd
cd is a shell builtin
type passwd
passwd is /usr/bin/passwd
which passwd
/usr/bin/passwd
echo $PATH
/home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin

When you type a name, Bash looks in this order: alias → built-in → the folders of $PATH (left to right). The current folder is not in the path, so start your own script with ./:

./backup.sh

Onthoud

type NAME answers "what will run?". which NAME only searches $PATH.

Let op

An alias with the same name as a program wins. To run the real program once, start it with a backslash: \ls.

2.2

Type commands with options and arguments

A command line is: the command, then options (they change how it works), then arguments (what it works on).

ls -l -a /etc
ls -la /etc
ls --all --format=long /etc
  • Short options have one dash and one letter (-l). You can join them: -la.
  • Long options have two dashes and a word (--all). They are easier to read in scripts.
  • Arguments are names of files, folders, users …

Join commands on one line:

SignMeaningExample
;Run one after the othercd /tmp; ls
&&Run the next one only if the first one workeddnf install -y httpd && systemctl start httpd
||Run the next one only if the first one failedping -c1 server2 || echo "server2 is down"

Onthoud

&& = "and then if OK", || = "or else".

2.3

Send output to a file (redirection)

Every command has 3 channels: 0 = input, 1 = output, 2 = errors. Redirection sends a channel to a file instead of the screen.

SignWhat it does
>Output to a file. The file is emptied first.
>>Output added to the end of a file.
2>Errors to a file.
2>&1Errors go to the same place as the output.
&>Output and errors to one file.
<Read the input from a file.
/dev/nullA "black hole": everything sent there is gone.
ls /etc > etc-list.txt
date >> etc-list.txt
find / -name "*.conf" 2> /dev/null
find / -name "*.conf" &> find-all.txt
mail -s "report" root < report.txt

Onthoud

> = new, >> = add, 2> = errors.

Let op

> on an important file empties it at once. Use >> when you are not sure.

2.4

Connect commands with pipes

A pipe | sends the output of one command as input to the next one. Small tools together do big jobs.

ps aux | grep sshd
ls -l /etc | less
cat /var/log/messages | grep -i error | wc -l
dnf list installed | tee installed.txt | wc -l

tee writes the data to a file and passes it on, so you can save a step in the middle of a pipe.

Op het werk

Build a pipe step by step. Run the first command, look at the output, then add the next |.

2.5

Use the command history

Bash remembers your commands in ~/.bash_history. Repeat or search them instead of typing again.

history
history 10
!42
!!
!dnf
sudo !!
Keys / commandWhat it does
↑ and ↓Go through the last commands
Ctrl+R then textSearch back in the history; press Ctrl+R again for the next match
!42Run command number 42 again
!!Run the last command again (sudo !! = last command with sudo)
!dnfRun the last command that started with dnf
history -cClear the history of this shell
history -wWrite the current history to the file now

The variable HISTSIZE sets how many commands Bash keeps in memory, HISTFILESIZE how many are kept in the file.

Let op

A password typed on the command line ends up in the history file. Remove it with history -d NUMBER, then history -w.

2.6

Save typing with Tab completion

Press Tab to complete a command, a file name or an option. Press Tab twice to see all possible choices.

systemctl sta<Tab>
systemctl status ssh<Tab>
nmcli con mod enp1s0 ipv4.<Tab><Tab>

Completion for options and sub-commands comes from the package bash-completion. It is installed on most systems.

Examentip

Tab completion is the fastest way to type long names without typing mistakes. Use it all the time in the exam.

2.7

Edit a file with vim

vim has two main modes. In command mode keys are commands. In insert mode keys are text. Esc always takes you back to command mode.

vim /etc/hosts
KeyWhat it does
i / a / oInsert before the cursor / after the cursor / on a new line
EscBack to command mode
:wq or ZZSave and quit
:q!Quit without saving
dd / 5ddDelete (cut) one line / 5 lines
yy / pCopy a line / paste it under the cursor
u / Ctrl+RUndo / redo
/text then nSearch for text, then go to the next match
gg / GGo to the first line / to the last line
:%s/old/new/gReplace every "old" with "new" in the whole file
:set numberShow line numbers

Onthoud

Lost? Press Esc twice, then type :q! to leave without changes.

Op het werk

Run vimtutor once. It is a 30-minute lesson inside the terminal.

Commando's in deze les

2.8

Edit a file with nano

nano shows its keys at the bottom of the screen. ^ means the Ctrl key.

nano notes.txt
KeysWhat it does
Ctrl+O then EnterSave
Ctrl+XQuit (it asks to save)
Ctrl+WSearch
Ctrl+K / Ctrl+UCut a line / paste it
Alt+UUndo

Examentip

nano is fine in the exam. Use the editor you know best; speed matters.

Commando's in deze les

2.9

Use variables

A variable keeps a value under a name. Environment variables are passed on to programs you start from the shell.

MYDIR=/var/www/html
echo $MYDIR
export MYDIR
env | grep MYDIR
unset MYDIR
  • NAME=value makes a shell variable (no spaces around =).
  • export NAME makes it an environment variable: programs started from this shell see it.
  • env (or printenv) shows the environment variables; set shows all variables and functions.

Important variables: PATH (where to find programs), HOME (your home folder), USER, LANG (language), PS1 (the prompt), EDITOR (the editor that other tools open).

Onthoud

No spaces around =. export = give it to child programs.

2.10

Know the files that set up the shell

When you log in, Bash reads a few files that set variables and aliases. Some are for all users, some only for you.

FileFor whomWhen
/etc/profile and /etc/profile.d/*.shAll usersAt login
/etc/bashrcAll usersEvery new shell
~/.bash_profileOne userAt login
~/.bashrcOne userEvery new shell
  1. Put your own settings in ~/.bashrc, for example an alias.
    echo "alias ll='ls -l --color=auto'" >> ~/.bashrc
  2. Load the file in the current shell without logging out.
    source ~/.bashrc

For all users, do not change /etc/profile: add a small file in /etc/profile.d/ instead. Updates of the system do not overwrite it.

Onthoud

login = profile files, every shell = bashrc files. source reads a file now.

2.11

Show a message at login

/etc/motd is shown after a user logs in. /etc/issue is shown before login on a text console.

echo "Maintenance on Saturday 22:00-23:00. Save your work." > /etc/motd
echo "Authorized users only. All activity is logged." > /etc/issue

Op het werk

Many security policies want a warning text before login. For SSH, put it in a file and set Banner /etc/issue.net in /etc/ssh/sshd_config.

Commando's in deze les

2.12

Find help with --help and man

Help is on the system: --help for a quick list of options, man for the full manual, man -k to find the right command.

  1. Quick help:
    useradd --help
  2. The full manual. Search with /word, n for the next match, q to quit.
    man useradd
    man 5 passwd
  3. Do not know the command? Search the short descriptions of all manuals:
    man -k partition
    apropos partition | grep 8
  4. If man -k says "nothing appropriate", build the search database:
    mandb
  5. More documentation, often with examples:
    ls /usr/share/doc/
    info coreutils
SectionContains
1Commands for all users
5Configuration files (like passwd, fstab)
8Commands for root (system administration)

Onthoud

1 = user commands, 5 = files, 8 = admin commands.

Examentip

There is no internet in the exam. Practice finding answers with man -k, man, and the EXAMPLES section at the end of many man pages.

2.13

Ask the AI assistant (RHEL Lightspeed)

RHEL 10 has a command-line assistant: the c command sends a question to Red Hat's AI service and shows an answer.

dnf install command-line-assistant
c "how do I make a user an administrator"

It needs a registered system and an internet connection. Do not type secrets or personal data in a question.

Examentip

The exam has no internet, so the assistant does not work there. Use man and --help.

2.14

Remember the main points

The most important facts of this chapter, in short.

  • A command has three parts: the command, its options (-l, --all) and its arguments (files, users …).
  • The shell finds commands as aliases, built-ins and programs in $PATH; type NAME shows which one runs.
  • Redirect output with > (overwrite), >> (add), errors with 2>, both with &>; read input with <.
  • A pipe | sends the output of one command into the next command.
  • history, !number, !! and Ctrl+R repeat old commands; Tab completes names.
  • vim has modes: Esc for command mode, i to type, :wq to save and quit, :q! to quit without saving.
  • Variables hold values; export makes them visible to child programs. Login settings live in /etc/profile, ~/.bash_profile and ~/.bashrc.
  • Help comes from man, man -k (apropos), --help, info and /usr/share/doc.

2.15

Learn the key terms

The words of this chapter in simple English.

TermMeaning
ShellThe program that reads your commands and starts them (Bash on RHEL).
Internal commandA command built into the shell, like cd or echo.
External commandA program file on disk, found through $PATH.
$PATHThe list of folders where the shell looks for programs.
STDIN / STDOUT / STDERRInput (0), normal output (1) and error output (2) of a program.
RedirectionSending input or output to or from a file.
PipeConnecting the output of one command to the input of the next.
Environment variableA name with a value that programs can read, like LANG or HOME.
AliasA short name for a longer command.
man pageThe manual of a command, in sections (1 = user commands, 5 = file formats, 8 = admin commands).

2.16

Check what you learned

Think about the answer first, then open it.

1How do you send only the error messages of a command to a file?

With 2>, for example find / -name x 2> errors.txt.

2What is the difference between > and >>?

> replaces the file; >> adds to the end of the file.

3How do you find which man page talks about passwords if you do not know the command?

man -k password (the same as apropos password). Run mandb first if the list is empty.

4How do you make a variable available to programs started from the shell?
5Which file do you change to give one user an alias at every login?

~/.bashrc of that user.

6In vim, how do you save the file and quit?

Press Esc, then type :wq and Enter.

7How do you search the command history for an old command?

Press Ctrl+R and type part of the command.

8How do you see if ls is an alias, a built-in or a program?

Oefenen

Alle commando's in dit hoofdstuk