20 Scenario-Based Linux Interview
Questions & Answers
1. User Management: A user complains they cannot log in. How will you troubleshoot?
1. Check if the user exists:
cat /etc/passwd | grep username
2. Verify the account is not locked:
sudo passwd -S username
3. Check failed login attempts:
sudo faillog -u username
4. Ensure correct permissions for the home directory:
ls -ld /home/username
2. File Permissions: A script is executable by one user but not another. How do you resolve this?
1. Check the script permissions:
ls -l script.sh
2. Add execute permission if missing:
chmod +x script.sh
3. Check file ownership:
chown user:user script.sh
3. Process Management: A service is consuming 100% CPU. How will you find and fix it?
1. Identify the process:
top or htop
2. Get detailed information:
ps aux --sort=-%cpu
3. Kill or restart the process:
kill -9 PID or systemctl restart service
4. SSH Issues: You cannot SSH into a remote machine. How do you debug?
1. Verify SSH service is running:
sudo systemctl status sshd
2. Check firewall rules:
sudo iptables -L
3. Ensure correct permissions for ~/.ssh/authorized_keys
4. Look for errors in logs:
sudo journalctl -u sshd --since '5 minutes ago'
5. Disk Space Full: / partition is full. How do you find and delete large files safely?
1. Find large files:
sudo du -ah / | sort -rh | head -20
2. Delete safely:
sudo rm -rf /path/to/large/file
3. Check open deleted files:
sudo lsof | grep deleted
6. File Corruption: A log file is showing junk characters. How will you check and recover it?
1. Check encoding:
file log.txt
2. View non-printable characters:
cat -v log.txt
3. Try opening with vi or nano:
vi log.txt
7. Crontab Not Running: A scheduled job is not executing. How do you debug?
1. Verify crontab entries:
crontab -l
2. Check logs:
sudo journalctl -u cron --since '5 minutes ago'
3. Ensure correct permissions:
chmod +x script.sh
8. Package Installation Failing: yum or apt is failing. How will you resolve it?
1. Clean package manager cache:
sudo apt clean or sudo yum clean all
2. Check network connection:
ping google.com
3. Update package lists:
sudo apt update or sudo yum update
9. How do you extend a partition without unmounting it?
1. Identify the partition:
lsblk
2. Resize the filesystem (ext4 example):
sudo resize2fs /dev/sdX
3. If using LVM:
sudo lvextend -r -L +10G /dev/mapper/vg-lv
10. What steps are needed to add a new disk to a Linux server?
1. Identify the new disk:
lsblk
2. Create a partition:
sudo fdisk /dev/sdX
3. Format it:
sudo mkfs.ext4 /dev/sdX1
4. Mount it:
sudo mount /dev/sdX1 /mnt
11. How to check which processes are writing to a file in real time?
sudo lsof +f -- /path/to/file
12. How to recover a deleted file in Linux?
1. If the file is still open by a process:
lsof | grep deleted
2. Recover using extundelete (if on ext4):
sudo extundelete /dev/sdX --restore-all
13. How to check disk I/O performance in Linux?
1. Check disk activity:
iostat -x 1
2. Monitor real-time disk usage:
iotop
14. A directory is taking too much space. How do you analyze it?
du -sh /path/to/directory/*
15. How do you remount a filesystem in read-write mode without rebooting?
sudo mount -o remount,rw /mnt
16. What happens when a file is deleted but is still in use by a process?
The space is not freed until the process releases it.
Identify the process and restart it:
lsof | grep deleted
17. How do you create and mount a swap file?
1. Create a swap file:
sudo fallocate -l 2G /swapfile
2. Secure permissions:
sudo chmod 600 /swapfile
3. Set up swap:
sudo mkswap /swapfile
4. Enable swap:
sudo swapon /swapfile
5. Make it permanent:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
18. How do you find large unused files across multiple partitions?
sudo find / -type f -size +1G -atime +30 -exec ls -lh {} +
19. How do you fix a corrupted file system using fsck?
1. Unmount the filesystem:
sudo umount /dev/sdX
2. Run fsck:
sudo fsck -y /dev/sdX
20. What is inode exhaustion, and how do you resolve it?
1. Check inode usage:
df -i
2. If inodes are full, delete small unused files.
21. You need to copy a huge file across servers. What's the fastest way?
1. Use rsync:
rsync -avz --progress file user@remote:/destination
2. Or scp with compression:
scp -C file user@remote:/destination
22. Your df -h and du -sh show different disk usage. Why?
Likely due to deleted files still held by a process.
Check with:
lsof | grep deleted
23. How do you find out which directory is consuming the most in /var?
du -sh /var/* | sort -rh | head -10