Linux Interview Q&A

1. What is Linux, and why is it important for DevOps?

Linux is an open-source, Unix-like operating system widely used in servers, cloud environments, and containers. DevOps engineers rely on Linux for running web/app servers, automating deployments, managing cloud infrastructure, and system monitoring.

2. What is the difference between Linux and Unix?

Unix is proprietary, developed in the 1970s, mainly used in enterprise servers. Linux is open-source, community-driven, and widely adopted in cloud, DevOps, and containerized environments.

3. How do you check the Linux version and distribution?

Use `cat /etc/os-release`, `uname -r`, or `lsb_release -a` to display OS version and kernel details.

4. What is the difference between absolute and relative paths in Linux?

Absolute Path: starts from root (/), e.g., /home/user/file.txt. Relative Path: based on current directory, e.g., ./file.txt.

5. How do you check running processes in Linux?

Use `ps aux` for a snapshot, `top` or `htop` for real-time monitoring.

6. How do you kill a process in Linux?

Use `kill -9 <PID>` to kill by process ID, or `pkill <process_name>` to kill by name. Example: `kill -9 1234`.

7. What is the difference between hard link and soft link?

Hard Link: Points directly to file’s inode. If the original is deleted, data remains. Soft Link (symlink): Points to file path. If the original is deleted, link is broken.

8. What is the difference between cron and at in Linux?

`cron` schedules recurring tasks. `at` schedules a one-time task.

9. How do you check disk usage in Linux?

Use `df -h` to check disk space by filesystem, and `du -sh` to check usage of a directory.

10. How do you check memory usage in Linux?

Run `free -m` or `free -h` to see memory usage. `vmstat` and `sar` provide detailed stats.

11. How do you search for a string inside files in Linux?

Use `grep 'keyword' filename` for a single file, or `grep -r 'keyword' /path/` for recursive search.

12. How do you find the top 10 largest files or directories?

Files: `du -ah /path | sort -rh | head -10`. Directories: `du -h --max-depth=1 | sort -hr | head -10`.

13. How do you monitor real-time logs in Linux?

Use `tail -f /var/log/syslog` or `tail -f /var/log/messages` for live monitoring.

14. How do you check system logs in Ubuntu?

Run `journalctl` or check `/var/log/syslog`.

15. What is SELinux?

SELinux (Security-Enhanced Linux) is a security module providing access control policies. Modes: Enforcing, Permissive, Disabled.

16. What is the difference between TCP and UDP?

TCP: Reliable, connection-oriented (e.g., SSH, HTTP). UDP: Faster, connectionless (e.g., DNS, Streaming).

17. How do you check which ports are open or being used?

Use `ss -tuln`, `netstat -tuln`, or `lsof -i`. To check which port a process is listening on, use `ss -tulnp`.

18. How do you create and extract a tar file?

Create: `tar -cvf archive.tar file1 file2`. Extract: `tar -xvf archive.tar`. For gzip: `tar -xvzf file.tar.gz`. To compress a directory: `tar -cvzf archive.tar.gz dir/`.

19. What is the difference between a shell and a terminal?

Shell: Command-line interpreter (e.g., Bash, Zsh). Terminal: Interface that allows users to interact with the shell.

20. How to display the most recently modified file in Linux?

Use `ls -ltr` to list files by modification time, with the latest at the bottom.

21. How to create nested directories like d1/d2/d3?

Run `mkdir -p d1/d2/d3`.

22. How to remove write permissions for group and others while keeping full permissions for the owner?

Use `chmod 755 filename`.

23. How to make a file read-only for everyone?

Run `chmod 444 filename`.

24. How to create a soft link to a directory?

Use `ln -s target_directory link_name`.

25. How to rename a file or directory?

Use `mv oldname newname`.

26. How to remove a directory without write permissions?

Use `rm -rf directory_name`.

27. How to display the first three lines of a file?

Use `head -3 filename`.

28. How to monitor a file in real-time?

Use `tail -f filename`.

29. How to find all XML files in a directory recursively?

Run `find /path -name "*.xml"`.

30. How to display the current working directory?

Use `pwd`.

31. How to display hidden files?

Use `ls -a`.

32. How to copy a file F1 to /tmp?

Run `cp F1 /tmp/`.

33. How to move a file F1 to /tmp?

Run `mv F1 /tmp/`.

34. How to search for the keyword 'error' in log.txt?

Use `grep 'error' log.txt`.

35. How to sort contents of a file?

Use `sort filename`.

36. How to count the number of lines in a file?

Run `wc -l filename`.

37. How to check system uptime?

Use `uptime`.

38. How to display logged-in users?

Run `who` or `w`.

39. How to check your system IP address?

Use `ip addr show` or `ifconfig`.

40. How to test connectivity to google.com?

Run `ping google.com`.

41. How to download a file from the internet?

Use `wget URL` or `curl -O URL`.

42. How to add and manage users in Linux?

Add user: `sudo adduser username`. Switch user: `su - username`. Delete user: `userdel -r username`.

43. How to check and manage groups in Linux?

Check groups: `groups username`. Add user to group: `usermod -aG group username`.

44. How to change file ownership?

Use `chown user:group filename`.

45. How to update and upgrade packages in Ubuntu?

Run `sudo apt update && sudo apt upgrade -y`.

46. How to install or remove nginx in Ubuntu?

Install: `sudo apt install nginx -y`. Remove: `sudo apt remove nginx -y`.

47. How to schedule cron jobs?

Use `crontab -e`. Example: `0 2 * * * command` runs daily at 2AM. Use `*/5 * * * *` for every 5 minutes.

48. How to check scheduled cron jobs?

Run `crontab -l`. For root: `sudo crontab -l -u root`.

49. How to display the last 50 lines of a file?

Use `tail -50 filename`.

50. How to search for a running process by name?

Use `ps aux | grep processname`.

51. How to check CPU usage in real time?

Use `top` or `htop`.

52. How to check the default shell of a user?

Use `echo $SHELL` or check `/etc/passwd`.

53. How to change the default shell?

Use `chsh -s /bin/bash username`.

54. How to check open files of a process?

Use `lsof -p PID`.

55. How to display environment variables?

Use `printenv` or `env`.

56. How to set and make an environment variable permanent?

Temporary: `export VAR=value`. Permanent: add to `~/.bashrc`.

57. How to check and change hostname?

Check: `hostname`. Change: `hostnamectl set-hostname newname`.

58. How to list and manage services with systemd?

List: `systemctl list-units --type=service`. Start/Stop: `systemctl start|stop servicename`. Enable: `systemctl enable servicename`. Status: `systemctl status servicename`.

59. How to reboot or shut down a system?

Reboot: `reboot` or `shutdown -r now`. Shutdown: `shutdown -h now` or schedule with `shutdown -h +10`.

60. How to lock and unlock a user account?

Lock: `passwd -l username`. Unlock: `passwd -u username`.

61. How to check last logins of users?

Use `last` or `lastlog`.

62. How to manage firewall rules?

Block IP: `iptables -A INPUT -s IP -j DROP`. Allow port 80: `ufw allow 80/tcp`.

63. How to check DNS resolution?

Use `dig domain.com` or `nslookup domain.com`.

64. How to check default gateway?

Use `ip route show`.

65. How to mount and unmount partitions?

Mount: `mount /dev/sdX /mnt/point`. Unmount: `umount /mnt/point`. List: `df -h`.

66. How to check inode usage?

Use `df -i`.

67. How to create and edit files?

Empty file: `touch file.txt`. With content: `echo 'text' > file.txt`. Append: `echo 'text' >> file.txt`. Edit: `nano file.txt` or `vi file.txt`.

68. How to count words, lines, and characters in a file?

Use `wc file.txt`.

69. How to display unique lines in a file?

Use `sort file.txt | uniq`.

70. How to compare and merge files?

Compare: `diff file1 file2`. Merge: `cat file1 file2 > newfile`.

71. How to create and run a shell script?

Create with shebang `#!/bin/bash`. Make executable with `chmod +x script.sh`. Run with `./script.sh` or `bash script.sh`.

72. How to monitor performance and kernel logs?

Performance: `vmstat`, `iostat`, `sar`. Kernel logs: `dmesg`.

73. How to manage SSH keys and connections?

Generate: `ssh-keygen -t rsa -b 4096`. Copy key: `ssh-copy-id user@host`. Connect: `ssh user@host`. Transfer files: `scp file user@host:/path/`.