Objective: Tell shell variables from environment variables, and see which ones a child shell inherits with
export. Customize your prompt, and make your settings and aliases permanent in.bashrcand.bash_profile. Add the EPEL repository, look inside packages withdnfandrpm, and install one by hand. Schedule jobs with cron, and read their output by mail.
Every time you log in, bash sets up an environment for you: your username, your home directory, where to look for commands, what your prompt looks like. In Lab 2 you used $PATH, in Lab 3 $USER, and in Lab 7 the SSH agent gave you $SSH_AUTH_SOCK. In Lab 14 you made your own variable, DEFAULT_ZONE, and found it was gone when you logged out. In Lab 18 you saw that every process has a parent. This lab ties those together: where variables come from, which processes get them, and how to make them last.
The same ideas explain cron, the service that runs jobs on a schedule. A cron job doesn't log in, so it doesn't get the environment you are used to. In Lab 19 you made scripts safe to run with nobody watching. Cron is how they run with nobody watching.
Note: This lab is done on your development server, NOT your production server. Connect with SSH from Terminal or PuTTY and log in as your own user.
Some steps give you every command. Others give you a Goal and ask you to work out the commands yourself. If you get stuck, open the hint. Once you have an answer, or if you are really stuck, open the solution and compare it with your own.
If you get stuck, check the Troubleshooting section at the end of this lab.
The examples in this lab use the username angus and the UTM interface enp0s1. On VirtualBox your interface is usually enp0s3.
A variable is a name that holds a value. Bash keeps two kinds:
| Kind | Who can see it | List them with |
|---|---|---|
| Shell variable | Only the shell that made it. | set |
| Environment variable | The shell and every program it starts, including a new shell. | env, printenv |
An environment variable is a shell variable that has been exported. So set lists all the variables in your shell, exported or not. It also lists bash functions, which you can ignore for now. env lists only the exported ones.
Compare the two lists. set is long, so send it to less, from Lab 2. Press Space to page down and q to quit:
env
set | less
env | wc -l
set | wc -l
| Command | What it does |
|---|---|
env |
Prints the environment variables, one NAME=value per line. |
set |
Prints every shell variable, then the functions. |
wc -l |
Counts the lines, from Lab 3. set prints many more. |


Note: These screenshots were taken on an older server, so the hostname and addresses are different from yours. The layout is the same.
Some variables you will use all the time:
echo $USER
echo $HOME
echo $HOSTNAME
echo $SHELL
echo $PATH
printenv HOME
| Variable | Holds |
|---|---|
USER |
Your username. |
HOME |
Your home directory. ~ is a short way to write it. |
HOSTNAME |
The server's name. |
SHELL |
Your login shell, from your line in /etc/passwd (Lab 3). |
PATH |
The directories bash searches for commands, separated by : (Lab 2). |
printenv NAME prints one environment variable, without the $. If the variable isn't in the environment, it prints nothing.
Make a variable, print it, and look for it in both lists:
env | grep -i my_new_variable
my_new_variable="my demo variable"
echo $my_new_variable
env | grep -i my_new_variable
set | grep -i my_new_variable

The variable is in set, but not in env. It is a shell variable only.
Common Pitfall: No spaces around the
=.my_new_variable = "x"makes bash run a command calledmy_new_variable, and you getcommand not found. Quotes keep the spaces inside the value, as in Lab 2.
By habit, environment variables set by the system have UPPERCASE names. Give your own shell variables lowercase names, so you never replace one of the system's by mistake.
Goal: For each of these variables, find out if it is an environment variable or only a shell variable: USER, HOME, PS1, BASH_VERSION, SHLVL.
env. A shell-only variable shows up in set but not env.printenv NAME prints nothing for a variable that isn't in the environment.^ anchor from Lab 3, such as grep "^PS1=", so you only match the start of a line.printenv USER
printenv HOME
printenv PS1
printenv BASH_VERSION
printenv SHLVL
set | grep "^PS1=\|^BASH_VERSION="
| Variable | Kind |
|---|---|
USER |
Environment |
HOME |
Environment |
PS1 |
Shell only |
BASH_VERSION |
Shell only |
SHLVL |
Environment |
PS1 is your prompt. You change it in Part 3. SHLVL counts how deep you are in shells. You use it in Part 2.
Another way: env | grep "^PS1=" prints nothing, while set | grep "^PS1=" prints the prompt.
In Lab 18 you learned that every process has a parent, and that systemd is process 1. pstree draws the whole family as a tree:
pstree
pstree -s $$
| Command | What it does |
|---|---|
pstree |
Draws every process, starting from systemd. |
$$ |
The PID of the shell you are typing in, from Lab 18. |
pstree -s $$ |
Draws only your shell, its parents above it, and what it is running. |
systemd───sshd───sshd───sshd───bash───pstree
Your SSH login is three sshd processes. Look at them with ps, from Lab 18:
ps -ef | grep sshd
root 812 1 0 09:02 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root 1520 812 0 10:14 ? 00:00:00 sshd: angus [priv]
angus 1524 1520 0 10:14 ? 00:00:00 sshd: angus@pts/0
angus 1563 1525 0 10:20 pts/0 00:00:00 grep --color=auto sshd
| Process | What it is |
|---|---|
sshd ... [listener] |
The service, started by systemd (PPID 1). It waits for new connections. |
sshd: angus [priv] |
Started for your connection. It runs as root, to do the few things that need root. |
sshd: angus@pts/0 |
Your session. It runs as you, and started your bash. |
Open a second SSH session and run ps -ef | grep sshd again. The listener is still one process, but there is a new [priv] and a new session for the second connection.
Typing bash starts a new shell inside the one you have. The new shell is a child, and your login shell is its parent. Compare the PID and the shell level before and after:
echo $$ $SHLVL
bash
echo $$ $SHLVL
pstree -s $$
1525 1
1602 2
systemd───sshd───sshd───sshd───bash───bash───pstree
The PID is different, so this is a different process, and SHLVL went up from 1 to 2.

Note: The screenshot was taken on an older server, so some of the services in its tree are different. Look at the
sshd───sshd───sshd───bash───bash───pstreeline.
Is your variable from Part 1 in the child shell? Check, then go back to the parent with exit:
echo $my_new_variable
set | grep -i my_new_variable
exit
echo $SHLVL
echo $my_new_variable
The child has nothing. The variable lives only in the parent shell, and the parent still has it. exit in a child shell takes you back to its parent. exit at SHLVL 1 logs you out.
export turns a shell variable into an environment variable. Children get a copy of every environment variable:
export my_new_variable
env | grep -i my_new_variable
bash
echo $my_new_variable
env | grep -i my_new_variable
This time the child has it. Start a grandchild, and check again:
bash
echo $SHLVL
echo $my_new_variable
pstree -s $$
SHLVL is 3, and the variable is still there. An exported variable is passed down to every level. Go back to your login shell. SHLVL tells you how many times to type exit:
exit
exit
echo $SHLVL
You can make and export a variable in one line: export my_new_variable="my demo variable". To remove a variable, use unset my_new_variable.
Goal: In a child shell, change the value of my_new_variable, and export a new variable called from_child. Go back to the parent. What is the value of my_new_variable there? Does from_child exist?
bash. Set both variables with export NAME="value".exit, check both with echo and set | grep.bash
export my_new_variable="changed in the child"
export from_child="hello from the child"
echo $my_new_variable
exit
echo $my_new_variable
echo $from_child
set | grep from_child
The parent still says my demo variable, and from_child doesn't exist. A child gets a copy of the environment when it starts. Changes it makes stay in the child, and are lost when it exits. Variables only go down, from parent to child, never up.
This is also why a script, which runs in a child shell, can't change the variables of the shell you ran it from.
Going further: You can give one command its own environment, without changing your shell. Put the variable in front of the command. TZ is the time zone from Lab 18:
date
TZ=America/New_York date
date
Log out completely, then log in again with SSH:
exit
After you log in, check:
echo $my_new_variable
It is gone. Every variable you make by hand only lasts until you log out, as you found with DEFAULT_ZONE in Lab 14. Part 4 shows you how to make settings last.
Your prompt is the shell variable PS1. Look at it, then change directory and watch the prompt change:
echo $PS1
cd /var/log
cd
[\u@\h \W]\$
The backslash codes are filled in each time bash shows the prompt:
| Code | Shows |
|---|---|
\u |
Your username. |
\h |
The hostname, up to the first dot. |
\W |
The last part of the current directory, such as log. |
\w |
The full path of the current directory. Your home shows as ~. |
\d |
The date, such as Thu Nov 19. |
\t |
The time, 24-hour, such as 10:42:07. |
\$ |
$ for a normal user, # for root. |
For the full list, run man bash and search for PROMPTING with /PROMPTING, as in Lab 4.
Save your prompt in a variable first, so you can put it back:
old_ps1="$PS1"
Change \W to \w to see the full path:
PS1='[\u@\h \w]\$ '
cd /var/log
cd /etc/yum.repos.d
cd
[angus@itfdn-dev01 /var/log]$
[angus@itfdn-dev01 /etc/yum.repos.d]$
[angus@itfdn-dev01 ~]$
Use single quotes, so bash stores the codes as they are and fills them in later. Keep the space after \$, so your typing doesn't touch the prompt.
Add the date with \d:
PS1='[\u@\h \w \d]\$ '
Terminals change color when they see a special escape code. \e[0;36m means "cyan from here on", and \e[0m means "back to normal":
PS1='\[\e[0;36m\][\u@\h \w]\$ \[\e[0m\]'
PS1='\[\e[0;31m\][\u@\h \w]\$ \[\e[0m\]'
| Part | What it does |
|---|---|
\e[0;36m |
Switches the text to cyan. \e is the Escape character. |
\e[0m |
Switches back to normal, so the commands you type aren't colored. |
\[ and \] |
Wrap each color code. They tell bash the code takes no space on the screen. Without them, long commands wrap onto the same line and overwrite the prompt. |
| Color | Code | Color | Code |
|---|---|---|---|
| Black | 0;30 |
Blue | 0;34 |
| Red | 0;31 |
Purple | 0;35 |
| Green | 0;32 |
Cyan | 0;36 |
| Yellow | 0;33 |
White | 0;37 |
Change 0; to 1; for bold, such as 1;32 for bold green.
Put your prompt back:
PS1="$old_ps1"
Question: In Task 1 you found that
PS1is a shell variable, not an environment variable. Change your prompt, then typebash. What does the child shell's prompt look like? Why?
The child shell has the normal [angus@itfdn-dev01 ~]$ prompt. PS1 isn't exported, so the child never gets your change. It sets up its own prompt from the startup files you read in Part 4. Type exit to go back.
Goal: Make a prompt that shows your username and hostname in green, the time, and the full path of the current directory, and ends with $. Test it in a few directories. Leave it set: you make it permanent in Task 4.
\[ and \].PS1='\[\e[0;32m\][\u@\h \t \w]\$ \[\e[0m\]'
cd /var/log
cd
[angus@itfdn-dev01 10:48:12 /var/log]$
[angus@itfdn-dev01 10:48:15 ~]$
Another way: Color only part of the prompt. This makes user@host green and the directory blue:
PS1='[\[\e[0;32m\]\u@\h\[\e[0m\] \[\e[0;34m\]\w\[\e[0m\]]\$ '
Bash reads startup files when it starts. Which files it reads depends on how it was started:
| How the shell started | Kind | Reads |
|---|---|---|
| You log in with SSH, or on the console | Login shell | /etc/profile, then ~/.bash_profile |
You type bash, or open a new tab in some terminals |
Non-login shell | ~/.bashrc |
| A cron job runs (Part 8) | Not interactive | Neither |
Files in /etc are for every user, and need sudo to change. The files in your home directory are yours. They start with a ., so you need ls -a (Lab 2) to see them. Back them up before you change them, as you did with nginx.conf in Lab 13:
cd
ls -a
cp ~/.bashrc ~/.bashrc.orig
cp ~/.bash_profile ~/.bash_profile.orig
cat ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
if [ -f ~/.bashrc ] checks that the file exists. . ~/.bashrc runs it, in this shell. So a login shell reads .bash_profile, and .bash_profile reads .bashrc. Now look at .bashrc:
cat ~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH
...
.bashrc reads /etc/bashrc, which sets your normal prompt, then adds ~/.local/bin and ~/bin to your PATH, and exports it.
| Put it in | When |
|---|---|
~/.bashrc |
Settings every interactive shell needs: variables, PS1, aliases. Nothing that prints. |
~/.bash_profile |
Things to do once, when you log in, such as a welcome message. |
Add a line to each file that prints a message. Quote the whole line in single quotes, so echo writes it to the file as it is:
echo 'echo "Hello from .bash_profile"' >> ~/.bash_profile
echo 'echo "Hello from .bashrc"' >> ~/.bashrc
tail -n 1 ~/.bash_profile ~/.bashrc
Common Pitfall: Use
>>, never>. As you learned in Lab 5,>empties the file first, and your startup file is gone. If that happens, copy back your.origbackup, or the default from/etc/skel/.
Start a child shell, then exit it:
bash
exit
Only Hello from .bashrc is printed. Now open a new SSH session to your development server, and leave the first one open.
Question: In the new SSH session, which messages are printed, and in which order? Why that order?
Both, with .bashrc first:
Hello from .bashrc
Hello from .bash_profile
An SSH login is a login shell, so it reads .bash_profile. Near its top, .bash_profile runs .bashrc, which prints its message. Your own line is at the bottom of .bash_profile, so it prints last.
Close the new session. Take the test lines out by putting back your backups:
cp ~/.bashrc.orig ~/.bashrc
cp ~/.bash_profile.orig ~/.bash_profile
Warning: Don't leave anything in
.bashrcthat prints.scpandsftpfrom Lab 8, and GUI tools such as WinSCP and Cyberduck, can start a shell that reads.bashrctoo. They expect to talk only to each other, and extra text breaks the transfer. Messages go in.bash_profile.
Add a variable to .bashrc, and set your editor to Vim. Many programs, such as crontab in Part 8, check EDITOR to choose which editor to open:
echo 'export my_new_variable="my demo variable"' >> ~/.bashrc
echo 'export EDITOR=vim' >> ~/.bashrc
tail -n 2 ~/.bashrc
echo $EDITOR
echo $EDITOR prints nothing. Changing a file doesn't change the shell you are in. Tell bash to read the file again with source:
source ~/.bashrc
echo $EDITOR
echo $my_new_variable
source ~/.bashrc and . ~/.bashrc do the same thing: they run the file in this shell, the way .bash_profile runs .bashrc. Running it with bash ~/.bashrc would run it in a child shell, and, as you saw in Task 2, the child can't change your shell.
Log out and back in, and check echo $my_new_variable again. This time it is there.
The single quotes matter. Compare:
echo 'Home is $HOME'
echo "Home is $HOME"
Inside single quotes bash changes nothing, so $HOME is written as it is. Inside double quotes bash fills in the variable first. To put a line with a $ into a startup file, use single quotes, so the variable is filled in each time the file runs.
Goal: Add your prompt from Task 3 to the end of ~/.bashrc. Load it into your shell, then check that a new SSH session and a child shell both use it.
vim (Lab 4) instead of echo. G jumps to the last line, and o opens a new line below it.source.vim ~/.bashrc
Add this line at the bottom, then save with :wq:
PS1='\[\e[0;32m\][\u@\h \t \w]\$ \[\e[0m\]'
source ~/.bashrc
bash
exit
Open a new SSH session: it has the green prompt too. PS1 still isn't exported, but every interactive shell reads .bashrc, so every shell sets it again. That is why settings go in .bashrc, not only in .bash_profile.
Going further: Run your scripts from Lab 11 from any directory, without typing the path. Add the scripts directory to the end of your PATH:
echo 'export PATH="$PATH:$HOME/scripts/shell"' >> ~/.bashrc
source ~/.bashrc
cd /tmp
first.sh
which first.sh
cd
An alias is a short name for a longer command. RHEL already gives you some:
alias
type ll
...
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
...
ll is aliased to `ls -l --color=auto'
Your list may be longer. type tells you what a name really is: an alias, a shell builtin, or a program on your PATH.
Make your own. h doesn't exist yet, so the first h fails:
h
alias h='history'
h
You can also replace an alias. This one changes ll:
alias ll='ls -lart --color=auto'
ll
| Option | What it does |
|---|---|
-l |
Long listing, from Lab 2. |
-a |
Shows hidden files, the ones starting with .. |
-r |
Reverses the sort order. |
-t |
Sorts by time. With -r, the newest files are last, just above your prompt. |
An alias can hold a whole pipe. This one shows how much space each directory under your current directory uses, smallest first:
alias du1='du -h -d 1 | sort -h'
cd
du1
| Part | What it does |
|---|---|
du -h -d 1 |
Disk usage, in human sizes such as 4.0K and 12M, one level deep. |
sort -h |
Sorts human sizes, so 12M comes after 900K. sort -n gets this wrong, because it only reads the number. |
Now start a child shell, and try your new aliases there:
bash
h
du1
exit
command not found. Aliases are never exported. They only live in the shell where you made them. To keep them, put them in .bashrc, which every interactive shell reads. To remove an alias from your shell, use unalias, such as unalias h.
Note: Aliases are for typing at the prompt. Scripts and cron jobs don't see them, so always write the full command in a script.
Goal: Add h, du1 and at least one alias of your own to ~/.bashrc. Load the file, then prove the aliases work in a child shell.
.bashrc with vim, or use echo '...' >> ~/.bashrc with the whole line in single quotes.source to load it, as in Part 4.gits for git status (Lab 10), or ports for sudo ss -tlnp (Lab 12).vim ~/.bashrc
Add at the bottom:
alias h='history'
alias du1='du -h -d 1 | sort -h'
alias gits='git status'
source ~/.bashrc
bash
h
gits
type du1
exit
gits only works inside a Git repository, such as ~/git/gitlab/itfdn-140-inclass-$USER. Anywhere else, Git says not a git repository.
dnf installs packages from repositories: servers that hold many packages, with a list of what is in them. You have used dnf install since Lab 3. Look at the repositories your server uses now:
dnf repolist
Not root, Subscription Management repositories not updated
repo id repo name
rhel-9-for-x86_64-appstream-rpms Red Hat Enterprise Linux 9 for x86_64 - AppStream (RPMs)
rhel-9-for-x86_64-baseos-rpms Red Hat Enterprise Linux 9 for x86_64 - BaseOS (RPMs)
On a Mac with Apple silicon you see aarch64 instead of x86_64. The first line only means you ran dnf without sudo. It is safe to ignore.
| Repository | What it holds |
|---|---|
| BaseOS | The core of RHEL. Red Hat supports it. |
| AppStream | Applications and tools, such as nginx and git. Red Hat supports it. |
| CodeReady Builder (CRB) | Extra tools for developers, from Red Hat but not supported. Off by default. |
| EPEL | Extra Packages for Enterprise Linux, built by the Fedora Project volunteers. Not from Red Hat. Many EPEL packages need CRB. |
Red Hat's repositories come with your subscription, from Lab 1, so you turn them on with subscription-manager. Turn on CodeReady Builder. It can take a minute:
sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
Repository 'codeready-builder-for-rhel-9-x86_64-rpms' is enabled for this system.
$(arch) is command substitution, from Lab 7. arch prints x86_64 or aarch64, like uname -m in Lab 0, so the same command works on every laptop.
EPEL isn't Red Hat's, so you install its release package straight from Fedora. That package doesn't hold any programs. It only adds the EPEL repository files to your server:
sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
dnf repolist
ls /etc/yum.repos.d/
dnf repolist now also lists codeready-builder-for-rhel-9-..., epel, and maybe epel-cisco-openh264. Each repository is described in a .repo file in /etc/yum.repos.d/. Red Hat's are in redhat.repo, which subscription-manager writes for you. Look at the EPEL file:
grep "^\[\|^enabled" /etc/yum.repos.d/epel.repo
[epel]
enabled=1
[epel-debuginfo]
enabled=0
[epel-source]
enabled=0
Each [name] starts a repository, and enabled=1 means dnf uses it. \[ matches a real [, because a plain [ means something special to grep, as in [Tt] in Lab 3.
Note: You may see
sudo dnf install epel-releasein guides on the web. That works on Rocky Linux, AlmaLinux and CentOS Stream, which carry the package themselves. RHEL doesn't, so on RHEL you install it from Fedora's URL.
cowsay is a small, silly program from EPEL. It is a good first test, because it isn't in any Red Hat repository:
sudo dnf install -y cowsay
cowsay hello $USER
cowsay -l
cowsay -f bud-frogs "Hello $USER"
-l lists the pictures, and -f picks one.
Make it greet you when you log in. A greeting prints, so it goes in .bash_profile, not .bashrc:
echo 'cowsay -f small "Hello $USER"' >> ~/.bash_profile
tail -n 1 ~/.bash_profile
Open a new SSH session: the cow greets you. Type bash in it: no cow, because a child shell doesn't read .bash_profile. Type exit.
Goal: Change your login greeting to a different picture and a message of your own. Change the line, don't add a second one. Test it in a new SSH session.
cowsay -l lists the pictures. Try a few at the prompt first.~/.bash_profile in vim, and edit the last line.cowsay -l
cowsay -f tux "Welcome back, $USER"
vim ~/.bash_profile
Change the last line to, for example:
cowsay -f tux "Welcome back, $USER. Today is $(date +%A)."
Then open a new SSH session to test it.
Going further: fortune prints a random saying. Install it from EPEL, and send it through a pipe to cowsay:
sudo dnf install -y fortune-mod
fortune | cowsay
RHEL packages are RPM files. Two tools work with them:
| Tool | Works with | Good for |
|---|---|---|
dnf |
Repositories | Finding, installing, updating and removing packages. It installs every other package a package needs, its dependencies, for you. |
rpm |
The packages on your server, and .rpm files |
Asking questions about what is installed. It never downloads anything, and never installs dependencies. |
Ask both tools about cowsay:
dnf info cowsay
rpm -qi cowsay
rpm -ql cowsay | head
rpm -qf /usr/bin/cowsay
dnf info shows the version, a summary and From repo : epel, the repository it came from. rpm takes -q (query) plus a letter for what to ask:
| Option | Asks |
|---|---|
-qa |
All installed packages. You used it in Lab 16. |
-qi |
Information about an installed package. |
-ql |
The list of files it installed. |
-qc |
Only its configuration files. You used it in Lab 18. |
-qf FILE |
Which package installed this file. |
-qR |
What the package requires: its dependencies. |
-p FILE |
Ask about a .rpm package file instead of an installed package, such as -qlp. |
rpm -qf answers "where did this file come from?" for files already on your server. In Lab 17 you used dnf provides to ask the same question about packages you don't have yet. Now that CodeReady Builder is on, the same search finds matches in more repositories:
dnf provides "*/locate"

Goal: Find out which packages installed pstree, from Part 2, and crontab, which you use in Part 8. Use rpm, and give it each command's full path.
which (Lab 2) prints a command's full path.$( ) from Lab 7 puts one command's output into another command.rpm -qf $(which pstree)
rpm -qf $(which crontab)
psmisc-23.4-3.el9.x86_64
cronie-1.5.7-12.el9.x86_64
Your version numbers may be different. pstree comes from psmisc, and crontab from cronie, the cron package.
Another way: dnf provides "*/bin/pstree", as in Lab 17.
dnf download saves a package file without installing it. Then rpm can look inside the file, and install it. You are going to install bmon, a tool from EPEL that shows how much data goes in and out of each network interface. You use it again in Lab 21:
mkdir -p ~/inclass/lab-20/rpms
cd ~/inclass/lab-20/rpms
dnf download bmon
ls
rpm -qlp bmon-*.rpm
sudo rpm -ivh bmon-*.rpm
| Command | What it does |
|---|---|
dnf download bmon |
Saves the bmon .rpm file in the current directory. No sudo, because nothing is installed. |
rpm -qlp bmon-*.rpm |
Lists the files inside the package file (-p). |
rpm -ivh |
Installs the file, verbose, with hash marks to show progress. Needs sudo. |
The install fails:
error: Failed dependencies:
libconfuse.so.2()(64bit) is needed by bmon-4.0-...
bmon needs a library that isn't installed, and rpm doesn't go and get it. That is the job dnf normally does for you.
Goal: Read the error, find the package that holds the missing library, download it, and install it with rpm. Then install bmon with rpm, and run it.
rpm -qpR bmon-*.rpm lists everything bmon needs.dnf download libconfuse
ls
sudo rpm -ivh libconfuse-*.rpm
sudo rpm -ivh bmon-*.rpm
rpm -q bmon libconfuse
Now run bmon. Give it your interface with -p:
bmon -p enp0s1
Use the arrow keys to move, d for detailed counters and i for more information. Press q to quit. man bmon lists the other options.
Another way: sudo dnf install -y ./bmon-*.rpm installs a downloaded file with dnf, which finds and installs the missing library for you. The ./ tells dnf it is a file, not a package name. This is what you would normally do. rpm -ivh is worth knowing for servers that can't reach a repository.
The crond service checks every minute for jobs to run. Each user can have a crontab, a list of jobs with their times. Check that the service is running, as in Lab 18:
systemctl status crond
There is also a system-wide crontab. Its comments are the best reminder of the layout:
cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
Five time fields, then the command. * means "every". Some examples:
| Line | Runs |
|---|---|
* * * * * cmd |
Every minute. |
*/5 * * * * cmd |
Every 5 minutes. |
10 * * * * cmd |
At minute 10 of every hour. |
10,40 * * * * cmd |
At minutes 10 and 40 of every hour. |
30 2 * * * cmd |
Every day at 2:30 in the morning. |
0 9 * * 1-5 cmd |
At 9:00, Monday to Friday. |
Times are in the server's local time zone, which you set in Lab 18. Websites such as crontab.guru explain any line in plain English.
Common Pitfall:
/etc/crontab, and the files in/etc/cron.d/, have a user-name field before the command, because they can run jobs as anyone. Your own crontab does not. If you copy* * * * * root ls -lainto it, cron tries to run a command calledroot, and fails every minute.
Your crontab starts empty. crontab -e opens it in your EDITOR, which is Vim since Part 4:
mkdir -p ~/inclass/lab-20
crontab -l
crontab -e
crontab -l says no crontab for angus. In Vim, press i and add this line, then save with :wq:
* * * * * date >> $HOME/inclass/lab-20/cron.log
crontab checks the line and prints crontab: installing new crontab. Wait two minutes, then check the log file, and cron's own log:
crontab -l
cat ~/inclass/lab-20/cron.log
sudo tail -n 3 /var/log/cron
cron.log gets a new line with the date and time every minute. Cron's log has a CMD line for each run:
Nov 19 10:42:01 itfdn-dev01 CROND[2345]: (angus) CMD (date >> $HOME/inclass/lab-20/cron.log)
Cron runs jobs at the start of the minute. Your crontab is stored in /var/spool/cron/angus, but always change it with crontab -e. It checks your lines, and tells crond about the change.
Warning:
crontab -rdeletes your whole crontab, with no question. On the keyboard r is next to e. Check withcrontab -lbefore you press Enter.
Goal: Add a second job that saves a cron job's environment, every minute, to ~/inclass/lab-20/cron-env.txt. After it runs, compare that environment with your own. Look at PATH and SHELL. Is my_new_variable there? Is EDITOR? Then turn both jobs off.
env prints the environment. Send it to the file with >, so the file only holds the latest run.# at the start of its line, the way comments work in config files.Run crontab -e and add:
* * * * * env > $HOME/inclass/lab-20/cron-env.txt
Wait a minute, then compare:
cat ~/inclass/lab-20/cron-env.txt
grep "^PATH=\|^SHELL=" ~/inclass/lab-20/cron-env.txt
echo $PATH
echo $SHELL
grep "my_new_variable\|EDITOR" ~/inclass/lab-20/cron-env.txt
SHELL=/bin/sh
PATH=/usr/bin:/bin
The cron environment is only a few lines. PATH is short, SHELL is /bin/sh, and my_new_variable and EDITOR are missing. A cron job isn't a login shell and isn't interactive, so it reads no startup files. Your variables, your aliases and your extra PATH aren't there.
That is why you give full paths in cron jobs, such as /home/angus/scripts/shell/first.sh, or set PATH= on a line of its own at the top of your crontab, the way /etc/crontab does.
Turn both jobs off. Run crontab -e and put # at the start of both lines:
#* * * * * date >> $HOME/inclass/lab-20/cron.log
#* * * * * env > $HOME/inclass/lab-20/cron-env.txt
If a job prints anything, and you didn't redirect it, cron mails the output to the crontab's owner. Add a job that checks your disk space every two minutes, with no redirect:
crontab -e
*/2 * * * * df -h /
df -h / shows how full the disk is, in human sizes. Wait two minutes, then look for the mail in cron's log:
sudo grep MTA /var/log/cron | tail -n 2
Nov 19 10:52:01 itfdn-dev01 CROND[2410]: (CRON) info (No MTA installed, discarding output)
There is no MTA, mail transfer agent, on your server, so the output was thrown away. A Minimal Install doesn't include one. Install Postfix, Red Hat's default mail server, and s-nail, a command-line mail reader. Then start Postfix, restart crond so it starts sending mail, and check that Postfix is listening on port 25, the mail port:
sudo dnf install -y postfix s-nail
sudo systemctl enable --now postfix
sudo systemctl restart crond
sudo ss -tlnp | grep ":25 "
LISTEN 0 100 127.0.0.1:25 0.0.0.0:* users:(("master",pid=3021,fd=13))
LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=3021,fd=14))
Postfix listens on localhost only, so it delivers mail between users on this server and nothing reaches it from outside. You don't need to open the firewall (Lab 14). enable --now enables and starts it in one command, as in Lab 14.
Wait two minutes, then read your mail:
mailx

Note: The screenshot shows an older mail reader, so its first line is different. The list of messages from the Cron Daemon looks the same.
At the & or ? prompt |
What it does |
|---|---|
| Enter, or a number | Reads the next message, or that message. |
h |
Lists the messages again. |
d 1 |
Deletes message 1. d * deletes them all. |
q |
Quits, and saves your changes. |
x |
Quits, and throws your changes away. |
Your mail is kept in /var/spool/mail/angus. Mail is a good way to hear from cron jobs, but every two minutes is too often. Turn the job off: run crontab -e and put # at the start of its line.
Goal: Give root a crontab with one job that runs free -h every two minutes. Read root's mail to see the output. Then turn the job off, and check that crontab -l shows all your own jobs turned off too.
sudo crontab -e opens root's crontab, not yours. sudo crontab -l lists it.mailx -f FILE opens a mailbox file. Root's mail is in /var/spool/mail/root, which only root can read.sudo crontab -l
sudo crontab -e
Add:
*/2 * * * * free -h
Wait two minutes, then read root's mail:
sudo mailx -f /var/spool/mail/root
Turn the job off with sudo crontab -e and a #. Then check both crontabs:
sudo crontab -l
crontab -l
Every job line in both should start with #.
Going further: Packages add their own jobs to /etc/cron.d/, and scripts to /etc/cron.hourly/ and /etc/cron.daily/. Look at cat /etc/cron.d/0hourly. Its line has the root user-name field, because it is a system crontab. Many newer services use systemd timers instead of cron: see them with systemctl list-timers.
As in Labs 14 to 18, keep your own notes in your GitLab repository. Start the file:
cd ~/git/gitlab/itfdn-140-inclass-$USER
git pull
echo "# Shell environment, packages and cron
## Variables
\`set\` shows all shell variables, \`env\` only the exported ones. \`export NAME=value\`. Children get a copy; nothing goes back up.
" >> environment-tips.md
cat environment-tips.md
Goal: Add at least three more tips or commands to environment-tips.md, for example about .bashrc and .bash_profile, rpm -qf, or crontab fields. Add a link to the file at the end of README.md. Commit both files in one commit, and push. In GitLab, click the link in your README and check that it opens your notes.
[text to show](file-name).git status and git diff before you commit.vim environment-tips.md
For example:
## Startup files
Login shell: `.bash_profile`, which runs `.bashrc`. `bash`: only `.bashrc`. Load changes with `source ~/.bashrc`.
Nothing that prints goes in `.bashrc`: it breaks scp and sftp.
## Packages
`dnf info pkg`, `rpm -qi pkg`, `rpm -ql pkg`, `rpm -qf /path/file`. `-p` asks about a `.rpm` file.
`rpm -ivh` doesn't install dependencies; `dnf install ./file.rpm` does.
## Cron
`crontab -e`, `crontab -l`. minute hour day-of-month month day-of-week command.
No startup files: use full paths. Output is mailed; check `/var/log/cron`.
Add the link, then check, commit and push:
echo "" >> README.md
echo "[Shell environment, packages and cron](environment-tips.md)" >> README.md
git status
git diff
git add README.md environment-tips.md
git commit -m "docs: add environment tips and link from README"
git push
git log --oneline
You have now:
set, env and printenv.pstree, $$ and SHLVL, and passed variables down to them with export.PS1, including colors..bashrc, and added a login greeting to .bash_profile.dnf and rpm about packages.rpm.crontab, found out what a cron job's environment holds, and read cron's output with mailx.Discuss these with others in class:
PS1 go in .bashrc and not only in .bash_profile, while a login greeting goes in .bash_profile?dnf installs dependencies for you, and rpm doesn't. When might you still use rpm -ivh?command not found. What is the most likely reason, and what are two ways to fix it?Q. pstree: command not found.
A. The psmisc package isn't installed. Run sudo dnf install -y psmisc.
Q. I get command not found when I make a variable.
A. There is a space around the =. Write name="value", with no spaces.
Q. My variable or alias is gone after I log in again.
A. Variables and aliases made at the prompt only last until you log out. Add them to ~/.bashrc, then run source ~/.bashrc.
Q. I changed .bashrc, but nothing happened.
A. A file change doesn't reach a shell that is already running. Run source ~/.bashrc, or log in again.
Q. My prompt is messed up when I type a long command, or use the Up arrow.
A. A color code isn't wrapped in \[ and \]. Check every \e[...m in PS1.
Q. I emptied or broke my .bashrc or .bash_profile.
A. Copy back your backup, such as cp ~/.bashrc.orig ~/.bashrc, or the default from /etc/skel/, such as cp /etc/skel/.bashrc ~/. If the file stops you from logging in normally, run ssh -t dev01 bash --noprofile --norc from your laptop, which starts a shell without reading either file. In PuTTY, use the console.
Q. scp, sftp, WinSCP or Cyberduck fails with "Received message too long" or a similar error.
A. Something in your .bashrc prints text. Move greetings and echo lines to .bash_profile.
Q. The cow doesn't appear when I type bash.
A. That is right. The greeting is in .bash_profile, which only a login shell reads. Open a new SSH session.
Q. subscription-manager says "This system has no repositories available through subscriptions", or the repository can't be enabled.
A. The server isn't registered with Red Hat. Check with sudo subscription-manager status, and see Lab 1 to register it.
Q. No match for argument: cowsay or bmon.
A. EPEL isn't enabled. Check dnf repolist, and install the EPEL release package from Part 6.
Q. dnf download says "No such command: download".
A. The download command comes from the dnf-plugins-core package. Run sudo dnf install -y dnf-plugins-core.
Q. rpm says "can't create transaction lock on /var/lib/rpm/.rpm.lock (Permission denied)".
A. Installing needs root. Run it again with sudo, or sudo !! from Lab 9.
Q. rpm -ql bmon-4.0-...rpm says the package is not installed.
A. -ql asks about installed packages. Add -p to ask about a package file: rpm -qlp bmon-*.rpm.
Q. My cron job never runs.
A. Check sudo tail /var/log/cron for a CMD line, and systemctl status crond. Check the time fields with crontab.guru, and the time zone with timedatectl (Lab 18). If the line has a %, such as date +%F, write it as \%: in a crontab, % means "new line".
Q. /var/log/cron doesn't exist.
A. Your server isn't running rsyslog, which writes that file. Read cron's messages from the journal instead: sudo journalctl -u crond | tail.
Q. My cron job runs, but fails with command not found.
A. Cron's PATH is only /usr/bin:/bin, and it doesn't read your startup files. Use full paths, or set PATH= at the top of your crontab, as in Task 9. Make sure you didn't copy a user-name field such as root from /etc/crontab.
Q. I get no mail from cron, and /var/log/cron says "No MTA installed, discarding output".
A. Install Postfix and start it, then restart crond, as in Part 8: sudo systemctl status postfix, sudo systemctl restart crond.
Q. mailx says "No mail for angus", but I had messages.
A. When you quit with q, mailx may move messages you have read to ~/mbox. Read them with mailx -f ~/mbox.
Q. git push is rejected.
A. Someone, perhaps you in the GitLab web page, changed the repository. Run git pull, then git push again, as in Lab 10.
If you are still stuck, post your question, and the commands and output you are seeing, in this week's Teams channel, or reach out by email for help.