Skip to content
Laviyx Learn

Try

Linux and Shell Scripting Interview Questions

Frequently asked Linux and Bash interview questions on commands, permissions, processes, services, cron, networking, disk space, troubleshooting and shell scripting, with hints and answers.

23 questions · Updated

  1. 1.What is the difference between the Linux kernel and a distribution?

    BeginnerConceptBasics
    Show hint

    Ubuntu is not the kernel.

    Show solution

    The kernel is the core that manages hardware, memory and processes. A distribution such as Ubuntu or Red Hat packages the kernel with tools, a package manager and defaults.

  2. 2.What does chmod 754 mean?

    BeginnerConceptPermissions
    Show hint

    Add 4, 2 and 1 for each of three groups.

    Show solution

    The owner has 7 (rwx), the group has 5 (r-x), and others have 4 (r--). So the owner can do anything, the group can read and execute, and everyone else can only read.

  3. 3.How do you find files larger than 100 MB?

    BeginnerCodingFiles
    Show hint

    find has a size condition.

    Show solution

    find / -type f -size +100M 2> /dev/null. The redirect hides permission errors.

  4. 4.What does 2>&1 mean?

    BeginnerConceptPipes
    Show hint

    Streams have numbers.

    Show solution

    It sends stream 2 (stderr) to wherever stream 1 (stdout) is going. In cmd > all.log 2>&1, both output and errors go to the file. It must come after the >.

  5. 5.What is the difference between grep and find?

    BeginnerConceptFiles
    Show hint

    Inside files, or the files themselves?

    Show solution

    grep searches the text inside files. find searches for files by name, type, size or age.

  6. 6.How do you find and stop a process that is using too much CPU?

    BeginnerDebuggingProcesses
    Show hint

    Sort first, then signal.

    Show solution

    Use top, or ps aux --sort=-%cpu | head to find the PID. Send kill PID, and use kill -9 PID only if it does not stop.

  7. 7.Print the 5 most frequent values in the first column of a file.

    IntermediateCodingText tools
    Show hint

    Four tools in a pipe.

    Show solution

    awk '{print $1}' file | sort | uniq -c | sort -nr | head -n 5

  8. 8.How do you add up the third column of a CSV file?

    IntermediateCodingText tools
    Show hint

    awk has END.

    Show solution

    awk -F, '{sum += $3} END {print sum}' file.csv

  9. 9.What is the difference between SIGTERM and SIGKILL?

    IntermediateConceptProcesses
    Show hint

    Which one can be caught?

    Show solution

    SIGTERM (15) asks a process to stop, so it can clean up. SIGKILL (9) ends it immediately, and it cannot be caught.

  10. 10.What is the difference between systemctl start and enable?

    IntermediateConceptServices
    Show hint

    Now, or at boot?

    Show solution

    start runs the service now. enable makes it start at boot. enable --now does both.

  11. 11.Write a cron line that runs a script every day at 02:30.

    IntermediateCodingAutomation
    Show hint

    Minute comes first.

    Show solution

    30 2 * * * /home/asha/backup.sh >> /home/asha/backup.log 2>&1

  12. 12.A script works by hand, but not in cron. Why?

    IntermediateDebuggingAutomation
    Show hint

    Compare the environments.

    Show solution

    cron has a minimal environment, so PATH is short and .bashrc is not loaded. Use full paths, cd in the script, and log the output to see the error.

  13. 13.How does SSH key login work?

    IntermediateConceptNetworking
    Show hint

    Which key goes where?

    Show solution

    The public key is in the server's ~/.ssh/authorized_keys. Your client proves that it holds the private key by signing a challenge. The private key is never sent.

  14. 14.How do you find which process is listening on port 8080?

    IntermediateDebuggingNetworking
    Show hint

    ss or lsof.

    Show solution

    sudo ss -ltnp | grep :8080, or sudo lsof -i :8080.

  15. 15.The disk is full. How do you find the cause?

    IntermediateDebuggingDisk
    Show hint

    Start wide, then go deeper.

    Show solution

    df -h shows which file system is full, and du -h --max-depth=1 / | sort -hr finds the biggest folders. Check logs and caches. If du shows much less than df, run lsof +L1 to find deleted files that are still open.

  16. 16.Why should you put quotes around Bash variables?

    IntermediateConceptScripting
    Show hint

    What if the value has a space?

    Show solution

    Without quotes, Bash splits the value into words and expands wildcards. rm $file with a file called my report.txt tries to remove two files. rm "$file" is correct.

  17. 17.What does set -euo pipefail do?

    IntermediateConceptScripting
    Show hint

    Three separate settings.

    Show solution

    -e stops on a failed command, -u makes unset variables an error, and -o pipefail makes a pipeline fail if any command in it fails.

  18. 18.What is the difference between source script.sh and ./script.sh?

    IntermediateConceptScripting
    Show hint

    Which shell runs it?

    Show solution

    ./script.sh runs in a child process, so its variables are lost when it ends. source runs in the current shell, so its variables stay.

  19. 19.A server is slow. How do you investigate?

    AdvancedDebuggingTroubleshooting
    Show hint

    Load, CPU, memory, disk, network, logs.

    Show solution

    Check uptime, then top for CPU and iowait, free -h for memory and swap, df -h for disk, and iostat for disk activity. Then read the logs of the busy process.

  20. 20.Describe the Linux boot process.

    AdvancedConceptInternals
    Show hint

    Firmware, loader, kernel, PID 1.

    Show solution

    Firmware starts the boot loader (GRUB), which loads the kernel and initial RAM disk. The kernel mounts the root file system and starts PID 1, usually systemd, which starts the services.

  21. 21.What happens when you run a command in the shell?

    AdvancedConceptInternals
    Show hint

    Two system calls.

    Show solution

    The shell parses the line and finds the program through PATH. It calls fork to make a child, and the child calls exec to load the program. The parent waits, and stores the exit code in $?.

  22. 22.Which Linux features make containers possible?

    AdvancedConceptInternals
    Show hint

    Isolate and limit.

    Show solution

    Namespaces isolate what a process can see, such as its PIDs, network and mounts. cgroups limit what it can use, such as CPU and memory.

  23. 23.A process vanished with no error in its log. What might have happened?

    AdvancedDebuggingTroubleshooting
    Show hint

    Memory.

    Show solution

    The kernel's OOM killer may have ended it when memory ran out. Check journalctl -k or dmesg for "Out of memory".

Review the lessons