Linux Interview Questions Part- 2
Top Linux Interview Questions with Answers
1. What is Linux?
Answer:
Linux is an open-source operating system based on the Unix kernel. It is the core software that manages hardware resources and provides essential services to applications. The term "Linux" often refers to the entire Linux OS (kernel + software), though technically, Linux is just the kernel. The operating system includes utilities, graphical environments, and other software.
2. Explain the File System Hierarchy in Linux.
Answer:
The Linux file system is organized in a hierarchical structure, with /
as the root directory. Common directories include:
/bin
: Essential user command binaries (e.g.,ls
,cp
)./home
: Users' home directories./etc
: Configuration files./var
: Variable data like logs./usr
: User programs and system utilities./tmp
: Temporary files created by system processes and users.
3. What are the basic file permissions in Linux?
Answer:
Linux permissions are divided into three types: r
(read), w
(write), and x
(execute), and are set for three classes: user (owner), group, and others. For example, rwxr-xr--
means the owner has full permission, the group can read and execute, and others can only read.
4. How do you change file permissions?
Answer:
Use
chmod
to change file permissions. For example:chmod 755 file.txt
This grants the owner full permissions (7), the group read and execute (5), and others read and execute (5).
To change the ownership of a file, use
chown
:chown user:group file.txt
This assigns ownership of
file.txt
to the specified user and group.
5. What are the differences between sudo
and su
?
Answer:
sudo
: Allows a permitted user to execute a command as the superuser or another user. It’s temporary and doesn't change the current user’s session.su
: Switches to the superuser (or another user) and starts a new shell session as that user.
6. What is a shell?
Answer:
A shell is a command-line interface that allows users to interact with the operating system. Common shells include:
Bash: Default on most Linux distributions.
Zsh: An extended shell with more features.
To change the default shell, use the chsh
command:
chsh -s /bin/zsh
7. Explain the purpose of environment variables in Linux.
Answer:
Environment variables store information that can be used by the shell or applications. Common variables:
PATH
: Directories the shell searches for executable files.HOME
: The current user's home directory.USER
: The current username.
To create an environment variable:
export MY_VAR="Hello"
8. How do you check the current running processes?
Answer:
To view running processes:
ps
: Displays active processes.ps aux
top
: Displays a dynamic real-time view of system processes.htop
: An interactive version oftop
(needs to be installed).pgrep
: Search for a specific process by name.
9. What is the difference between hard links and soft (symbolic) links?
Answer:
Hard link: Directly points to the inode (data) of the file. Changes in the original file reflect in the hard link, and both share the same data.
Symbolic (soft) link: Points to the filename. It’s more like a shortcut. If the original file is deleted, the symlink becomes broken.
To create:
Hard link:
ln original.txt hardlink.txt
Symbolic link:
ln -s original.txt symlink.txt
10. Explain package management in Linux.
Answer:
Linux uses package managers to install, update, and remove software:
Debian-based (Ubuntu, etc.):
apt
sudo apt install package_name sudo apt update && sudo apt upgrade sudo apt remove package_name
Red Hat-based (CentOS, etc.):
yum
ordnf
sudo yum install package_name sudo yum update
11. How do you search for files in Linux?
Answer:
find
: Searches for files based on various criteria (name, size, etc.).find /home/user -name "*.txt"
locate
: Searches an indexed database of files.locate filename
12. How do you view the content of a file?
Answer:
cat
: Concatenates and displays the contents of a file.less
andmore
: Pagers to view large files one screen at a time.head
: Displays the first few lines.tail
: Displays the last few lines. Usetail -f
to follow a file in real time.tail -f /var/log/syslog
13. What is cron
and how do you schedule jobs using it?
Answer:cron
is used to schedule recurring tasks in Linux. A cron job consists of five fields (minute, hour, day, month, day of the week) followed by the command:
30 2 * * * /path/to/script.sh
This runs script.sh
every day at 2:30 AM.
To manage cron jobs:
View jobs:
crontab -l
Edit jobs:
crontab -e
Remove jobs:
crontab -r
14. What are system logs and where are they stored?
Answer: System logs are stored in the /var/log/
directory. Common log files include:
/var/log/syslog
or/var/log/messages
: General system log./var/log/auth.log
: Authentication logs.
Use journalctl
to view systemd logs:
journalctl -xe
15. What is the difference between df
and du
commands?
Answer:
df
: Displays the available disk space on file systems.df -h
The
-h
flag shows human-readable sizes (e.g., MB, GB).du
: Shows the disk usage of files and directories.du -sh /path/to/directory
16. How do you manage services in Linux?
Answer:
Use systemctl
for managing system services (on systemd
systems):
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl status nginx
For older systems without systemd
, you may use service
:
sudo service nginx start
17. What is the difference between a terminal emulator and a shell?
Answer:
A terminal emulator (like GNOME Terminal, Konsole) is a program that emulates a physical terminal and allows users to interact with the shell.
A shell (like Bash or Zsh) is a command-line interpreter that runs commands.
18. How do you compress and extract files in Linux?
Answer:
tar
: Create or extract tar archives.tar -czvf archive.tar.gz /path/to/files tar -xzvf archive.tar.gz
gzip
andbzip2
: Compress individual files.gzip file.txt bzip2 file.txt
19. How do you redirect output in Linux?
Answer:
>
: Redirects output to a file, overwriting it.ls > file.txt
>>
: Appends output to a file.ls >> file.txt
2>
: Redirects error output.ls non_existing_file 2> error.txt
20. What is SSH and how do you use it to connect to a remote server?
Answer:
SSH (Secure Shell) is used to securely connect to a remote server. Use the ssh
command:
ssh user@hostname
To copy files, use scp
:
scp localfile user@hostname:/remote/path