As before, try to first answer ALL questions from memory.
Only after that should you use internet, your notes, slides, or console to find the answer.
SERIES 1
- Give commands to display:
a) ip address of the machine
ip a
b) ip address of the default gateway
ip route | grep default
c) ip address of DNS server(s)
cat /etc/resolv.conf or resolvectl status
- What is the contents of ~/.ssh/id_rsa file?
The content is a string of characters representing the private key of a key-pair and which is specific to an specific public key.
- What is the contents of ~/.ssh/authorized_keys file?
These are the public keys that are allowed in a server to permit the connection from a client machine that has the specific private key for secure connection (ssh).
- Give command to generata keys required for key-based SSH authentication:
ssh-keygen -t ed25519 -f mykey
- Give command to setup key-based SSH authentication between current account and account jerry on machine 1.2.3.4. :
ssh-copy-id jerry@1.2.3.4
- Recall UFW. Give a command to:
a) display whether UFW is enabled or list all currently used rules:
sudo ufw status, sudo ufw status numbered
b) display all rules numbered:
sudo ufw status numbered
c) delete rule number 3:
sudo ufw delete [rule number OR name]
d) allow incoming connections to port 6667 via TCP protocol:
sudo ufw allow 6667/tcp
e) allow incoming connections for ntp service:
sudo ufw allow ntp
As before, try to first answer ALL questions from memory.
Only after that should you use internet, your notes, slides, or console to find the answer.
SERIES 2
- Give command to create a symbolic link named ‘followme’ pointing to /etc/network directory
- ln -s /etc/network followme
- Give command to remove symbolic link ~/subdirlink
- Give 2 reasons why symbolic links are used, give 1 example of each
- it creates a shortcut in the windows sense but it can do much more.
- Backward compatibility by pointint the old standard dirs and files to new current location like in the example of folder /bin and /sbin that currently were moved to /usr/bin and /usr/sbin
- Give a command to view or change what possible programs can be used as ‘vi’ command in /usr/bin/vi.
- sudo update-alternatives –config vi
As before, try to first answer ALL questions from memory.
Only after that should you use internet, your notes, slides, or console to find the answer.
SERIES 3
Write a command using sed (and other commands) to:
- display the first 5 lines of /etc/group file where all colons : have been replaced with commas ,
- head -n5 /etc/group | sed ‘s/:/,/g’
- display the contents of /etc/hosts where any occurence of 127 is changed to word LOCAL
- sed ‘s/127/LOCAL/g’ /etc/hosts
- take the output of ‘ls -l ~/’ command and replace any occurence of rwx in it with word ALL
- ls -l ~/ | sed ‘s/rwx/ALL/g’
- display lines 5,6,7 of /etc/passwd
head /etc/passwd -n7 | tail -n3 OR sed -n ‘5,7p’ /etc/passwd
Hint: try head and tail
- Recall command ‘top’. If a computer has 8 cores and they’re all occupied at 75% what will be the value of ‘load’ displayed by command top?
- is the number of cores times decimal percentage expression: 8 X 0.75 = 6
- Explain meaning of each of the following shell operators in one sentence and give one example of use
> Write to OR overwrite to file
>> Append to file
2> send stderr code to
&> send everything to
2>&1 send stderr to ?
< redirects standard input FROM a file
| pipe is used to transfer stdout to the next command
; semicolon separator makes second command to execute regardless
&& AND operator only executes second command if first one succeeds
|| OR operator executes second command only when the first one failed
As before, try to first answer ALL questions from memory.
Only after that should you use internet, your notes, slides, or console to find the answer.
SERIES 4
- Find out how many (approximately) programs named ‘bash’ are currently running on your system.
ps -aux | grep bash
- Find out what is the program that takes most RAM or CPU
ps aux –sort=%cpu
- Create a backup of entire /etc into a file saved in /tmp directory (you pick an appropriate file name in /tmp)
tar -zvcf /tmp/etc_backup.tar.gz /etc
- Restore backup of /etc/ directory previously created into /tmp directory (do NOT override originals is /etc)
tar -zxvf /tmp/etc_backup.tar.gz -C /tmp/etc2
- Run cp /etc/passwd /tmp/users.txt. Then use gzip to compress /tmp/users.txt
cp /etc/passwd /tmp/users.txt
gzip /tmp/users.txt
Verify that compressing it worked and users.txt.gz exists.
ls -l users.txt.gz
- Uncompress the file you compressed in previous step.
gunzip /tmp/users.txt.gz /tmp
ls -l users.txt
- [multiple answers] Start Firefox.
- How can you find out what is the PID of that process? Do it.
ps aux | grep firefox
- How can you terminate the process? Do it.
kill PID
- How would you terminate the process forcefully?
kill -9 PID
- How would you terminate all processes started from executable named ‘less’ ?
killall less OR pkill less
- Write a command that will check if group named ‘admin’ exists in /etc/group. If the group exists – your command should display message “found it” and if not “no such group”. Make sure you don’t display anything else. Repeat the same command looking for group named ‘adm’.
if cut -d: -f1 /etc/group | grep -q “^adm$”; then echo “found it”; else echo “is not there”; fi
As before, try to first answer ALL questions from memory.
Only after that should you use internet, your notes, slides, or console to find the answer.
TASK 1.
Create script called taller.sh.
Script requires exactly 4 parameters.
parameters 1 and 3 are names (any text).
parameters 2 and 4 are heights (numbers).
Script will compare heights and then say
which person is taller.
Note: do NOT worry about the case when they are the same height.
Example console session:
./taller.sh JJ JJ JJ
Error: 4 parameters required
Usage exampe: taller.sh Jenny 165 Timmy 182
./taller.sh Jenny JJ Timmy 156
Error: JJ is not a number
./taller.sh Eva 176 Martin 165
Eva is 176 which is taller than Martin who is 165
./taller.sh Mimi 166 Maxim 172
Maxim is 172 which is taller than Mimi who is 166
#! /bin/bash
echo "Provide name1 heught1 name 2 height2"
# VALIDATE 4 PARAMETERS
if [ "$#" -ne "4" ]; then
echo "error: you must provide four parameters: name1, height1, name2, height2"
exit
fi
# VALIDATE 2nd and 4th params are valid integers
echo "$2$4" | grep -E "^[0-9]+$" &>/dev/null
if [ "$?" -ne "0" ]; then
echo "Error: Heights must be a positive valid integer in cms"
exit
fi
# PART 3 DOIT
if [ "$2" -eq "$4" ]; then
echo "Both have the same height"
else
if [ "$2" -gt "$4" ]; then
echo "$1 is taller than $3"
else
echo "$3 is taller than $1"
fi
fi
TASK 2.
Create script findline.sh.
Script requires two parameters on command line.
Parameter 1 must be a valid path to a regular file you can read.
Parameter 2 is any text.
The script will use grep to find out how many lines of file (Parameter 1) contain the word passed as Parameter 2.
Example console session:
./findline.sh
Error: invalid number of parameters.
Usage: findline.sh pathtofile word
./findline.sh blah.txt jerry
Error: file blah.txt is not a regular file.
./findline.sh /etc/passwd stud
Word stud found in 1 line(s) in /etc/passwd
#!/bin/bash
# 1. Check if exactly two parameters are provided
if [ "$#" -ne 2 ]; then
echo "Error: invalid number of parameters."
echo "Usage: findline.sh pathtofile word"
exit 1
fi
# Assign parameters to descriptive variables
FILE_PATH="$1"
SEARCH_WORD="$2"
# 2. Check if the file is a regular file (-f) and is readable (-r)
if [ ! -f "$FILE_PATH" ] || [ ! -r "$FILE_PATH" ]; then
echo "Error: file $FILE_PATH is not a regular file."
exit 1
fi
# 3. Use grep to count the matching lines
# -c counts matching lines, -w ensures it matches the exact word
LINE_COUNT=$(grep -c "$SEARCH_WORD" "$FILE_PATH")
# 4. Output the result
echo "Word $SEARCH_WORD found in $LINE_COUNT line(s) in $FILE_PATH"
TASK 3
Create executable script santafriend.sh.
Script takes multiple names of people as parameters.
You do NOT have to verify the number of parameters passed or their values.
For every name on the list of parameters say “Hello …” with the name.
However, if the name is “Santa” do NOT say “Hello…” but instead say “WOW, you, really?”
Example console session:
./santafriend.sh Jerry Terry Santa Marry
Hello Jerry
Hello Terry
Wow, you, really?
Hello Marry
#! /bin/bash
# set -x
# 1 3 5 7 9
# VALIDATION
# if [ "$#" -eq 1 ]; then SAME RESULT
if [ "$#" -lt 1 ]; then
echo "Error: you must enter at least one parameter (name or names)"
exit
fi
for NAME in "$@"; do
if [ "$NAME" = "Santa" ] || [ "$NAME" = "santa" ]; then
echo "Wow, you, really?"
else
echo "Hello $NAME"
fi
done
TASK 4
Create executable findlucy.sh
Interactively ask user for a list of friends names.
Require that the list is not empty.
If the list is empty then terminate with error message.
Print each friend’s name and if you find name “Lucy” as one of the name print out a special greeting.
Example console session:
./findlucy.sh
Enter list of names on one line
Error: you must have some friends.
./findlucy.sh
Enter list of names on one line
Jerry Lucy Terry
You’re friends with Jerry
OMG, you know Lucy too!
You’re friends with Terry
Hint: to compare for empty use … = “”
#! /bin/bash
# set -x
echo "Enter list of names on one line and press Enter"
read LIST
# Alternative you can use read -p to set the variable LIST
#read -p "Enter list of names in one line and press Enter: " LIST
# VALIDATION
if [ "$LIST" == "" ]; then
echo "Error: you must enter at least one name"
exit 1
fi
for NAME in $LIST; do
if [ "$NAME" == "Lucy" ]; then
echo "OMG, you know Lucy too!?"
else
echo "Hello $NAME"
fi
done