Saturday, September 16, 2023

Setting Up an NFS Server on Red Hat 8 / CentOS 8 and Mounting on a Client

Introduction


Network File System (NFS) is a popular protocol that allows you to share files and directories between Unix-like systems over a network. In this guide, we will walk you through the process of creating an NFS server on Red Hat 8 or CentOS 8 and then mounting that NFS share on a client machine. NFS is an efficient and reliable way to share files and directories among systems in a network.


Network File System (NFS) is a distributed file system protocol originally developed by Sun Microsystems (Sun) in 1984,It allows you to mount your local file systems over a network and remote hosts to interact with them as they are mounted locally on the same system. NFS, like many other protocols, builds on the Open Network Computing Remote Procedure Call (ONC RPC) system. 


    Feature & Benefits of NFS Server 

    • NFS server allows to mount locally from  remote server files.
    • Fast and reliable services 
    •  NFS can be configure as centralized storage .
    • Users get their data irrespective of physical location.
    • File sync real time . 
    • Can be secured with Firewalls.

    NFS Configuration files 
    /etc/exports  : This is main configuration file which contain information of all shared Directory .

    /etc/fstab : This is not NFS configuration but we use this file to mount share permanently. 

    Before you begin, make sure you have the following:


    1. A Red Hat 8 or CentOS 8 server machine.

    2. A client machine running a compatible operating system (e.g., Red Hat, CentOS, or any other Linux distribution).

    3. Root or sudo access to both the server and client machines.

    Server IP - 192.168.1.50

    Client IP - 192.168.1.100


    Step 1: Install NFS Server

    On the server machine (Red Hat 8 or CentOS 8), you need to install the NFS server software. You can do this using the following command:

    Note: You must use root login to perform this task else you can use sudo before commands


    #dnf install nfs-utils

    This command will install the necessary NFS packages.


    Step 2: Configure the NFS Export

    Next, you need to define which directories you want to share over NFS. Edit the NFS exports file by opening it in a text editor:

    #vi /etc/exports 


    In the exports file, add a line for each directory you want to share. The line should follow this format:

    /path/to/shared/directory client_ip(options)


    For example, if you want to share the directory `/data` with a client at IP address `192.168.1.100` with read-write access, add the following line:

    /data 192.168.1.100(rw,sync,no_root_squash,no_all_squash)

    Save and close the file.


    Step 3: Export the NFS Share

    After configuring the NFS exports, you need to export the shares and restart the NFS server:


    #sudo exportfs -a

    #sudo systemctl restart nfs-server


    Step 4: Configure Firewall Rules

    To allow NFS traffic through the firewall, you need to open the necessary ports:

    #firewall-cmd --permanent --zone=public --add-service=nfs

    #firewall-cmd --permanent --zone=public --add-service=mountd

    #firewall-cmd --permanent --zone=public --add-service=rpc-bind

    #firewall-cmd --reload 



    Step 5: Install NFS Client Utilities (On the Client Machine)

    On the client machine, you need to install the NFS client utilities if they are not already installed:


    #dnf install nfs-utils


    Step 6: Mount the NFS Share (On the Client Machine)

    Now, you can mount the NFS share from the server to the client. Create a directory on the client to serve as the mount point:

    #mkdir /mnt/nfs-share


    Then, mount the NFS share:  

     You can mount the NFS share temporarily or add an entry to `/etc/fstab` for automatic mounting at boot. Here are both options:

    To see available NFS share on server 

    #showmount -e 192.168.1.50


       Temporary Mount:

         To temporarily mount the NFS share, use the following command. Replace `server_ip` with the IP address or hostname of your NFS server and `/data` with the shared directory path on the NFS server:


    #mount -t nfs server_ip:/path/to/shared/directory /mnt/nfs-share

    Replace `server_ip` with the IP address of your NFS server and `/path/to/shared/directory` with the directory you want to mount from the server.

    like: 

    #mount -t 192.168.1.50:/data  /mnt/nfs-share



    Permanent Mount (via /etc/fstab):


         To configure the NFS share to be mounted automatically at boot, you can add an entry to `/etc/fstab`. Edit the file using a text editor:


    #vi /etc/fstab

         

         Add the following line to the end of the file, replacing `server_ip:/data` with the NFS server and directory path, and `/mnt/nfs-share` with your desired mount point:


       

    192.168.1.50:/data /mnt/nfs-share nfs defaults 0 0

            

         Save the file and exit the text editor.


    Step 7:  Mount the NFS Share and Verify

       If you added the entry to `/etc/fstab`, you can mount all entries in `/etc/fstab` with the following command:

    #mount -a

       

    You can verify that the NFS share is mounted on the client by listing the contents of the mount point:


    ls /mnt/nfs-share


    If you see the files and directories from the NFS share, congratulations! You've successfully set up an NFS server on Red Hat 8 or CentOS 8 and mounted it on a client machine.


     Unmount the NFS Share** (if needed):

       To unmount the NFS share when you no longer need access to it, you can use the `umount` command:


    #umount /mnt/nfs-share


    Tip: Make sure you remove entry from /etc/fstab to unmount it permanently 


    8. **Access the NFS Share**:


       You can now access the NFS share as if it were a local directory. For example, you can use `cd /mnt/nfs-share` to navigate to the NFS share and manipulate files.


    #ls /mnt/nfs-share


    That's it! You've successfully set up an NFS client on your Red Hat 8 or CentOS 8 system to access an NFS share from an NFS server.


    Conclusion


    Setting up an NFS server on Red Hat 8 or CentOS 8 and mounting it on a client machine is a straightforward process. NFS provides a convenient way to share files and directories across a network, making it a valuable tool for networked Linux systems.

    How to Reset the Root Password in RedHat/CentOs 8 from Emergency Mode


    Introduction

    In the world of Linux system administration, there may come a time when you forget the root password for your Red Hat 8 server. While this can be a daunting situation, there's no need to panic. Red Hat provides a robust set of tools that allow you to reset the root password even if you can't log in. In this blog post, we'll walk you through the process of resetting the root password in Red Hat 8 from emergency mode.


    What is Emergency Mode?

    Emergency mode is a special boot mode in Red Hat and other Linux distributions that provides a minimal environment to repair or recover a system when it's in an unusable state. It allows you to gain access to the system's filesystem, even if you don't know the root password. This makes it the perfect starting point for resetting the root password.

    Note: This guide assumes you have physical or console access to the server or virtual machine. If you're working on a remote server without console access, you may need to contact your hosting provider or data center for assistance.


    Here's how to reset the root password in Red Hat 8 / CentOS 8 from emergency mode:


    1. Boot into Emergency Mode:

       - Start by rebooting your Red Hat 8 server or virtual machine.

       - When the GRUB bootloader menu appears during boot, select the Red Hat 8 entry you want to boot into.

       - Press the `e` key to edit the boot parameters.


    2. Edit the Boot Parameters:

       - Locate the line that starts with `linux` or `linuxefi`.

       - Append `rd.break enforcing=0` to the end of the line. This will boot the system into emergency mode without enforcing SELinux policies.

       Example:

     linux /vmlinuz-4.18.0-305.el8.x86_64 root=/dev/mapper/rhel-root ro crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet rd.break enforcing=0

       

    3. Mount the Filesystem:

       - Press `Ctrl` + `x` or `Ctrl` + `b` to boot into the modified kernel command line.

       - The system will boot into emergency mode, and you'll be dropped into a shell. The root filesystem is mounted as read-only.

       To remount it as read-write, run the following command:

     #mount -o remount,rw  /sysroot


    4. Reset the Root Password:

       - Change the root password using the `passwd` command:

     #chroot /sysroot

     #passwd

       You will be prompted to enter a new root password.


    5. SELinux Relabeling:

       - To ensure SELinux labels are correctly applied, execute the following command:

     #touch /.autorelabel

    6. Exit Emergency Mode:

       - Exit the emergency shell:

     #exit

       

    7. Reboot Your System:

       - Reboot the system:

    reboot


    After rebooting, your Red Hat 8 system should have the root password reset. You can now log in with the new password you set during step 4.


    Conclusion

    Forgetting the root password can be a stressful situation, but with Red Hat's rescue mode, you can regain access to your system and reset the password securely. Remember to keep your passwords in a secure place to prevent this from happening in the future. Always practice good password hygiene and consider using a password manager to keep track of your credentials.

    Sunday, September 10, 2023

    How to Install MySQL Server on RedHat/CentOS 8 and Create Database, MySQL User, and Table

    MySQL is one of the most popular open-source relational database management systems used for storing and managing data. If you're running RedHat or CentOS 8 and need to set up MySQL for your application or project, this guide will walk you through the installation process, creating a database, a MySQL user, and a table.


    Step 1: Update the System

    Before you start installing MySQL, it's a good practice to ensure your system is up to date. Open a terminal and run the following commands:

    sudo dnf update

    This will update the package list and upgrade existing packages on your CentOS 8 system.


    Step 2: Install MySQL Server

    CentOS 8's default package manager is `dnf`. You can easily install MySQL Server using the following command:


    sudo dnf install mysql-server


    This command will download and install MySQL Server and its dependencies. Once the installation is complete, start the MySQL service and enable it to start on boot:


    sudo systemctl start mysqld

    sudo systemctl enable mysqld


    Step 3: Secure MySQL Installation

    For security purposes, MySQL comes with a script to help you secure your installation. Run the following command and follow the prompts:


    sudo mysql_secure_installation


    This script will allow you to set a root password, remove anonymous users, disallow root login remotely, remove the test database, and reload the privilege tables.


    Step 4: Log in to MySQL

    Now that you have MySQL installed and secured, you can log in as the root user:

    sudo mysql -u root -p

    You'll be prompted to enter the root password you set during the secure installation process.


    Step 5: Create a Database

    Let's create a new database. Replace `<database_name>` with your desired database name:

    CREATE DATABASE <database_name>;

    For example, to create a database named "mydb," use this command:

    CREATE DATABASE mydb;

    SHOW DATABASES;


    Step 6: Create a MySQL User


    After creating the database, you can create a MySQL user and grant privileges to it. Replace `<username>` and `<password>` with your preferred username and password:

    CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';

    For example, to create a user named "myuser" with the password "mypassword," use this command:

    CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';

    Now, grant the user privileges on the database you created:

    GRANT ALL PRIVILEGES ON <database_name>.* TO '<username>'@'localhost';

    Using the example user and database from above, grant privileges as follows:

    GRANT ALL PRIVILEGES ON mydb.* TO 'myuser'@'localhost';

    Don't forget to flush the privileges to apply the changes:

    FLUSH PRIVILEGES;


    To change mysql user password 

    Use mysql;

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';


    Step 7: Create a Table

    With your database and user in place, you can now create tables and start storing data. Let's create a simple table for demonstration. Replace `<table_name>` with your desired table name and define the table structure:


    CREATE TABLE <table_name> (

       employee_id INT AUTO_INCREMENT PRIMARY KEY,
       first_name VARCHAR(255),
       last_name VARCHAR(50) NOT NULL,
       email VARCHAR(100) UNIQUE
       hire_date DATE,
       salary DECIMAL(10, 2)
    );


    For example, to create a table named "employees" with columns for employee_idfirst_name, last_name, email, hire_date and salary:


    CREATE TABLE employees (

        employee_id INT AUTO_INCREMENT PRIMARY KEY,

        first_name VARCHAR(50) NOT NULL,

        last_name VARCHAR(50) NOT NULL,

        email VARCHAR(100) UNIQUE,

        hire_date DATE,

        salary DECIMAL(10, 2)

    );




    To view all available tables in database 


    SHOW TABLES;



    To see table's field formats.


    DESCRIBE table_name;



    To view contant of a tables in database 


    SELECT * FROM table_name;




    8. Delete a Database:


    To delete a database, you can use the MySQL command line:


    DROP DATABASE your_database_name;


     9. Delete a Table:


    To delete a table, you can use the MySQL command line. Make sure you're in the correct database:


    USE your_database_name;


    Then, drop the table:


    DROP TABLE your_table_name;




    10. Change MySQL User Password:


    To change the password for a MySQL user, use the following command:

    sql



    ALTER USER 'username'@'localhost' IDENTIFIED BY 'new_password';



    Replace 'username' with the actual username and 'new_password' with the new password.



    11. Backup and Restore:

    • To create a MySQL backup (dump):

      mysqldump -u <username> -p <database_name> > backup.sql

    • To restore a backup:

      mysql -u <username> -p <database_name> < backup.sql


    12. Optimize and Repair Tables:

    • To optimize all tables in a database:

    • OPTIMIZE TABLE <table_name>;

    • To repair all tables in a database:

      REPAIR TABLE <table_name>;


    TIP : If you want to give graphical access of mysql server to the users you can install PhpMyAdmin or Adminer also MySQL Workbench client  for Developer  


    Now, you have a MySQL server up and running on your CentOS 8 system, a database created, a MySQL user set up, and a table ready to store your data.

    Remember to replace placeholders with your actual database, user, password, and table names as needed. MySQL is a versatile and powerful database system, and you can continue to build and expand your database as your application or project grows.





    Saturday, September 9, 2023

    Managing Logical Volume Management (LVM) on RedHat 8 / CentOS 8

    Introduction


    Logical Volume Management (LVM) is a powerful storage management tool that allows for flexible and efficient management of storage devices on Red Hat 8 and CentOS 8 systems. LVM provides features like volume resizing, snapshot creation, and striping, making it an essential component for system administrators and IT professionals. In this blog, we'll dive into the fundamentals of LVM configuration and management on Red Hat 8 and CentOS 8.


    Understanding LVM Components

    Before we start configuring LVM, let's understand the essential components of LVM:

    1. Physical Volumes (PVs): These are physical storage devices like hard drives, SSDs, or partitions. PVs are the building blocks of LVM.

    2. Volume Groups (VGs): A VG is a collection of one or more PVs. It acts as an intermediary layer between PVs and logical volumes.

    3. Logical Volumes (LVs): LVs are virtual partitions created from VGs. They can be thought of as equivalent to regular disk partitions.

    Now, let's explore how to configure and manage LVM on Red Hat 8 / CentOS 8.


     Step 1: Installation

    If LVM is not already installed, you can install it using the following command:

    sudo dnf install lvm2


    Step 2: Creating Physical Volumes (PVs)


    To use LVM, you first need to designate one or more disks or partitions as Physical Volumes. Use the `pvcreate` command for this purpose. For example:


    sudo pvcreate /dev/sdb1 /dev/sdc1


    Step 3: Creating a Volume Group (VG)


    Once you have PVs, you can create a Volume Group. This step groups together your PVs into a logical unit. For example:


    sudo vgcreate myvg /dev/sdb1 /dev/sdc1


    Step 4: Creating Logical Volumes (LVs)


    With your Volume Group set up, you can now create Logical Volumes. These are the virtual partitions that you can use to store data. Here's how to create an LV:


    sudo lvcreate -n mylv -L 10G myvg


    This command creates an LV named `mylv` with a size of 10 gigabytes within the `myvg` Volume Group.


    Step 5: Formatting and Mounting LVs

    After creating an LV, you can format it with a filesystem of your choice (e.g., ext4, xfs) and then mount it to a directory:


    sudo mkfs.ext4 /dev/myvg/mylv

    sudo mount /dev/myvg/mylv /mnt/mylv


    Step 6: Extending LVs


    One of the significant advantages of LVM is the ability to easily extend your storage. If you need to increase the size of an LV, you can do so without disrupting your system. Here's how you can extend an LV:


    # Extend the LV by 5GB

    sudo lvextend -L +5G /dev/myvg/mylv


    For More Details


    # Resize the filesystem to utilize the additional space

    sudo resize2fs /dev/myvg/mylv


    Step 7: Snapshots and Other Advanced Features

    LVM offers advanced features like snapshots, mirroring, and striping. Snapshots allow you to create point-in-time copies of your LVs for backup or testing purposes. 


    Step 8: Monitoring and Maintenance

    Regularly monitor your LVM setup using tools like `lvs`, `vgs`, and `pvs`. These commands provide information about LVs, VGs, and PVs, respectively. Additionally, consider setting up regular backups to protect your data.


    Conclusion

    Logical Volume Management (LVM) is a versatile tool that simplifies storage management on Red Hat 8 and CentOS 8 systems. By understanding the fundamental components and following the steps outlined in this guide, you can create, manage, and expand storage resources efficiently. Whether you are a system administrator or an IT enthusiast, LVM is an essential skill to master for effective storage management in Linux environments.





    Mastering Cron Jobs in Linux: A Comprehensive Guide with Examples

    Introduction: Cron jobs are an indispensable tool in the world of system administration and automation. They allow users to schedule tasks t...