Linux Interview Questions Part- 4

ยท

6 min read

Linux Interview Questions with Answers

36. What is a process in Linux, and how do you manage processes?

Answer:
A process is an instance of a running program. Each process has a unique Process ID (PID) and consumes system resources.

Common commands to manage processes:

  • View all processes:

      ps aux
    
  • Find a process by name:

      pgrep process_name
    
  • Kill a process:

      kill PID
    

    Use kill -9 PID for forceful termination.

  • Nice a process: To change a process's priority.

      nice -n 10 command  # Increase priority (lower value is higher priority)
    

37. What is the difference between tar, gzip, and zip?

Answer:

  • tar: A utility for creating archives. It combines multiple files into a single file (archive) but does not compress them by default.

      tar -cvf archive.tar /path/to/files
    
  • gzip: A compression utility that compresses files to save space. It can compress a single file.

      gzip file.txt
    
  • zip: A compression and file packaging utility that can compress multiple files and directories into a single ZIP file.

      zip archive.zip file1.txt file2.txt
    

38. What is the difference between TCP and UDP?

Answer:

  • TCP (Transmission Control Protocol):

    • Connection-oriented protocol, ensuring reliable communication.

    • Guarantees delivery of data packets in the same order they are sent.

    • Uses error-checking and acknowledgment.

    • Used for applications where data integrity is critical (e.g., HTTP, FTP).

  • UDP (User Datagram Protocol):

    • Connectionless protocol, providing faster but less reliable communication.

    • No guarantees for packet delivery, order, or integrity.

    • Used for applications where speed is essential, and some data loss is acceptable (e.g., DNS, VoIP).

39. How do you check disk usage in Linux?

Answer:
You can check disk usage using various commands:

  • df: Shows the amount of disk space used and available on filesystems.

      df -h  # Human-readable format
    
  • du: Estimates file and directory space usage.

      du -sh /path/to/directory  # Summary of a specific directory
      du -ah /path/to/directory  # All files and directories with sizes
    

40. Explain the use of the grep command.

Answer:
grep is a command-line utility for searching plain-text data for lines matching a regular expression.

  • Basic usage:

      grep "pattern" filename
    
  • Common options:

    • -i: Ignore case (e.g., grep -i "pattern" filename).

    • -r: Recursively search through directories (e.g., grep -r "pattern" /path/to/directory).

    • -v: Invert match, showing lines that do not match the pattern.

    • -n: Show line numbers in output.

41. What is the purpose of the /etc/fstab file?

Answer:
The /etc/fstab file is used to define how disk partitions, filesystems, and swap space should be mounted at boot time. It contains information about the device, mount point, filesystem type, options, and dump and pass options.

Example entry:

UUID=xxxx-xxxx-xxxx-xxxx  /mnt/data  ext4  defaults  0  2
  • UUID: Unique identifier for the filesystem.

  • /mnt/data: Mount point.

  • ext4: Filesystem type.

  • defaults: Mount options.

  • 0: Dump (backup).

  • 2: Filesystem check order.

42. How do you perform a system update on a Debian-based system?

Answer:
To update a Debian-based system (like Ubuntu):

  1. Update the package list:

     sudo apt update
    
  2. Upgrade the installed packages:

     sudo apt upgrade
    
  3. Full upgrade (handles changing dependencies):

     sudo apt full-upgrade
    

43. What is the purpose of the chroot command?

Answer:
The chroot command changes the apparent root directory for the current running process and its children. This is often used for system recovery, testing, and creating isolated environments.

Example:

sudo chroot /newroot /bin/bash

This command changes the root directory to /newroot, effectively isolating the process from the rest of the filesystem.

44. What are crontab and cron jobs?

Answer:
crontab is a file that defines a scheduled task (cron job) for execution at specified times and intervals. The cron daemon executes these tasks automatically.

View the crontab:

crontab -l

Edit the crontab:

crontab -e

Cron job format:

* * * * * command_to_run
  • The five asterisks represent minute, hour, day of month, month, and day of week.

45. Explain the purpose of rsync.

Answer:
rsync is a file synchronization and transfer tool. It efficiently transfers files and directories by only copying changes made since the last sync, making it ideal for backups and mirroring.

Basic usage:

rsync -avz source/ destination/
  • -a: Archive mode (preserves permissions, timestamps, etc.).

  • -v: Verbose output.

  • -z: Compress files during transfer.

46. How do you check system uptime in Linux?

Answer:
To check how long the system has been running:

uptime

This command displays the current time, how long the system has been running, how many users are logged on, and the system load averages.

47. What is the purpose of the env command?

Answer:
The env command is used to display the current environment variables or to run a command in a modified environment.

Example: Display environment variables:

env

Run a command with modified environment variables:

env VAR=value command

48. How do you find and kill processes by name?

Answer:
To find and kill processes by name, you can use the following commands:

  1. Find the PID using pgrep:

     pgrep process_name
    
  2. Kill the process:

     kill $(pgrep process_name)
    

    or use pkill:

     pkill process_name
    

49. What is the role of the /proc directory?

Answer:
The /proc directory is a virtual filesystem that provides a mechanism for the kernel to communicate with user space. It contains information about system processes, system configuration, and other kernel-related data.

Common files:

  • /proc/cpuinfo: Information about the CPU.

  • /proc/meminfo: Information about memory usage.

  • /proc/[PID]: Directory for a specific process, containing details about that process.

50. How do you configure a static IP address in Linux?

Answer:
To configure a static IP address in a Linux system, you typically edit the network configuration files or use network management tools.

For example, on a Debian-based system:

  1. Edit the network interfaces file:

     sudo nano /etc/network/interfaces
    
  2. Add or modify the following lines:

     auto eth0
     iface eth0 inet static
         address 192.168.1.100
         netmask 255.255.255.0
         gateway 192.168.1.1
    
  3. Restart networking services:

     sudo systemctl restart networking
    

51. What are the differences between a hard shutdown and a soft shutdown?

Answer:

  • Soft Shutdown: Involves using commands like shutdown, poweroff, or halt, allowing the system to close applications and processes gracefully. This minimizes the risk of data loss and filesystem corruption.

  • Hard Shutdown: Involves cutting power to the system abruptly (e.g., holding down the power button). This can lead to data loss and filesystem corruption, as processes are terminated immediately without saving state.

ย