Complete CentOS Tutorial with Usage Examples

Table of Contents

1. Introduction to CentOS

CentOS (Community Enterprise Operating System) is a Linux distribution that historically provided a free, community-supported, and functionally compatible alternative to Red Hat Enterprise Linux (RHEL). It was widely used for servers due to its stability, long-term support, and robustness.

While CentOS Linux (the traditional rebuild of RHEL) has ceased, it has been succeeded by **CentOS Stream**, which is an upstream development branch for RHEL. Despite this shift, understanding CentOS principles is crucial for anyone working with RHEL-based systems in enterprise environments.

Key Characteristics:

2. CentOS vs. RHEL vs. CentOS Stream

It's important to clarify the relationship between these distributions:

For Enterprise Users: If you need a free, stable, RHEL-compatible OS for production without commercial support, alternatives like AlmaLinux or Rocky Linux (which are true RHEL rebuilds) are now commonly used. CentOS Stream is better suited for development, continuous integration, and exploring future RHEL features.

3. Installation Methods

CentOS (or CentOS Stream) is primarily used as a server OS, but can also be installed with a GUI for desktop use or learning.

Basic Installation Steps (VM Graphical Install):

  1. Boot your VM/computer from the CentOS Stream ISO.
  2. Choose "Install CentOS Stream".
  3. Select Language and Keyboard layout.
  4. Installation Summary:
    • Installation Destination: Select your virtual disk and configure partitioning (automatic is usually fine for beginners).
    • Network & Host Name: Configure network interface (e.g., enable Ethernet, set hostname).
    • Software Selection: Crucial step! Choose your base environment:
      • `Server with GUI`: Installs GNOME desktop and graphical tools.
      • `Server`: Minimal installation, command-line only.
      • `Minimal Install`: Even smaller, basic command-line utilities.
    • Time & Date: Set your timezone.
    • KDUMP: Leave enabled (default).
    • Security Policy: Can apply a security profile (optional).
  5. User Settings:
    • Root Password: Set a strong password for the `root` user.
    • User Creation: Create a standard user account (recommended). Check "Make this user administrator" if you want `sudo` privileges for this user.
  6. Click **Begin Installation**.
  7. After installation, **Reboot System**.
  8. Log in with your created user account (or `root`). If you installed with GUI, you'll see the desktop.

4. Basic Linux Commands (CLI)

The command-line interface (CLI) is fundamental for managing CentOS. Open a "Terminal" application (if using GUI) or log in via SSH (if headless server).

5. Linux File System Hierarchy (FHS)

CentOS adheres to the Filesystem Hierarchy Standard (FHS), which defines a standardized directory structure for Linux systems.

6. File Permissions and Ownership

Linux security relies heavily on file permissions and ownership to control access.

Permissions (Read, Write, Execute):

Permission Categories:

Viewing Permissions (`ls -l`):

ls -l my_script.sh
# Example Output: -rwxr-xr-- 1 user group 1234 Jul 19 10:00 my_script.sh

Changing Permissions (`chmod`):

Can use symbolic mode or octal (numeric) mode.

Changing Ownership (`chown`, `chgrp`):

Requires `sudo`.

7. User and Group Management

CentOS, like other Linux systems, supports multi-user environments.

User Accounts:

Common Commands (requires `sudo` for management):

Groups:

Configuration Files:

8. Package Management (DNF/YUM)

CentOS (and RHEL-based systems) uses the RPM Package Manager, with `dnf` (Dandified YUM) being the modern front-end for installing, updating, and removing software packages. Older CentOS 7 and earlier used `yum`.

Common `dnf` Commands:

9. Process Management

Managing running programs and tasks on your system.

10. Service Management (systemd)

CentOS (and other RHEL-based systems) uses `systemd` to manage system services (daemons) and boot processes.

11. Networking

CentOS uses `NetworkManager` or `systemd-networkd` for network configuration. Configuration files are typically in `/etc/sysconfig/network-scripts/`.

# Example ifcfg-eth0 for static IP:
# Use 'sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0' or 'sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0'

# DEVICE=eth0
# BOOTPROTO=static
# ONBOOT=yes
# IPADDR=192.168.1.100
# NETMASK=255.255.255.0
# GATEWAY=1992.168.1.1
# DNS1=8.8.8.8
# DNS2=8.8.4.4

# After editing:
# sudo systemctl restart NetworkManager # Or network.service on older versions

12. Storage Management

Managing disk space, partitions, and file systems on CentOS.

13. Shell Scripting Basics

Automate tasks by writing a series of commands in a file.

A. Creating a Script:

# Create a file named 'hello_centos.sh'
nano hello_centos.sh

# Add the following content:
#!/bin/bash
# This is a basic CentOS shell script

echo "Hello from CentOS!"
echo "Current date and time: $(date)"
echo "Disk usage of /home: $(df -h /home | awk 'NR==2{print $5}')"

B. Making Executable and Running:

chmod +x hello_centos.sh
./hello_centos.sh

C. Scripting Concepts:

14. Text Editors (Nano, Vim)

You'll frequently use command-line text editors in CentOS, especially for configuration files.

15. Security Best Practices (Firewall, SELinux)

CentOS integrates robust security features. Proper configuration is key.

16. Desktop Environments (GUI - Optional)

If you installed CentOS Stream as "Server with GUI", it comes with the GNOME desktop environment. If you installed a minimal version and want to add a GUI:

  1. Update your system:
    sudo dnf update -y
  2. Install the "Server with GUI" group:
    sudo dnf groupinstall "Server with GUI" -y
  3. Set the default target to graphical:
    sudo systemctl set-default graphical.target
  4. Reboot your system:
    sudo reboot
  5. Upon reboot, you should be greeted by the GNOME graphical login screen.
  6. To install other lightweight GUIs like XFCE (from EPEL repository):
    sudo dnf install epel-release -y
    sudo dnf --enablerepo=epel group install "Xfce" -y
    echo "exec /usr/bin/xfce4-session" >> ~/.xinitrc
    sudo systemctl set-default graphical.target
    startx # Or reboot
CentOS: The Enterprise Workhorse!

CentOS (and its successor CentOS Stream) provides a robust, stable, and secure foundation for server environments. Mastering its command-line tools, package management, service control, and security features like Firewalld and SELinux is crucial for any system administrator or DevOps professional. Practice regularly in a VM or cloud environment to solidify your skills.