Interview

15 Linux Administration Interview Questions and Answers

Prepare for your next IT interview with our comprehensive guide on Linux administration, featuring expert insights and practical questions.

Linux administration is a critical skill in the IT industry, underpinning the infrastructure of countless servers, networks, and systems worldwide. Known for its stability, security, and flexibility, Linux is the operating system of choice for many enterprises and tech giants. Mastery of Linux administration involves understanding system configurations, network management, security protocols, and automation tools, making it an indispensable asset for IT professionals.

This article offers a curated selection of interview questions designed to test and enhance your knowledge of Linux administration. By working through these questions, you will gain a deeper understanding of key concepts and practical skills, ensuring you are well-prepared for any technical interview in this field.

Linux Administration Interview Questions and Answers

1. Explain the boot process of a Linux system.

The boot process of a Linux system involves several stages:

  • BIOS/UEFI Initialization: The system initializes hardware components and performs a Power-On Self Test (POST) to ensure functionality.
  • Bootloader Stage: The bootloader, such as GRUB or LILO, loads the Linux kernel into memory.
  • Kernel Initialization: The kernel initializes hardware and mounts the root filesystem, then starts the init process.
  • Init/Systemd Process: This process manages system services, reading configuration files to determine which services to start.
  • Runlevel/Target Initialization: The system enters a specific runlevel or target, defining the machine’s state and running services.
  • User Login: The system presents a login prompt for user access.

2. Write a script to find and delete files older than 30 days in a specific directory.

To find and delete files older than 30 days in a specific directory, use the find command in a shell script. The -mtime option specifies the age of files, and -exec allows command execution on found files.

Example script:

#!/bin/bash

# Directory to search
DIRECTORY="/path/to/directory"

# Find and delete files older than 30 days
find "$DIRECTORY" -type f -mtime +30 -exec rm {} \;

In this script:

  • DIRECTORY is the path to search for old files.
  • -type f ensures only files are considered.
  • -mtime +30 specifies files older than 30 days.
  • -exec rm {} \; deletes each found file.

3. Describe how you would set up a cron job to run a backup script every Sunday at 2 AM.

Cron jobs in Linux schedule tasks to run automatically. To set up a cron job to run a backup script every Sunday at 2 AM, edit the crontab file using crontab -e. The format for a cron job entry is:

* * * * * command_to_run
- - - - -
| | | | |
| | | | +---- Day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)

For this task, the cron job entry would be:

0 2 * * 0 /path/to/backup_script.sh

To add this cron job:

  • Open the crontab file for editing:
    crontab -e
    ```</li>
    
    <li>Add the cron job entry:
    ```bash
    0 2 * * 0 /path/to/backup_script.sh
    ```</li>
    
    <li>Save and exit the editor.</li>
    </ul>
    
    <h4>4. How do you check disk usage on a Linux system? Provide a command example.</h4>
    
    To check disk usage on a Linux system, use the `df` command for a summary of available and used disk space. The `-h` flag makes the output human-readable.
    
    Example:
    
    ```bash
    df -h
    

    For detailed information about specific directories, use the du command:

    du -sh /path/to/directory
    

    5. Write a command to list all currently running processes and filter out those owned by a specific user.

    To list all currently running processes and filter those owned by a specific user, use the ps command with grep.

    Example:

    ps -u username
    

    Replace username with the actual username. The -u option specifies the user whose processes you want to display.

    6. Describe how you would secure SSH access on a Linux server.

    Securing SSH access on a Linux server involves several steps:

    1. Change the Default SSH Port: Modify the SSH port to a non-standard one to reduce automated attacks.

       sudo nano /etc/ssh/sshd_config
       # Change the line
       Port 22
       # To something like
       Port 2222
    

    2. Disable Root Login: Prevent root login via SSH for security.

       sudo nano /etc/ssh/sshd_config
       # Change the line
       PermitRootLogin yes
       # To
       PermitRootLogin no
    

    3. Use SSH Keys: SSH keys provide a more secure authentication method.

       ssh-keygen -t rsa -b 4096
       ssh-copy-id user@server
    

    4. Configure Firewall Rules: Allow only specific IP addresses to connect to the SSH port.

       sudo ufw allow from <trusted_ip> to any port 2222
       sudo ufw enable
    

    5. Enable Two-Factor Authentication (2FA): Add an extra layer of security with 2FA.

       sudo apt-get install libpam-google-authenticator
       google-authenticator
    

    6. Limit User Access: Restrict SSH access to specific users.

       sudo nano /etc/ssh/sshd_config
       # Add the line
       AllowUsers user1 user2
    

    7. Write a command to find all open network ports on a Linux system.

    To find all open network ports on a Linux system, use the netstat or ss command.

    Example using netstat:

    netstat -tuln
    

    Example using ss:

    ss -tuln
    

    8. Write a script to automate the installation of a package and its dependencies using a package manager.

    To automate the installation of a package and its dependencies, use a shell script with a package manager like apt-get.

    Example:

    #!/bin/bash
    
    # Update the package list
    sudo apt-get update
    
    # Install the package
    sudo apt-get install -y <package_name>
    

    Replace <package_name> with the desired package. The -y flag automatically confirms prompts.

    9. Describe how you would configure a firewall using iptables or firewalld.

    To configure a firewall, use either iptables or firewalld. Both tools manage packet filtering and NAT.

    Example using iptables:

    # Allow incoming SSH connections
    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
    # Block all other incoming connections
    sudo iptables -A INPUT -j DROP
    

    Example using firewalld:

    # Allow incoming SSH connections
    sudo firewall-cmd --zone=public --add-port=22/tcp --permanent
    
    # Reload the firewall
    sudo firewall-cmd --reload
    

    10. Write a command to display the last 50 lines of a log file and continuously update it.

    To display the last 50 lines of a log file and continuously update it, use the tail command with the -f option.

    tail -n 50 -f /path/to/logfile.log
    

    11. Write a script to check if a service is running and restart it if it is not.

    To check if a service is running and restart it if not, use a Bash script with systemctl.

    #!/bin/bash
    
    SERVICE_NAME="your_service_name"
    
    if systemctl is-active --quiet $SERVICE_NAME
    then
        echo "$SERVICE_NAME is running"
    else
        echo "$SERVICE_NAME is not running, restarting..."
        systemctl restart $SERVICE_NAME
    fi
    

    12. Write a command to change the ownership of all files in a directory recursively.

    To change the ownership of all files in a directory recursively, use the chown command with the -R option.

    Example:

    chown -R new_owner:new_group /path/to/directory
    
    • -R: Applies the command recursively.
    • new_owner: The new owner’s username.
    • new_group: The new group’s name (optional).
    • /path/to/directory: The target directory.

    Example:

    chown -R john:developers /home/john/projects
    

    13. Explain how you would troubleshoot a network connectivity issue on a Linux server.

    To troubleshoot a network connectivity issue, follow these steps:

    • Check Network Interface Configuration: Use ifconfig or ip addr to verify network interfaces.
    • Check Connectivity: Use ping to test connectivity to local and external hosts.
    • Check Routing Table: Use route or ip route to ensure correct routing.
    • Check DNS Configuration: Verify DNS settings in /etc/resolv.conf and use nslookup or dig for testing.
    • Check Firewall Rules: Use iptables or firewalld to check for blocking rules.
    • Check Network Services: Use netstat or ss to verify running services and ports.
    • Check Logs: Review system logs in /var/log/ for error messages.
    • Traceroute: Use traceroute to identify connection failures along the path.

    14. Write a script to parse a log file and extract entries from a specific date range.

    To parse a log file and extract entries from a specific date range, use a shell script with awk.

    #!/bin/bash
    
    # Usage: ./parse_log.sh logfile start_date end_date
    # Date format: YYYY-MM-DD
    
    logfile=$1
    start_date=$2
    end_date=$3
    
    awk -v start="$start_date" -v end="$end_date" '
    {
        log_date = substr($0, 1, 10)
        if (log_date >= start && log_date <= end) {
            print $0
        }
    }' "$logfile"
    

    15. Explain the differences in package management between Debian-based and Red Hat-based systems.

    Debian-based systems, like Ubuntu, use the Advanced Package Tool (APT) for package management, with .deb as the primary package format. Common commands include apt-get, apt-cache, and dpkg.

    Red Hat-based systems, such as CentOS and Fedora, use YUM or DNF for package management, with .rpm as the primary format. Common commands include yum, dnf, and rpm.

    Key differences include:

    • Package Format: Debian-based systems use .deb files, while Red Hat-based systems use .rpm files.
    • Package Management Tools: Debian-based systems use APT, whereas Red Hat-based systems use YUM or DNF.
    • Repositories: Both systems use repositories, but configurations differ.
    • Dependency Resolution: Both APT and YUM/DNF handle dependencies, but mechanisms differ.
Previous

10 C# Algorithm Interview Questions and Answers

Back to Interview
Next

10 Solaris L1 Interview Questions and Answers