WorksheetsLinux Basics Worksheet
Total questions: 150
Worksheet time: 1hrs 15mins
What is a terminal and a shell in Linux?
A terminal is a command interpreter; a shell is a window manager
A terminal is a text interface; a shell is the command interpreter that runs inside it
A terminal is the Linux kernel; a shell is the file system
A terminal is a graphical desktop; a shell is a text editor
How can you check which shell is being used?
echo $SHELL
uname -s
which shell
cat /etc/passwd
What is the difference between an absolute path and a relative path?
An absolute path starts from /, while a relative path starts from the current directory
An absolute path is shorter than a relative path
A relative path always includes the home directory, an absolute path does not
They are the same; both start from the current directory
How can you create a directory test inside the current directory?
mkdir test
touch test
mkfs test
cp test
How can you create multiple nested directories using a single command?
mkdir -p parent/child/grandchild
mkdir parent; mkdir child; mkdir grandchild
mkdir -r parent/child/grandchild
md -p parent/child/grandchild
How can you create an empty file example.txt?
touch example.txt
mkdir example.txt
rm example.txt
cp example.txt /tmp/
How can you copy the file source.txt to backup.txt?
cp source.txt backup.txt
mv source.txt backup.txt
cat source.txt > backup.txt && rm source.txt
ln -s source.txt backup.txt
How can you move a file from the home directory to /tmp?
mv ~/file /tmp/
cp ~/file /tmp/
rm ~/file /tmp/
cd ~/file /tmp/
How can you rename the file old.txt to new.txt?
mv old.txt new.txt
cp old.txt new.txt && rm old.txt
rename new.txt old.txt
ln -s old.txt new.txt
How can you delete a file without confirmation?
rm -f file
rm -i file
rm -r file
delete file
How can you delete a directory along with all its contents?
rm -rf dir
rmdir dir
del dir
chmod -R 000 dir
What are hidden files and how can you view them?
Files starting with a dot; view with ls -a
Encrypted files; view with cat -e
System-only files; view with sudo ls
Backup files; view with ls -b
How can you display the current directory?
pwd
whoami
ls -l
. /etc/profile
How can you navigate to the home directory?
cd ~
cd /
cd ..
cd -
How can you move two levels up from the current directory?
cd ../..
cd ..
cd ../../..
cd ./..
How can you list directory contents with detailed information?
ls -l
ls
ls -a
du -h
How can you display only directories in the current folder?
ls -d */
ls -lR
find / -type d
ls -a
How can you find a file by name in the system?
find / -name "filename"
ps aux | grep filename
cat /etc/shells
df -h
How can you find files modified in the last 24 hours?
find . -type f -mtime -1
grep -r "24h" .
ls -l --today
. -mtime 1
How can you view the size of files in a directory?
ls -lh
free -h
du -s
stat -c %s *
What are UID and GID?
Numeric identifiers for users and groups
Usernames and group names
Kernel and GPU IDs
Unique IDs for files and directories
What is the difference between the root user and a regular user?
Root has unrestricted administrative privileges; a regular user has limited permissions
Root is a normal user with a different home directory
A regular user manages the system; root is for everyday tasks
Root can only read files; a regular user can write files
How can you view your UID and GID?
id
whoami
groups
passwd -S
How can you add a new user?
useradd username
addgroup username
newuser --create
usermod -aG username
How can you delete a user along with their home directory?
userdel -r username
deluser username
rm -rf /home/username
groupdel username
How can you create a new group?
groupadd groupname
addgroup --user groupname
mkgroup groupname
usermod -G groupname
How can you add a user to a group?
usermod -aG groupname username
groupadd -a username groupname
useradd -G groupname
chgrp username groupname
How can you see which groups a user belongs to?
groups username
whoami -g
getenforce username
chgrp -l username
How can you change the owner of a file?
chown newowner file
chmod newowner file
chgrp newowner file
owner file newowner
How can you change the group of a file?
Use chgrp newgroup filename
Use chown newgroup filename
Use chmod g+ filename
Move the file into a directory named after the group
What do the numbers in chmod 755 mean?
Owner has rwx, group has r-x, others have r-x
Owner has r-x, group has rwx, others have ---
Owner has rw, group has r, others have r
Owner has ---, group has ---, others have rwx
How can you give the owner execute permission on a file?
Run chmod u+x filename
Run chmod g+x filename
Run chown +x filename
Run setfacl -m o:x filename
How can you remove write permission for the group?
Run chmod g-w filename
Run chmod g+w filename
Run chgrp -w filename
Run chmod o-w filename
How can you set permissions rw-r--r-- using numeric notation?
Use chmod 644 filename
Use chmod 664 filename
Use chmod 744 filename
Use chmod 600 filename
What is the sticky bit and where is it used?
A directory setting where only the file owner (or root) can delete or rename files, commonly on /tmp
A flag that lets scripts run with root privileges by default
A bit that prevents any file from being executed
A memory feature that compresses inactive pages
How can you set the sticky bit on a directory?
Run chmod +t directory
Run chown +t directory
Run chmod 644 directory
Sticky bit applies only to files with chmod g+t
How can you view file permissions using ls -l?
Run ls -l to list long format showing permissions, owner, group, and size
Run ls -a to show only permissions
Run stat -c %A to show only ACL entries
Use cat filename to display permissions at the top
How can you change a user’s password?
Run passwd username
Run chpasswd username directly on the prompt
Run usermod -p username
Run chage -l username
How can you lock a user account?
Run usermod -L username
Run passwd -u username
Run useradd -l username
Run chsh -s /sbin/nologin username
How can you view the last login of a user?
Run lastlog -u username
Run whoami
Run last with no options only
Run users
What is a process in Linux?
An instance of a running program
A file stored on disk
A user’s terminal session
A kernel module loaded but not running
How can you view all running processes?
Run ps aux
List /proc with ls -l
Run top -n 1 only shows one user
Run who
How can you view processes of a specific user?
Run ps -u username
Run ps -p username
Run ps aux | grep username is the standard command
Run killall -u username to list then kill
How can you find a process by name?
Use pgrep name
Use pidofall name
Use grep name /etc/passwd
Use which name
How can you display a process tree?
Run pstree
Run pmap
Run ps -q
Run tree /proc to show processes
What are PID and PPID?
Process ID and parent process ID
Permissions identifier and process privilege ID
Packet identifier and parent descriptor
Processor index and process device
How can you terminate a process by PID?
Run kill PID
Run stop PID
Delete /proc/PID with rm -f
Run exit PID from the shell
How can you forcefully terminate a process?
Run kill -9 PID
Run killall -1 PID
Run pkill -STOP name
Run systemctl stop -f process
How can you send the HUP signal to a process?
Run kill -HUP PID
Run pkill -9 name
Run killall -KILL process
Run kill -CONT PID
How can you suspend a process?
Send SIGSTOP, for example kill -STOP PID
Send SIGKILL with kill -9 PID
Run bg to stop it
Run renice +5 PID
How can you resume a suspended process?
Send SIGCONT, for example kill -CONT PID
Send SIGSTOP to the process
Run renice 0 PID
Exit and restart the shell
How can you run a process in the background?
Append & to the command
Prefix the command with sudo
Use nohup as the only requirement
Set a higher ulimit value first
How can you bring a background process to the foreground?
Use fg %job
Use bg %job
Use jobs -f to attach
Press Ctrl+C in the terminal
What is the jobs command used for?
Listing active jobs in the current shell
Viewing system services
Editing crontab entries
Compiling source code
How can you monitor CPU and memory usage in real time?
Run top
Run who
Run ls
Run passwd
How can you sort processes by memory usage?
In top, press M to sort by memory
In top, press P to sort by memory
With ps, use --sort +pid to show memory first
In free, press m to sort
How can you change a process priority at startup?
Start the command with nice -n 10 command
Use renice -n 10 PID before starting
Use kill -n 10 PID
Use chrt -p command without options
How can you change the priority of a running process?
Run renice -n 5 -p PID
Run nice -n 5 PID on an already running process
Run chmod +x PID to grant priority
Run fg -n PID to raise priority
What is a zombie process?
A process that has exited but remains in the process table until its parent reads its status
A process stuck in uninterruptible sleep waiting for I/O
An orphaned running process with no parent
A hidden malware process masquerading as init
How can you find and eliminate a zombie process?
Use ps to identify processes with state Z and terminate or restart their parent process
Run kill -9 on the zombie process ID to remove it directly
Use nice -n 19 on the zombie to lower priority until it disappears
Run top and kill the child process with SIGSTOP
How can you open a file in Nano?
nano filename
open filename with nano
nano --edit filename
edit filename -n
How can you save a file and exit Nano?
Press Ctrl+O, Enter to write, then Ctrl+X to exit
Press Ctrl+S to save and Ctrl+Q to quit
Type :wq and press Enter
Press F10 to save and exit
How can you exit Nano without saving?
Press Ctrl+X and answer N when prompted
Type :q! and press Enter
Press Ctrl+Q twice
Press Esc Esc and Enter
How can you start text selection in Nano?
Press Ctrl+^ (Ctrl+6) to set the mark
Press Shift+Arrow to start selection
Press Ctrl+A to begin selecting
Press Alt+S to start selection
How can you copy selected text in Nano?
Press Alt+6 to copy the marked region
Press Ctrl+C to copy
Press Ctrl+Insert to copy
Press Alt+C to copy selection
How can you paste text in Nano?
Press Ctrl+U to paste
Press Ctrl+V to paste
Press Shift+Insert to paste
Press Alt+P to paste
How can you search for text in Nano?
Press Ctrl+W and type the search term
Type /pattern and press Enter
Press Ctrl+F to find
Press Alt+F and type the term
How can you replace text in Nano?
Press Ctrl+\ to start replace
Type :%s/old/new/g
Press Ctrl+R to replace
Press Alt+R and enter terms
How can you open Vim from the command line?
vim filename
open -e vim filename
vi --open filename
vim --start filename
How can you exit Vim without saving?
:q! in normal mode
:quit! in insert mode
ZZ
ZQ then Enter
How can you save a file in Vim?
:w in normal mode
:save in any mode
:write! only in insert mode
:wq! only
How can you switch to insert mode in Vim?
Press i in normal mode
Press s in command-line mode
Type :insert
Press Ctrl+I
How can you delete a line in Vim?
dd in normal mode
dl in insert mode
D in insert mode
:delete line
How can you undo the last action in Vim?
Press u in normal mode
Press Ctrl+Z
Type :undo!
Press U twice in insert mode
How can you copy a line in Vim?
yy in normal mode
cc in normal mode
y$ in insert mode
:copyline
How can you paste copied text in Vim?
p in normal mode
Ctrl+V in insert mode
Shift+Insert in any mode
:paste!
How can you search for a word in Vim?
/word in normal mode
Ctrl+F and type word
?word in insert mode
:find word from command-line mode
How can you replace a word throughout the entire file in Vim?
:%s/old/new/g
:s/old/new
:g/old/replace/new
:A/old/new/all
How can you jump to line 50 in Vim?
:50
gg50
G50
/50
How can you display line numbers in Vim?
:set number
:show numbers
:lineno on
:set nu! off
How can you find the string "error" in a log file?
grep "error" logfile.log
find "error" logfile.log
awk "error" logfile.log
sed -n "error" logfile.log
How can you search for a string without case sensitivity?
grep -i "pattern" file
grep --nocase-only "pattern" file
grep -c "pattern" file
grep -I "pattern" file
How can you display line numbers in grep output?
grep -n "pattern" file
grep --lines "pattern" file
grep -l "pattern" file
grep --show-numbers "pattern" file
How can you invert a grep search (show lines without the pattern)?
grep -v "pattern" file
grep --no-match "pattern" file
grep -x "pattern" file
grep --invert-only file
How can you search for text recursively in a directory?
grep -R "pattern" directory
grep --deep "pattern" directory
grep -d "pattern" directory
grep -P "pattern" directory
How can you replace "foo" with "bar" in a file using sed?
sed -i 's/foo/bar/g' file
sed 'find foo replace bar' file
sed -r 'foo->bar' file
sed --swap foo bar file
How can you delete line 5 from a file using sed?
sed -i '5d' file
sed 'd5' file
sed --delete 5 file
sed -n '5p' file
How can you append text to the end of each line using sed?
sed -i 's/$/text/' file
sed -i '$a text' file to every line
sed 'append text' file
sed -A 'text$' file
How can you print the second column of a file using awk?
awk '{print $2}' file.txt
awk '{print 1, 2}' file.txt
awk -F: '{print $1}' file.txt
sed -n '2p' file.txt
How can you print lines where the third column is greater than 100 using awk?
awk '$3>100' file.txt
awk '{if ($3>=100) print}' file.txt
grep -E '3>100' file.txt
cut -d' ' -f3 file.txt
How can you calculate the sum of numbers in the first column using awk?
awk '{s+=$1} END{print s}' file.txt
awk 'END{print $1}' file.txt
awk '{sum=$1} END{print sum}' file.txt
sed 's/\n/+/' file.txt | bc
How can you sort a file alphabetically?
sort file.txt
sort -n file.txt
uniq file.txt
grep sort file.txt
How can you sort a file numerically?
sort -n file.txt
sort -r file.txt
uniq -c file.txt
sort -h file.txt
How can you remove duplicate lines from a file?
sort -u file.txt
uniq -d file.txt
grep -v duplicate file.txt
awk '!seen[$0]++' file.txt | head -n 1
How can you count occurrences of each line in a file?
sort file.txt | uniq -c
wc -l file.txt
awk '{print NR,$0}' file.txt
cat file.txt | nl
How can you find all .conf files in /etc?
find /etc -type f -name "*.conf"
grep -R ".conf" /etc
ls /etc/*.conf -R
locate /etc/*.conf
How can you find files larger than 100 MB?
find / -type f -size +100M
du -sh * | grep 100M
ls -lh | awk '$5>100M'
df -h | grep +100M
How can you find and delete all .tmp files in /tmp?
find /tmp -type f -name "*.tmp" -delete
rm -rf /tmp/*.tmp /s
find /tmp -type d -name "*.tmp" -prune -delete
xargs -0 rm -f /tmp/*.tmp
How can you use xargs to delete found files?
find . -type f -name "*.tmp" -print0 | xargs -0 rm -f
ls *.tmp | xargs rm -rf --preserve-root
grep -R tmp . | xargs rm -f
xargs find . -name "*.tmp" -delete
How can you pass the output of one command as arguments to another?
command1 | xargs command2
command1 > command2
command2 <(command1) without xargs
command1 && command2
How can you check internet connectivity?
ping -c 4 google.com
traceroute -n 8.8.8.8
nc -zv google.com 80
curl -O google.com
How can you find the IP address of your network interface?
ip addr show
ip route show
ss -tuln
dig +short myip.opendns.com @resolver1.opendns.com
How can you view the routing table?
ip route show
ip link show
route add default gw 192.168.1.1
ss -s
How can you check port availability on a remote host?
nc -zv example.com 80
ss -tuln | grep 80
ip route get example.com
ping -f example.com
How can you view open ports on the local machine?
ss -tuln
nc -zv localhost 1-65535
lsof /proc/net/tcp
ip addr
How can you find which process is using port 80?
sudo lsof -i :80
ip route show | grep 80
ps aux | grep 80
netstat -r | grep 80
How can you use traceroute?
traceroute example.com
tracepath -p 80
ping -R example.com
route -n traceroute
What does the ss command do?
Displays socket statistics and network connections
Shows disk usage by directory
Scans remote hosts for open vulnerabilities
Manages software packages and repositories
How can you connect to a remote server via SSH?
ssh user@server.example.com
telnet user@server.example.com
nc -zv server.example.com 22
scp user@server.example.com
How can you copy a file to a remote server using SCP?
scp file.txt user@server:/path/
cp file.txt user@server:/path/
rsync --delete user@server:/path/ file.txt
ssh user@server:/path/ file.txt
How can you generate SSH keys?
ssh-keygen
ssh-copy-id
openssl rsa -in ~/.ssh/id_rsa
gpg --gen-key
How can you copy a public SSH key to a server?
ssh-copy-id user@server
ssh-keygen -t rsa
scp ~/.ssh/id_rsa.pub user@server:/authorized_keys
ssh-add -L user@server
How can you configure the SSH client using a config file?
Create or edit ~/.ssh/config with Host entries
Modify /etc/hosts for SSH shortcuts
Place keys in /usr/local/ssh/config
Run ssh --configure to generate settings
How can you check DNS name resolution?
dig example.com
ip route show example.com
ss -s
arp -a example.com
How can you view network interfaces?
ip link show
ip route list table all
ss -tuln
route -n
How can you restart the network service?
sudo systemctl restart NetworkManager
sudo systemctl start sshd
sudo ifconfig restart
sudo service cron restart
How can you add a static route?
sudo ip route add 10.0.0.0/24 via 192.168.1.1
sudo ifconfig eth0 add 10.0.0.0/24 gw 192.168.1.1
sudo netstat -r add 10.0.0.0/24 via 192.168.1.1
sudo route show add 10.0.0.0/24 gw 192.168.1.1
How can you test network download speed?
Run speedtest-cli
Run ping -f
Run netstat -s
Run traceroute -d
How can you view network interface statistics?
ip -s link
ip addr flush
route -n
ping -c 1 1.1.1.1
How can you configure port forwarding using SSH?
ssh -L 8080:remote.host:80 user@remote.host
ssh -X 8080:remote.host:80 user@remote.host
ssh --forward 8080 80 user@remote.host
ssh -R 8080:remote.host:80 user@remote.host
What is a shebang?
The first line like #!/bin/bash that tells the OS which interpreter to use
A Bash comment used to disable a script
A special variable that stores the script name
A file permission that makes a script executable
How can you make a script executable?
chmod +x script.sh
chown +x script.sh
exec script.sh
bash --make-exec script.sh
How can you declare a variable in Bash?
NAME="value"
let NAME = value
var NAME = "value"
declare NAME := "value"
How can you output the value of a variable?
echo "$NAME"
printf NAME
cat $NAME
display ${NAME} without quotes
How can you request user input?
read NAME
input NAME
ask NAME
gets NAME
How can you pass arguments to a script?
Provide them after the script name, for example ./script.sh arg1 arg2
Write them into the script header with #ARGS=...
Export them as environment variables only
Use sudo to supply arguments interactively
How can you check the number of passed arguments?
Echo the special parameter $#
Count words in $@ using wc
Use ${#1} to get the total
Check the variable $ARGC
How can you reference the first script argument?
$1
$0
$@
$#
How can you check if a file exists?
if [ -f "file.txt" ]; then ... fi
if [ -eql file.txt ]; then ... fi
if test -d file.txt; then ... fi
if [ file.txt ]; then ... fi
How can you check if a file is a directory?
if [ -d "/path" ]; then ... fi
if [ -f "/path" ]; then ... fi
if [ -x "/path" ]; then ... fi
if [ -s "/path" ]; then ... fi
How can you write an if conditional statement?
if [ condition ]; then commands; fi
if (condition) { commands }
if condition: commands fi
if: condition then commands end
How can you compare strings in Bash?
[[ " a b" ]]
[ a−eq b ]
a=∗ b in test
compare " a b" using -gt
How can you compare numbers in Bash?
[ " a b" ]
[[ " a b" ]]
[ " a b" ]
[ " a b" ]
How can you write a for loop?
for i in 1 2 3; do echo "$i"; done
for (i=1; i<=3; i++) echo $i
foreach i (1 2 3) { print $i }
loop i in 1..3 -> echo i
How can you write a while loop?
while condition; do commands; done
while (condition) { commands }
while: condition then commands end
do while condition: commands done
How can you process each line of a file in a loop?
while IFS= read -r line; do commands; done < file.txt
for line in file.txt; do commands; done
cat file.txt | for each line; do commands; done
read -f file.txt while true; do commands; done
How can you create a function in Bash?
greet() { echo "hi"; }
function greet: echo "hi" end
def greet(): echo "hi"
func greet { echo "hi" }
How can you call a function?
Write its name, for example greet
Use call greet()
bash run greet
exec function greet
How can you return a value from a function?
Echo the value and capture it with command substitution, for example result=$(myfunc)
Use return 42 to pass a string result
Assign to $1 inside the function to set the caller's variable
Write to /proc/return
How can you handle errors in a script?
Enable strict mode, for example set -euo pipefail, and check exit statuses
Ignore nonzero exit codes to keep running
Redirect all errors to /dev/null to avoid failures
Use sleep to delay failures until the end
How can you view the status of a service?
systemctl status nginx
service nginx runlevel
systemctl watch nginx
chkconfig --state nginx
How can you start a service?
systemctl start nginx
service nginx enable
systemctl up nginx
systemctl on nginx
How can you stop a service?
systemctl stop nginx
systemctl killall
systemctl disable nginx
service nginx down-now
How can you restart a service?
systemctl restart nginx
systemctl reload --all
service nginx reset
systemctl bounce nginx
How can you enable a service at boot?
systemctl enable nginx
systemctl autostart nginx now
service nginx boot-on
systemctl start nginx --onboot
How can you disable a service from startup?
systemctl disable nginx
systemctl stop-on-boot nginx
service nginx boot-off
systemctl mask-once nginx
How can you view all running services?
systemctl list-units --type=service --state=running
systemctl list-unit-files --type=service
journalctl -u sshd
service --status-all
How can you list all available services?
systemctl list-unit-files --type=service
systemctl list-units --type=service --state=running
journalctl -xe
systemctl status
How can you view service dependencies?
systemctl list-dependencies sshd
journalctl -u sshd -p info
systemctl cat sshd
systemctl show -p Requires
How can you check if a service is enabled?
systemctl is-enabled sshd
systemctl is-active sshd
systemctl enable sshd --check
systemctl status sshd | grep enabled
