10 CentOS Interview Questions and Answers
Prepare for your next technical interview with this guide on CentOS, featuring common questions and detailed answers to enhance your understanding.
Prepare for your next technical interview with this guide on CentOS, featuring common questions and detailed answers to enhance your understanding.
CentOS, a free and open-source Linux distribution, is widely recognized for its stability and reliability in enterprise environments. It is derived from the sources of Red Hat Enterprise Linux (RHEL) and offers a robust platform for managing servers, deploying applications, and ensuring security. CentOS is favored by many organizations for its long-term support and compatibility with a wide range of software.
This article provides a curated selection of interview questions designed to test your knowledge and proficiency with CentOS. By reviewing these questions and their detailed answers, you will be better prepared to demonstrate your expertise and problem-solving abilities in a technical interview setting.
The boot process in CentOS involves several stages:
To write a shell script that checks disk usage and sends an email alert if it exceeds 80%, you can use the df
command to get the disk usage and mail
command to send the email. Here is a simple example:
#!/bin/bash # Set the threshold THRESHOLD=80 # Get the disk usage percentage USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g') # Check if the usage is greater than the threshold if [ $USAGE -gt $THRESHOLD ]; then # Send an email alert echo "Disk usage is at ${USAGE}% on $(hostname)" | mail -s "Disk Usage Alert" [email protected] fi
To find all files larger than 100MB in the /var directory, you can use the find
command in Linux. The find
command is a powerful utility that allows you to search for files and directories based on various criteria.
find /var -type f -size +100M
In this command:
/var
specifies the directory to search in.-type f
ensures that only files are considered (not directories).-size +100M
filters files that are larger than 100MB.To install and configure Apache on CentOS, follow these steps:
1. Install Apache: Use the package manager yum
to install Apache.
2. Start and Enable Apache: Ensure that Apache starts on boot and is currently running.
3. Configure Firewall: Allow HTTP and HTTPS traffic through the firewall.
4. Verify Installation: Check that Apache is running by accessing the default web page.
5. Configure Apache: Edit the configuration files to customize the server settings.
Example commands:
# Install Apache sudo yum install httpd -y # Start Apache sudo systemctl start httpd # Enable Apache to start on boot sudo systemctl enable httpd # Allow HTTP and HTTPS through the firewall sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload # Verify Apache is running curl http://localhost
To configure Apache, you can edit the main configuration file located at /etc/httpd/conf/httpd.conf
. For example, to change the document root, you would modify the DocumentRoot
directive:
# Open the configuration file in a text editor sudo vi /etc/httpd/conf/httpd.conf # Change the DocumentRoot directive DocumentRoot "/var/www/html"
After making changes to the configuration file, restart Apache to apply the changes:
sudo systemctl restart httpd
#!/bin/bash # Variables SOURCE_DIR="/path/to/source" DEST_DIR="/path/to/destination" LOG_FILE="/path/to/logfile.log" # Function to log messages log_message() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE } # Check if source directory exists if [ ! -d "$SOURCE_DIR" ]; then log_message "Source directory does not exist: $SOURCE_DIR" exit 1 fi # Check if destination directory exists, create if not if [ ! -d "$DEST_DIR" ]; then mkdir -p "$DEST_DIR" if [ $? -ne 0 ]; then log_message "Failed to create destination directory: $DEST_DIR" exit 1 fi fi # Perform the backup using rsync rsync -av --delete "$SOURCE_DIR/" "$DEST_DIR/" if [ $? -eq 0 ]; then log_message "Backup successful from $SOURCE_DIR to $DEST_DIR" else log_message "Backup failed from $SOURCE_DIR to $DEST_DIR" exit 1 fi
RAID (Redundant Array of Independent Disks) is a technology that combines multiple physical disk drives into a single logical unit for data redundancy, performance improvement, or both. There are different levels of RAID, such as RAID 0, RAID 1, RAID 5, and RAID 10, each offering different benefits and trade-offs.
To configure a software RAID array on CentOS, you can use the mdadm
utility, which is a powerful tool for creating, managing, and monitoring RAID arrays.
Example:
# Install mdadm if not already installed sudo yum install mdadm # Create a RAID 1 array with two devices sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1 # Create a filesystem on the RAID array sudo mkfs.ext4 /dev/md0 # Mount the RAID array sudo mkdir -p /mnt/raid1 sudo mount /dev/md0 /mnt/raid1 # Save the RAID configuration sudo mdadm --detail --scan | sudo tee -a /etc/mdadm.conf # Update the initramfs sudo dracut -H -f /boot/initramfs-$(uname -r).img $(uname -r)
To monitor CPU usage and log it to a file every minute in CentOS, you can use a combination of the mpstat
command and a cron job. The mpstat
command provides CPU usage statistics, and cron can be used to schedule the logging at regular intervals.
First, create a script that logs the CPU usage:
#!/bin/bash # cpu_monitor.sh mpstat 1 1 | awk '/Average/ {print strftime("%Y-%m-%d %H:%M:%S"), $3, $4, $5, $6, $7, $8, $9, $10, $11}' >> /var/log/cpu_usage.log
Make the script executable:
chmod +x cpu_monitor.sh
Next, set up a cron job to run this script every minute:
crontab -e
Add the following line to the crontab file:
* * * * * /path/to/cpu_monitor.sh
This cron job will execute the cpu_monitor.sh
script every minute, logging the CPU usage to /var/log/cpu_usage.log
.
To set up a high-availability cluster using Pacemaker and Corosync on CentOS, follow these high-level steps:
pcs
command-line tool to configure the cluster, including setting up resources, constraints, and failover policies.Yum (Yellowdog Updater, Modified) and DNF (Dandified Yum) are package management tools used in CentOS to manage software packages. They allow users to install, update, and remove packages from the system repositories. DNF is the next-generation version of Yum and is used in CentOS 8 and later versions.
Yum and DNF handle dependencies automatically, ensuring that all required packages are installed or updated together. They also provide features like package groups, repository management, and transaction history.
Here are some common commands for using yum and dnf:
# Installing a package sudo yum install package_name sudo dnf install package_name # Updating a package sudo yum update package_name sudo dnf update package_name # Removing a package sudo yum remove package_name sudo dnf remove package_name # Listing installed packages yum list installed dnf list installed # Searching for a package yum search package_name dnf search package_name
To set up and manage virtual machines using KVM on CentOS, follow these steps:
1. Install KVM and related packages: First, ensure that your system supports hardware virtualization. Then, install KVM and related packages such as qemu-kvm
, libvirt
, and virt-manager
.
2. Enable and start the libvirtd service: Enable and start the libvirtd
service to manage the virtualization environment.
3. Create a virtual network: Use virsh
or virt-manager
to create a virtual network for your virtual machines.
4. Create and manage virtual machines: Use virt-install
or virt-manager
to create and manage virtual machines. You can specify various parameters such as CPU, memory, storage, and network configurations.
5. Manage virtual machines: Use virsh
commands to start, stop, and manage your virtual machines. You can also use virt-manager
for a graphical interface.
Example commands:
# Install KVM and related packages sudo yum install -y qemu-kvm libvirt virt-install bridge-utils virt-manager # Enable and start the libvirtd service sudo systemctl enable libvirtd sudo systemctl start libvirtd # Create a virtual network (example using default network) sudo virsh net-start default sudo virsh net-autostart default # Create a virtual machine (example) sudo virt-install \ --name centos-vm \ --ram 2048 \ --disk path=/var/lib/libvirt/images/centos-vm.img,size=20 \ --vcpus 2 \ --os-type linux \ --os-variant centos7.0 \ --network network=default \ --graphics none \ --console pty,target_type=serial \ --location 'http://mirror.centos.org/centos/7/os/x86_64/' \ --extra-args 'console=ttyS0,115200n8 serial'