Linux Interview Question and Answers
- How do you check available memory in Linux?
- Description: The
free
command shows the amount of free and used memory in your system. - Example:
free -h
-h
makes the output human-readable by using units like MB and GB.
- Description: The
- What are some common Linux distributions?
- Description: Linux distributions are different versions of the Linux operating system, each with unique features and software.
- Examples: Ubuntu, Debian, Fedora, CentOS, Red Hat Enterprise Linux.
- What is a symbolic link?
- Description: A symbolic link is a type of file that acts as a shortcut to another file or directory.
- Example:
ln -s /path/to/original /path/to/symlink
- This creates a symlink named
symlink
pointing tooriginal
.
- This creates a symlink named
- How do you find files on a Linux system?
- Description: The
find
command searches for files and directories based on specific criteria. - Example:
find /home/user -name "*.txt"
- This searches for all
.txt
files in the/home/user
directory.
- This searches for all
- Description: The
- What does the
grep
command do?- Description:
grep
searches for lines in files that match a given pattern. - Example:
grep "search_term" filename.txt
- This finds and displays lines in
filename.txt
that contain “search_term”.
- This finds and displays lines in
- Description:
- How can you start or stop services in Linux?
- Description: Services can be managed using
systemctl
in systems withsystemd
. - Examples:
systemctl start nginx
(starts the nginx service) andsystemctl stop nginx
(stops the nginx service).
- Description: Services can be managed using
- What is SSH and how is it used?
- Description: SSH (Secure Shell) provides a secure way to access and manage remote computers over an encrypted connection.
- Example:
ssh user@remote_host
- This command connects to
remote_host
using the usernameuser
.
- This command connects to
- How do you check disk usage in Linux?
- Description: The
df
command shows how much disk space is used and available on file systems. - Example:
df -h
-h
provides human-readable output with sizes in KB, MB, or GB.
- Description: The
- What is the role of the Linux kernel?
- Description: The kernel is the core of the Linux operating system, managing hardware resources and system services.
- How do you change file permissions in Linux?
- Description: The
chmod
command changes who can read, write, or execute a file. - Example:
chmod 755 filename
- This gives the owner full permissions and others read and execute permissions.
- Description: The
- What is the purpose of the
tar
command?- Description:
tar
is used to create, extract, and manage archive files. - Example:
tar -cvf archive.tar /path/to/directory
- This creates an archive named
archive.tar
containing the contents of/path/to/directory
.
- This creates an archive named
- Description:
- How do you compress and decompress files?
- Description: Use
gzip
to compress files andgunzip
to decompress them. - Examples:
- Compress:
gzip file.txt
- Decompress:
gunzip file.txt.gz
- Compress:
- Description: Use
- What is the purpose of the
cron
daemon?- Description:
cron
schedules tasks to run automatically at specified times or intervals. - Example: To run a script every day at midnight, you might add
0 0 * * * /path/to/script.sh
to the crontab.
- Description:
- How do you monitor system processes and resources?
- Description: The
top
command provides a real-time overview of system processes and resource usage. - Example:
top
- Displays a dynamic list of running processes and system resource usage.
- Description: The
- What is a firewall in Linux?
- Description: A firewall controls network traffic based on rules to protect your system from unauthorized access.
- Example: Using
iptables
to block traffic on port 22:iptables -A INPUT -p tcp --dport 22 -j DROP
.
- How do you create a new user in Linux?
- Description: The
useradd
command creates a new user account. - Example:
useradd -m newuser
- Creates a new user named
newuser
with a home directory.
- Creates a new user named
- Description: The
- How do you check the system’s IP address?
- Description: The
ip addr
command shows IP addresses assigned to network interfaces. - Example:
ip addr show
- Lists all network interfaces and their IP addresses.
- Description: The
- How do you kill a process in Linux?
- Description: Use
kill
to terminate processes using their PID, orkillall
to terminate by process name. - Examples:
kill 1234
(kills the process with PID 1234)killall firefox
(kills all processes namedfirefox
)
- Description: Use
- What is the purpose of the
iptables
command?- Description:
iptables
configures the Linux kernel firewall to manage network traffic. - Example:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
- Allows incoming traffic on port 80.
- Description:
- How do you mount a filesystem in Linux?
- Description: The
mount
command attaches a filesystem to a directory, making it accessible. - Example:
mount /dev/sdX1 /mnt
- Mounts the filesystem on
/dev/sdX1
to the/mnt
directory.
- Mounts the filesystem on
- Description: The
- What does the
rsync
command do?- Description:
rsync
synchronizes files and directories between locations efficiently. - Example:
rsync -av /source/ /destination/
- Copies files from
/source/
to/destination/
, preserving attributes.
- Copies files from
- Description:
- How do you view hardware information in Linux?
- Description: The
lshw
command provides detailed information about your system’s hardware. - Example:
lshw -short
- Displays a summary of hardware components.
- Description: The
- What is a shell in Linux?
- Description: The shell is a command-line interface for interacting with the operating system by typing commands.
- Example:
bash
is a common shell used in Linux systems.
- How do you check network configuration?
- Description: The
ifconfig
command (deprecated) andip
command display network settings. - Example:
ip a
- Shows details of all network interfaces.
- Description: The
- What does the
chroot
command do?- Description:
chroot
changes the root directory for a process, isolating it in a new environment. - Example:
chroot /path/to/newroot /bin/bash
- Changes the root directory to
/path/to/newroot
and starts a bash shell.
- Changes the root directory to
- Description:
- How do you search recursively for a string in files?
- Description: Use
grep
with the-r
option to search for a string in all files within a directory. - Example:
grep -r "search_term" /path/to/directory
- Searches for “search_term” in all files under
/path/to/directory
.
- Searches for “search_term” in all files under
- Description: Use
- What is the purpose of the
find
command?- Description:
find
searches for files and directories based on various criteria. - Example:
find /home/user -type f -name "*.jpg"
- Finds all
.jpg
files in the/home/user
directory.
- Finds all
- Description:
- How do you check CPU usage in Linux?
- Description: Use
top
,htop
, ormpstat
to view CPU usage and process information. - Example:
top
- Displays a dynamic, real-time list of processes and CPU usage.
- Description: Use
- How do you check file permissions in Linux?
- Description: Use
ls -l
to display file permissions along with other file details. - Example:
ls -l filename
- Shows permissions, ownership, and other details of
filename
.
- Shows permissions, ownership, and other details of
- Description: Use
- What is the role of the
grep
command?- Description:
grep
searches for lines in files that match a specified pattern and outputs them. - Example:
grep "error" logfile.txt
- Finds and displays lines in
logfile.txt
containing the word “error”.
- Finds and displays lines in
- Description: