Linux Interview Questions Part- 3

ยท

5 min read

Linux Interview Questions with Answers

21. Explain how networking works in Linux.

Answer:
Networking in Linux is managed using a variety of tools to configure network interfaces, check connectivity, and troubleshoot issues.

  • Check interfaces:

      ip a
    

    or the older command:

      ifconfig
    
  • Ping a remote host:

      ping google.com
    
  • Check open ports:

      netstat -tuln
    

    or:

      ss -tuln
    

22. What is iptables, and how is it used in Linux?

Answer:
iptables is a command-line firewall utility that allows administrators to configure rules to filter and manipulate network traffic. It works with tables and chains to define what happens to incoming, outgoing, or forwarded packets.

Example to block incoming traffic on port 80:

sudo iptables -A INPUT -p tcp --dport 80 -j DROP
  • -A INPUT: Append a rule to the input chain.

  • -p tcp: Specify TCP protocol.

  • --dport 80: Specify port 80 (HTTP).

  • -j DROP: Drop the packets.

23. What is a Linux kernel module?

Answer:
A Linux kernel module is a piece of code that can be loaded into or unloaded from the kernel dynamically, without needing to reboot the system. Kernel modules extend the functionality of the kernel, such as adding support for new hardware devices.

  • List loaded modules:

      lsmod
    
  • Load a module:

      sudo modprobe module_name
    
  • Remove a module:

      sudo rmmod module_name
    

24. What is LVM (Logical Volume Management)?

Answer:
LVM is used to manage disk storage flexibly. It allows you to create, resize, and manage disk volumes dynamically.

  • Steps to create and manage LVM volumes:

    1. Create a physical volume (PV):

       sudo pvcreate /dev/sdb
      
    2. Create a volume group (VG):

       sudo vgcreate my_vg /dev/sdb
      
    3. Create a logical volume (LV):

       sudo lvcreate -L 10G -n my_lv my_vg
      
    4. Format and mount the logical volume:

       sudo mkfs.ext4 /dev/my_vg/my_lv
       sudo mount /dev/my_vg/my_lv /mnt
      

25. How do you monitor system performance in Linux?

Answer:
Linux provides various tools to monitor CPU, memory, disk, and network performance.

  • vmstat: Displays system processes, memory, and CPU usage.

      vmstat 2 5  # Updates every 2 seconds, for 5 iterations.
    
  • iostat: Monitors disk I/O performance.

      iostat -x 2 5
    
  • free: Shows free and used memory.

      free -h
    
  • sar: Collects and reports system activity.

      sar -u 2 5
    
  • top or htop: Shows real-time process and resource usage.

26. What is SELinux, and how does it work?

Answer:
SELinux (Security-Enhanced Linux) is a security architecture integrated into the Linux kernel that provides mandatory access control (MAC). It restricts users and applications based on defined security policies.

  • Check the current SELinux status:

      sestatus
    
  • Change SELinux mode: To temporarily change SELinux to permissive (disables enforcing of policies but logs violations):

      sudo setenforce 0
    
  • Configuration: To permanently change the mode, edit /etc/selinux/config.

27. How do you create a user and group in Linux?

Answer:

  • Create a new user:

      sudo useradd -m newuser
      sudo passwd newuser
    

    The -m option creates a home directory for the user.

  • Add a user to a group:

      sudo usermod -aG groupname username
    

    The -aG option appends the user to the specified group.

  • Delete a user:

      sudo userdel -r username
    

    The -r option removes the user's home directory.

28. How do you manage disk partitions in Linux?

Answer:

  • View existing partitions:

      sudo fdisk -l
    
  • Create or modify partitions using fdisk or parted: To create a new partition with fdisk:

      sudo fdisk /dev/sdb
    

    Follow the interactive prompts to create a new partition.

  • Format the partition:

      sudo mkfs.ext4 /dev/sdb1
    
  • Mount the partition:

      sudo mount /dev/sdb1 /mnt
    

29. What is the difference between NFS and Samba?

Answer:

  • NFS (Network File System): A protocol that allows users to share directories and files over a network on Linux/Unix systems. It's native to Unix systems and commonly used for sharing directories between Linux servers.

    • NFS is faster for Unix/Linux systems but not as flexible for Windows clients.
  • Samba: A suite of programs that allows Linux machines to share files with Windows clients by implementing the SMB/CIFS protocol.

    • Samba is typically used when Linux systems need to share files with Windows systems.

30. How do you manage software services across runlevels or targets in Linux?

Answer:
In modern systemd-based systems, services are managed using targets (which replaced traditional runlevels). Each target represents a system state, such as multi-user or graphical mode.

  • List all systemd targets:

      systemctl list-units --type=target
    
  • Switch to a different target (for example, to boot into a multi-user text mode):

      sudo systemctl isolate multi-user.target
    
  • Enable a service to start on boot:

      sudo systemctl enable nginx
    
  • Disable a service:

      sudo systemctl disable nginx
    

31. What is the difference between nohup and & when running a command in Linux?

Answer:

  • &: Runs a command in the background. For example:

      command &
    

    The command runs in the background, but it will terminate if the terminal is closed.

  • nohup: Runs a command immune to hangups (ignore the HUP signal) so that it continues running even after you log out or close the terminal.

      nohup command &
    

32. How do you troubleshoot boot issues in Linux?

Answer:
If a Linux system fails to boot, follow these steps:

  • Check boot logs:

      journalctl -b
    

    This displays the logs from the last boot.

  • Use dmesg: This command shows kernel messages, which can provide insights into hardware-related boot issues.

      dmesg | less
    
  • Rescue or Recovery mode: Most distributions allow you to boot into a recovery mode where you can access a limited shell to troubleshoot the system.

  • Grub troubleshooting: Edit boot parameters in GRUB to boot into single-user mode or disable problematic services.

ย