In this article we are going to cover Linux Interview Questions and Answers, Linux Scenario based interview questions and answers.
1. User Management: A user complains they cannot log in. How will you troubleshoot?
Ans:- Check if the username is correct, verify the password using passwd --status or chage -l, and check /var/log/auth.log for login failures and reasons. Also, confirm the user’s shell and home directory exist.
2. File Permissions: A script is executable by one user but not another. How do you resolve this?
Ans:- Use ls -l to check permissions on the script. Ensure the non-executing user has read and execute permissions, or is part of a group that does. Use chmod to add necessary permissions.
3. Process Management: A service is consuming 100% CPU. How will you find and fix it?
Ans:- Use top or htop to identify the process by PID. Once identified, analyze its logs for issues. If necessary, send a SIGTERM (kill) signal to gracefully stop it, or SIGKILL for immediate termination.
4. SSH Issues: You cannot SSH into a remote machine. How do you debug?
Ans:- Check if the remote machine is reachable (ping). Verify the SSH daemon is running (systemctl status sshd or service ssh status). Check firewall rules on both client and server, and review /var/log/auth.log on the server for connection attempts and errors.
5. Disk Space Full: / partition is full. How do you find and delete large files safely?
Ans:- Use du -sh * in various directories, or du -ax / | sort -rh | head -n 20 to find large directories. Safely delete log files, old backups, or temporary files after verifying they are no longer needed.
6. File Corruption: A log file is showing junk characters. How will you check and recover it?
Ans:- First, check the disk for errors using fsck. If the file is still corrupted, it’s likely permanently damaged. You might try to recover from a backup. If it’s an active log, restart the service writing to it to regenerate a clean log.
7. Crontab Not Running: A scheduled job is not executing. How do you debug?
Ans:- Check crontab -l for the user and ensure the command path is absolute. Verify the cron service is running. Look for errors in /var/log/syslog or /var/log/cron. Redirect cron job output to a file to capture errors.
8. Package Installation Failing: yum or apt is failing. How will you resolve it?
Ans:- Check network connectivity and repository reachability. Clear package manager caches (yum clean all or apt clean). Update package lists (yum update or apt update). Check for broken packages or dependency issues.
9. User Cannot Sudo: A user was added to sudoers, but sudo still doesn’t work. What could be wrong?
Ans:- Verify the entry in /etc/sudoers or /etc/sudoers.d/ is syntactically correct (use visudo). Ensure the user is actually part of the specified group (e.g., wheel or sudo) using groups username. Check if the user needs to log out and back in for group changes to take effect.
10. Kernel Panic: How do you troubleshoot and recover from a kernel panic?
Ans:- A kernel panic usually indicates a serious hardware or driver issue. The system will halt. You’ll need to reboot. Analyze the console output for the panic message, which often points to the failing module or driver. Check hardware, update drivers, or try an older kernel version.
11. Zombie Processes: How do you identify and remove zombie processes?
Ans:- Use ps aux | grep Z or top (look for ‘Z’ state). Zombie processes are already dead but their parent hasn’t reaped their exit status. You cannot kill a zombie directly. The parent process needs to be fixed or killed, which will then cause init to inherit and clean up the zombie.
12. Too Many Open Files Error: How do you fix “Too many open files” in Linux?
Ans:- This indicates hitting the ulimit for open files. Use ulimit -n to see the current limit. Increase the soft and hard limits in /etc/security/limits.conf and apply system-wide changes by editing /etc/sysctl.conf for fs.file-max.
13. Finding and Killing Processes: A process is causing high memory usage. How do you locate and stop it?
Ans:- Use top or htop and sort by memory usage (M key in top). Identify the process ID (PID). Use kill <PID> to send a SIGTERM signal, or kill -9 <PID> for a SIGKILL if it doesn’t respond.
14. Home Directory Missing: A user’s home directory is missing. How will you restore it?
Ans:- If a backup exists, restore it. Otherwise, manually create the home directory (mkdir /home/username), copy default skeleton files from /etc/skel (cp -r /etc/skel/. /home/username), and set correct ownership and permissions (chown -R username:groupname /home/username).
15. Time Sync Issues: The server time is incorrect. How do you fix NTP sync?
Ans:- Ensure the ntpd or chronyd service is running and configured correctly with reliable NTP servers in /etc/ntp.conf or /etc/chrony.conf. Manually sync once with ntpdate (if ntpd not running) or chronyc sources -v, then restart the service.
16. Finding Recently Changed Files: How do you find all files modified in the last 10 minutes?
Ans:- Use find /path/to/search -type f -mmin -10.
17. Deleting Large Files But Space Not Freeing Up: What could be the reason?
Ans:- The most common reason is that a process still has the deleted file open. The space won’t be truly freed until the process closes the file. Identify the process using lsof | grep deleted and restart or kill it.
18. Checking System Load: How do you analyze high system load issues?
Ans:- Use uptime or top to check load averages. Investigate high CPU usage with top, high memory usage with free -h and top, and high I/O wait with iostat -x or vmstat. Check logs for errors or misbehaving applications.
19. NFS Mount Issues: How do you troubleshoot an NFS mount not working?
Ans:- Check network connectivity to the NFS server. Verify the NFS server is running and exporting the share. Check firewall rules on both client and server. Inspect /var/log/messages or dmesg on the client for mount errors. Try mounting with mount -vvv.
20. Hostname Resolution Issues: ping is working, but ssh is failing by hostname. Why?
Ans:- ping uses ICMP, while ssh requires a full TCP connection and hostname resolution. If ping by IP works but by hostname doesn’t, it indicates DNS resolution issues. Check /etc/resolv.conf, nslookup hostname, and dig hostname. Also, verify no SSH-specific firewall rules or host entries are blocking the hostname.
File System & Storage
21. How do you extend a partition without unmounting it?
Ans:- Generally, extending a physical partition requires unmounting. However, if using LVM (Logical Volume Manager), you can extend a logical volume while it’s mounted, then extend the filesystem (e.g., resize2fs for ext/xfs) without unmounting.
22. What steps are needed to add a new disk to a Linux server?
Ans:-
- Identify the new disk (
fdisk -lorlsblk). - Partition the disk (
fdiskorgparted). - Format the partition with a filesystem (
mkfs.ext4,mkfs.xfs). - Create a mount point (
mkdir /mnt/newdisk). - Mount the filesystem (
mount /dev/sdXN /mnt/newdisk). - Add an entry to
/etc/fstabfor persistent mounting on reboot.
23. How to check which processes are writing to a file in real time?
Ans:- Use lsof <filename>. This will show processes that have the file open, including those actively writing to it. For real-time monitoring of disk activity, iotop can show which processes are performing I/O.
24. How to recover a deleted file in Linux?
Ans:- It’s very difficult without specialized tools or if the blocks have been overwritten. For ext filesystems, tools like extundelete or debugfs might offer a slim chance. The best method is to restore from a recent backup.
25. How to check disk I/O performance in Linux?
Ans:- Use iostat -x 1 to get extended I/O statistics, showing metrics like %util (device utilization), await (average I/O wait time), and svctm (service time). vmstat can also show I/O wait.
26. A directory is taking too much space. How do you analyze it?
Ans:- Navigate into the directory and use du -sh * to see the size of its immediate subdirectories and files. Recursively drill down into the largest ones to identify where the space is consumed. ncdu provides an interactive, visual way to do this.
27. How do you remount a filesystem in read-write mode without rebooting?
Ans:- Use mount -o remount,rw /path/to/mountpoint.
28. What happens when a file is deleted but is still in use by a process?
Ans:- The directory entry for the file is removed, so it becomes “invisible” and cannot be accessed by name. However, the data blocks on disk are not freed until the process that has the file open finally closes its file descriptor. lsof | grep deleted can show such files.
29. How do you create and mount a swap file?
Ans:-
- Create the file:
fallocate -l 2G /swapfile(ordd if=/dev/zero of=/swapfile bs=1M count=2048). - Set permissions:
chmod 600 /swapfile. - Format as swap:
mkswap /swapfile. - Enable swap:
swapon /swapfile. - Add to
/etc/fstabfor persistence:/swapfile none swap sw 0 0.
30. How do you find large unused files across multiple partitions?
Ans:- This is complex as “unused” is hard to define. You can find large files using find / -xdev -type f -size +1G (adjust size). Determining if they’re “unused” requires checking access times (-atime), process usage (lsof), and domain knowledge of the system.
31. How do you fix a corrupted file system using fsck?
Ans:- First, unmount the filesystem: umount /dev/sdXN. Then run fsck -y /dev/sdXN (the -y automatically answers yes to prompts, use with caution). If it’s the root filesystem, you’ll need to boot into rescue mode or from a live CD.
32. What is inode exhaustion, and how do you resolve it?
Ans:- Inode exhaustion occurs when a filesystem runs out of available inodes (which store metadata about files and directories), even if there’s still free data block space. It’s common with many small files. Resolution involves deleting small, unneeded files, moving files to a different filesystem with more inodes, or creating a new filesystem with a higher inode density.
33. You need to copy a huge file across servers. What’s the fastest way?
Ans:- scp or rsync are common. For very large files, rsync -P provides progress and allows resuming. If bandwidth is limited, consider using tar and ssh in a pipeline (e.g., tar -cf - large_file | ssh user@remote "cd /dest/path && tar -xf -") which saves temporary disk space.
34. Your df -h and du -sh show different disk usage. Why?
Ans:- df -h reports filesystem usage based on mounted partitions. du -sh reports actual disk space used by files within a directory. The discrepancy often arises because:
- Files are open and deleted by a process (see Q28).
- Bind mounts or other mounts hide files in the underlying directory.
dudoesn’t count files in other filesystems, whiledfincludes unreadable sectors or reserved blocks.
35. How do you find out which directory is consuming the most in /var?
Ans:- Use du -sh /var/* | sort -rh | head -n 5. This will list the top 5 largest subdirectories in /var in a human-readable format.
Conclusion:
We have covered Linux Interview Questions and Answers.
Related Articles: