Linux Tutorials – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Tue, 17 Jan 2023 02:28:25 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 Backing Up Your Linux System with Rsync: A Step-by-Step Guide https://tecadmin.net/backup-linux-system/ https://tecadmin.net/backup-linux-system/#respond Tue, 17 Jan 2023 02:28:25 +0000 https://tecadmin.net/?p=23062 For many computer users, the most stressful part of working with a Linux system is having to back up their data. The good news is that there is a simple solution to this problem: set up an automatic rsync backup script that will automatically keep your data safe. In this article, we will go over [...]

The post Backing Up Your Linux System with Rsync: A Step-by-Step Guide appeared first on TecAdmin.

]]>
For many computer users, the most stressful part of working with a Linux system is having to back up their data. The good news is that there is a simple solution to this problem: set up an automatic rsync backup script that will automatically keep your data safe. In this article, we will go over the tools and steps that you need to take to set up an automated backup system on a Linux system with rsync. You will learn how to use rsync to automatically create backups of files, how to keep these backups up-to-date, and how to restore them in the event of data loss or corruption.

If you regularly perform backups on your Linux system, chances are you already know about rsync, a command-line utility that can be used to back up and synchronize files and directories. However, if you’re new to rsync, it might come as a surprise that this simple command is capable of backing up your entire Linux system. In this guide, we’ll show you how to use rsync to back up your Linux system using different strategies.

Steps to Backup Your Linux System

  1. Prepare a Backup Device
  2. To make a full backup of the system, you need a device that has much space to keep all files. A backup device can be a locally attached drive, network device, or cloud storage like Amazon S3, Azure Spaces, etc.

    Create a directory to store the backup on the backup device. Assuming you have attached a separate disk in your local machine mounted at /mnt directory.

    mkdir /mmnt/full-backup 
    

  3. Install Rsync Utility
  4. Rsync is a command line utility that helps to synchronize content between two directories. They either exist on the local system or one can be the remote. You can quickly install on using the default package manager on most modern Linux distributions. To install Rsync on Debian-based systems, type:

    sudo apt install rsync 
    

  5. Backup Your System
  6. You can run the command directly to make a backup of the complete system. For example, to create a backup of the system to the “/mnt/full-backup” directory, run the following command.

    sudo rsync -aAXv / --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/full-backup 
    

    The above command will backup the entire root (/) directory, excluding /dev, /proc, /sys, /tmp, /run, /mnt, /media, and /lost+found directories, and save the data in /mnt/full-backup folder. Here:

    The `-aAXv` options are used so that the files are transferred in “archive” mode, which ensures that symbolic links, devices, permissions, ownerships, modification times, ACLs, and extended attributes are preserved.

  7. Automate the Backup
  8. It’s good practice to schedule automatic backups. You can simply add the above command in crontab, or write them in a shell script and then schedule the script.

    #!/usr/bin/evn bash
    
    BACKUP_PATH="/mnt/full-backup"
    EXCLUDE_DIR='{"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}'
    SOURCE_DIR="/"
    
    sudo rsync -aAXv ${SOURCE_DIR} --exclude=${EXCLUDE_DIR} ${BACKUP_PATH}
    
    if [ $? -eq 0 ]; then
        echo "Backup completed successfully"
    else
        echo "Some error occurred during backup"
    fi

    You can schedule the script to run automatically using a tool such as cron. This will allow you to create regular backups of your system without having to manually run the script.

    To schedule the script, edit the crontab:

    crontab -e 
    

    Append the following entry. Make sure to set the correct script name and path. The below schedule will run the script at 02 AM every day.

    0  2  *  *  *  bash backup.sh >> backup.log

    Save and close the editor.

    Conclusion

    Now that you know how to use rsync, you may want to take advantage of its advanced features. For instance, you can use rsync to efficiently copy files from one directory to another. You can also generate incremental backups that allow you to quickly recover files at any time. If you want even more control over your backup process, you can even schedule backups.

    The post Backing Up Your Linux System with Rsync: A Step-by-Step Guide appeared first on TecAdmin.

    ]]> https://tecadmin.net/backup-linux-system/feed/ 0 10 Most Popular Open Source Linux Shells https://tecadmin.net/most-popular-open-source-linux-shells/ https://tecadmin.net/most-popular-open-source-linux-shells/#respond Mon, 09 Jan 2023 02:23:29 +0000 https://tecadmin.net/?p=33682 The Linux shell is a command-line interface that allows users to interact with the operating system and execute commands. There are several different types of Linux shells available, each with its own set of features and characteristics. In this article, we will introduce the 10 most popular open-source Linux shells, which are widely used by [...]

    The post 10 Most Popular Open Source Linux Shells appeared first on TecAdmin.

    ]]>
    The Linux shell is a command-line interface that allows users to interact with the operating system and execute commands. There are several different types of Linux shells available, each with its own set of features and characteristics. In this article, we will introduce the 10 most popular open-source Linux shells, which are widely used by developers, system administrators, and other users around the world.

    1. Bash Shell

    Bash, or the Bourne Again Shell, is the default shell on most Linux and Unix-like operating systems. It is a widely-used, powerful, and flexible shell that is suitable for a wide range of tasks.

    The main features and characteristics of the bash shell are:

    • Command history: Bash allows users to easily access and execute previous commands using the up and down arrow keys.
    • Job control: Bash supports job control, which allows users to run processes in the background, interrupt them, and resume them.
    • Shell scripts: Bash supports shell scripting, which allows users to automate tasks by writing scripts that execute a series of commands.
    • Aliases: Bash allows users to create aliases for frequently used commands, which can save time and improve efficiency.

    2. Zsh Shell

    Zsh, or the Z shell, is a feature-rich and highly customizable shell that is popular among power users. It has a large number of built-in features and supports a wide range of plugins and themes.

    • Command completion: Zsh supports programmable command completion, which allows users to customize the way command completions are displayed and triggered.
    • Plugins: Zsh supports a wide range of plugins that can add additional features and functionality to the shell.
    • Themes: Zsh supports customizable themes that allow users to change the appearance of the shell prompt and other elements.
    • Advanced command history: Zsh includes advanced command history features, such as the ability to search and execute previous commands.

    3. Fish Shell

    Fish, or the Friendly Interactive Shell, is a modern and user-friendly shell that is designed to be easy to use and learn. It has a syntax highlighting the feature and supports auto-suggestions, making it popular among new users.

    • Syntax highlighting: Fish includes syntax highlighting, which makes it easier to read and understand scripts by displaying different parts of the code in different colors.
    • Auto-suggestions: Fish supports auto-suggestions, which offer suggestions for completing commands as the user types them.
    • User-friendly syntax: Fish has a user-friendly syntax that is designed to be easy to learn and use.
    • Tab completions: Fish supports tab completions for commands, options, and arguments.

    4. Ksh Shell

    Ksh, or the Korn Shell, is a shell developed by David Korn at Bell Labs. It is known for its command history feature, which allows users to easily access and execute previous commands.

    • Command history: Ksh includes a command history feature that allows users to easily access and execute previous commands.
    • Aliases: Ksh allows users to create aliases for frequently used commands, which can save time and improve efficiency.
    • Functions: Ksh supports the creation of functions, which are reusable blocks of code that can be called from multiple places in a script.
    • Array variables: Ksh supports array variables, which allow users to store and manipulate multiple values in a single variable.

    5. Csh Shell

    Csh, or the C shell, is a shell that was developed at the University of California, Berkeley. It is similar to the C programming language and is known for its syntax and control structures.

    • Syntax and control structures: Csh is based on the C programming language, and its syntax and control structures are similar to those of C.
    • Aliases: Csh allows users to create aliases for frequently used commands, which can save time and improve efficiency.
    • Command history: Csh includes a command history feature that allows users to easily access and execute previous commands.
    • Job control: Csh supports job control, which allows users to run processes in the background, interrupt them, and resume them.

    6. Dash Shell

    Dash, or the Debian Almquist Shell, is a lightweight and fast shell that is designed to be used as the default shell on Debian-based systems. It is known for its minimalism and speed, and is often used in scripts and other automated tasks.

    • Lightweight and fast: Dash is a lightweight and fast shell that is designed to be used as the default shell on Debian-based systems.
    • Portable: Dash is portable and can be easily compiled on a wide range of systems.
    • Suitable for scripts: Dash is often used in scripts and other automated tasks due to its speed and minimalism.

    7. Tcsh Shell

    Tcsh, or the TENEX C shell, is a shell that is based on the C shell and includes additional features such as command line editing and programmable completion.

    • Command line editing: Tcsh includes command line editing features, such as the ability to use the left and right arrow keys to move the cursor, and the CTRL-R key to search the command history.
    • Programmable completion: Tcsh supports programmable completion, which allows users to customize the way command completions are displayed and triggered.
    • Aliases: Tcsh allows users to create aliases for frequently used commands, which can save time and improve efficiency.

    8. Ash Shell

    Ash, or the Almquist Shell, is a lightweight and portable shell that is often used in embedded systems and other resource-constrained environments.

    • Lightweight and portable: Ash is a lightweight and portable shell that is often used in embedded systems and other resource-constrained environments.
    • Suitable for scripts: Ash is often used in scripts and other automated tasks due to its small size and minimalism.
    • POSIX compliance: Ash is compliant with the POSIX shell specification, which makes it suitable for use in scripts that need to be portable across different systems.

    9. Psh Shell

    Psh, or the Public Domain Korn Shell, is a fork of the Korn shell that includes additional features and improvements. It is known for its portability and compatibility with other shells.

    • Command history: Psh includes a command history feature that allows users to easily access and execute previous commands.
    • Aliases: Psh allows users to create aliases for frequently used commands, which can save time and improve efficiency.
    • Functions: Psh supports the creation of functions, which are reusable blocks of code that can be called from multiple places in a script.
    • Portability: Psh is designed to be portable and can be easily compiled on a wide range of systems.

    10. Xonsh Shell

    Xonsh is a Python-based shell that combines the features of multiple shells and adds additional features such as syntax highlighting and tab completion. It is known for its flexibility and integration with other Python tools.

    • Python-based: Xonsh is a Python-based shell that allows users to use Python syntax and libraries in the shell.
    • Syntax highlighting: Xonsh includes syntax highlighting, which makes it easier to read and understand scripts by displaying different parts of the code in different colors.
    • Tab completions: Xonsh supports tab completions for commands, options, and arguments.
    • Integration with other tools: Xonsh can be integrated with other Python tools and libraries, allowing users to leverage their capabilities in the shell.

    Wrap Up

    These are some of the most popular open-source Linux shells available today. Each shell has its own set of features and characteristics, and the right shell for a specific task will depend on the needs and preferences of the user.

    The post 10 Most Popular Open Source Linux Shells appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/most-popular-open-source-linux-shells/feed/ 0
    How to Create Bash Aliases with Parameters https://tecadmin.net/how-to-create-bash-aliases-with-parameters/ https://tecadmin.net/how-to-create-bash-aliases-with-parameters/#respond Tue, 03 Jan 2023 08:34:12 +0000 https://tecadmin.net/?p=33217 Bash aliases are shortcuts that allow you to use a shorter or simpler command to represent a longer or more complex command. Bash aliases are useful when you frequently use long or complex commands and want to save time and effort by using a shorter or simpler command instead. To create a Bash alias with [...]

    The post How to Create Bash Aliases with Parameters appeared first on TecAdmin.

    ]]>
    Bash aliases are shortcuts that allow you to use a shorter or simpler command to represent a longer or more complex command. Bash aliases are useful when you frequently use long or complex commands and want to save time and effort by using a shorter or simpler command instead. To create a Bash alias with arguments and parameters, you can use the alias command and include variables in the alias definition.

    In this article, we will explore how to create Bash aliases with arguments and parameters.

    Creating a Bash Alias

    You can use the `alias` command for creating aliases in your Linux system.

    alias alias_name='command'
    

    Here `alias_name` is the name of the alias, and `command` is the command that you want to alias.

    For example, consider the following command:

    ls -l /var | grep "^d" 
    

    This command lists the directories in the `/var` directory in a long format and filters the output to show only the directories.

    To create an alias for this command, you can use the following `alias` command:

    alias lsdir='ls -l /var | grep "^d"' 
    

    This will create an alias named `lsdir` that represents the original command. To use the alias, you can simply type `lsdir` at the command prompt, and the original command will be executed.

    Creating Bash Alias with Arguments

    Bash aliases do not accept arguments, but we can create a function that will accept the command line parameters. These functions can be used as aliases in your Linux system. For example, consider the following function definition:

    lsdir(){ ls -l $1 | grep "^d"; } 
    

    This alias definition creates an alias named `lsdir` that takes an argument ($1) representing the directory to list. To use this alias, you can pass the directory as an argument when you invoke the alias. For example:

    lsdir /var 
    

    This will list the directories in the `/etc` directory in long format and filter the output to show only the directories.

    How to Create Bash Aliases with Parameters
    Creating a Bash Alias with Parameters

    Setup Permanent Bash Aliases

    To make the alias permanent, you can add the alias command to your `~/.bashrc` file. This will ensure that the alias is available every time you start a new Bash session.

    vim ~/.bashrc 
    

    Append the following script at end of the script.

    lsdir(){
            ls -l $1 | grep "^d";
    }

    How to Create Bash Aliases with Parameters
    Create Permanent Bash Alias with Parameters

    Then source the `~/.bashrc` configuration to update the current shell environments.

    source ~/.bashrc 
    

    It is important to note that Bash aliases are not the same as Linux commands, and they are not recognized by other programs or shells. If you want to use the alias in a script or in another shell, you will need to define the alias in that script or shell as well.

    Conclusion

    In conclusion, Bash aliases are useful when you frequently use long or complex commands and want to save time and effort by using a shorter or simpler command instead. To create a Bash alias with arguments and parameters, you can use the alias command and include variables in the alias definition. This allows you to pass arguments and parameters to the alias when you invoke it. To make the alias permanent, you can add the alias command to your ~/.bashrc file. However, it is important to note that Bash aliases are not the same as Linux commands, and they are not recognized by other programs or shells. If you want to use the alias in a script or in another shell, you will need to define the alias in that script or shell as well.

    The post How to Create Bash Aliases with Parameters appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/how-to-create-bash-aliases-with-parameters/feed/ 0
    5 Practical Examples to Check If a Port is Open https://tecadmin.net/check-if-port-is-open/ https://tecadmin.net/check-if-port-is-open/#respond Mon, 02 Jan 2023 15:28:57 +0000 https://tecadmin.net/?p=32976 In Linux, a port is a numbered network connection that allows a device to communicate with other devices over the internet or a local network. It is important to ensure that the desired ports are open and accessible to ensure the smooth functioning of network services. There are various ways to check if a port [...]

    The post 5 Practical Examples to Check If a Port is Open appeared first on TecAdmin.

    ]]>
    In Linux, a port is a numbered network connection that allows a device to communicate with other devices over the internet or a local network. It is important to ensure that the desired ports are open and accessible to ensure the smooth functioning of network services. There are various ways to check if a port is open in Linux, and in this article, we will discuss five of them.

    In this tutorial, we will discuss 3 methods (`nc`, `nmap`, and `telnet`) to check the listening (open) port on a remote host. Also, discuss 2 (`lsof` and `ss`) commands to check if a port is listening on the local machine.

    Check Open Port on Remote Host

    First, check if a post is open and listening on the remote host.

    1. Using `nc` Command
    2. One way to check if a port is open is to use the `nc` (Netcat) utility. The nc command allows you to send data to a port and see if a response is received. To check if a port is open, use the following syntax:

      ## Syntax 
      nc -vz hostname port
      

      For example, to check if port 22 is open on the host example.com, you can use the following command:

      nc -vz 192.168.1.100 22 
      

      If the port is open, you will see the message `Connection to hostname port [tcp/ssh] succeeded!`. If the port is closed, you will see the message `Connection to hostname port [tcp/ssh] failed: Connection refused`.

    3. Using `telnet` Command
    4. Another way to check if a port is open is to use the `telnet` utility. The telnet command allows you to connect to a port on a remote host and see if a connection is established. To check if a port is open, use the following syntax:

      ## Syntax 
      telnet hostname port
      

      For example, to check if port 80 is open on the host www.example.com, you can use the following command:

      telnet www.example.com 80 
      

      If the port is open, you will see a blank screen. To exit, press `CTRL + ]` and then type quit. If the port is closed, you will see the message `Connected to hostname. Escape character is '^]'. Connection closed by foreign host.`.

    5. Using `nmap` Command
    6. The `nmap` command is a utility that performs network scanning and probing. It can be used to check if a port is open by performing a port scan on the target host. To check if a port is open, use the following syntax:

      ## Syntax 
      nmap -p port hostname
      

      To check if port 80 is open on the host `www.example.com` using the `nmap` command, you can use the following syntax:

      nmap -p 80 www.example.com 
      

      If the port is open, you will see a line in the output indicating that the port is `open`. If the port is closed, you will see a line indicating that the port is `closed`.

    Shell Script to Check Port Status

    Shell scripts are very useful for task automation. You can create a bash script that checks if a port is open on the local or remote host. An example script is written below:

    #!/usr/bin/env bash
    
    HOST=192.168.10.100  #remote host
    PORT=22  # Port to check
    
    nc -z ${HOST} ${PORT}
    if [ $? -eq 0 ]
    then
            echo "Port is open"
    else
            echo "Port is closed"
    fi

    Here HOST is the hostname or IP address of the remote or local host system. The PORT contains the port number to be checked. As you can read above, `nc` is the command line utility that can connect to the host on any port and return the status. The “$?” is a system environment variable that contains the exit status of the last command.

    You can also modify the script as per your requirements. For example, we can pass the HOST and PORT values as command line parameters as shown below screenshot.

    Check Script To Check Port Is Open
    Check Script To Check Port Is Open

    The above results show that on host 192.168.10.101 port 80 is open for our system but port 81 is closed.

    Check Listening Port on LocalHost

    Many times we need to check if any port is listening on our local machine. In that case, you can use the following commands. Lets, check if a port is listening on the local host.

    1. Using `lsof` Command
    2. The `lsof` command is a utility that displays information about open files. It can be used to check if a port is open by looking for the port in the list of open files. To check if a port is open, use the following syntax:

      ## Syntax 
      lsof -i :port
      

      For example, to check if port 80 is open, you can use the following command:

      lsof -i :80 
      

      If the port is open, you will see a line with the port number and the name of the process using the port. If the port is closed, you will not see any output.

    3. Using `ss` Command
    4. The `ss` command is a utility that displays network socket information. It can be used to check if a port is open by looking for the port in the list of open sockets. To check if a port is open, use the following syntax:

      ## Syntax 
      ss -lnp | grep port
      

      For example, to check if port 80 is open, you can use the following command:

      ss -lnp | grep 80 
      

      If the port is open, you will see a line with the port number and the status LISTEN. If the port is closed, you will not see any output.

    Note that you may need to use `sudo` to run these commands, depending on your system configuration.

    Conclusion:

    In conclusion, checking if a port is open in Linux is an essential task for maintaining the smooth functioning of network services. The five methods discussed in this article provide different ways to check the availability of a port in Linux. These methods include using the `telnet` command, the `netstat` command, the `lsof` command, the `nmap` command, and the `ss` command. Whether you are a beginner or an experienced Linux user, these methods will help you troubleshoot any issues related to port availability on your system.

    The post 5 Practical Examples to Check If a Port is Open appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/check-if-port-is-open/feed/ 0
    Shell Script to Monitor Disk Space and Send Alert https://tecadmin.net/shell-script-to-check-disk-space-and-send-alert/ https://tecadmin.net/shell-script-to-check-disk-space-and-send-alert/#respond Tue, 27 Dec 2022 03:13:46 +0000 https://tecadmin.net/?p=33058 It is essential to monitor the disk space on a Linux server to ensure enough free space is available for new files and applications. If the disk becomes full, it can cause issues such as system crashes, data loss, and other problems. To prevent these issues, you can use a shell script to monitor the [...]

    The post Shell Script to Monitor Disk Space and Send Alert appeared first on TecAdmin.

    ]]>
    It is essential to monitor the disk space on a Linux server to ensure enough free space is available for new files and applications. If the disk becomes full, it can cause issues such as system crashes, data loss, and other problems. To prevent these issues, you can use a shell script to monitor the disk space and send an alert when the available space falls below a certain threshold.

    In this article, we will walk through the process of creating a shell script that monitors the disk space and sends an alert when the available space falls below a certain threshold. We will use the df command to check the available disk space and the mail command to send the alert. The features of this script are:

    • This script can check available free space for multiple disks
    • You can enable to send an email notification
    • You can set the threshold values for Warning and Critical conditions
    • Accept inputs as command line parameters

    Step 1: Shell Script to Check Disk Space

    I have written this shell script that is capable of checking for the available free space on given disks and notifying the admin if the disk space is low. This script required a Bash shell to run. First copy the shell script on your Linux system. In the next steps, I will provide instructions on how to execute it.

    Download this script from GitHub:

  9. https://github.com/tecrahul/shell-scripts/blob/master/check-disk-space/check_disk_space.sh
  10. Otherwise copy the below shell script and paste it to a file on your server.

    #/usr/bin/env bash
    
    #########################################################################
    #########################################################################
    #
    # This shell script checks for free disk space for defined disks and send
    # email alert based on threshold defined for warning and critical emails 
    #
    # Warning and critical thresholds can be passed as command-line parameters
    # The command can be run as:
    #
    # "bash /path/to/script.sh -w 20 -c 10 -d /dev/sda1 -d /"
    #
    # The above script will check free space on /dev/sda1 and disk
    # mounted on the root (/) file system. The script will send a Warning alert
    # if free space is less than 20% of available space and a Critical alert
    # will be sent if free space is less than 10%. 
    #
    #   Default warning alert threshold: 20%
    #   Default critical alert threshold: 10%
    #   Default disk to check: /
    #
    #########################################################################
    #########################################################################
    
    ### initializing variables
    
    ## To enable email notification set ENABLE_EMAIL_ALERT to 1
    ENABLE_EMAIL_ALERT=1
    NOTIFICATION_EMAIL="youremail@example.com"
    
    ## Uncomment and set a custom hostname, default uses the system's hostname
    #HOSTNAME="web-server1"
    
    
    ## Other variables required for the script
    
    THRESHOLD_WARNING=20     #In percent
    THRESHOLD_CRITICAL=10    #In percent
    
    WARNING=0
    CRITICAL=0
    
    WARNING_ALERT=0
    CRITICAL_ALERT=0
    
    ### Create a temporary file to compose an email
    mail_content=`mktemp`
    
    
    ### Read the command line parameters
    while getopts ":w:c:d:" option; do
        case ${option} in
            w)
                THRESHOLD_WARNING=${OPTARG}
                ;;
            c)
                THRESHOLD_CRITICAL=${OPTARG}
                ;;
            d)
                set -f
                disks+=($OPTARG)
                ;;
        esac
    done
    
    send_notification(){
    	echo "Sending email notification to ${NOTIFICATION_EMAIL}"
    	SUBJECT="${1} ALERT: Host $HOSTNAME Disk Check"
    	mail -s ${NOTIFICATION_EMAIL} < ${mail_content}
    }
    
    ### Function to check available space on a given disk 
    check_disk_space(){
    
        local total_used_space=`df -h $1 | awk '{print $5}' | tail -1`
        local used_space_percent=`echo ${total_used_space:0:-1}`
        local free_space_percent=$(( 100 - $used_space_percent ))
    
        if (( $free_space_percent <= ${THRESHOLD_CRITICAL} )); then
            CRITICAL=1
            return 2
        elif (( $free_space_percent <= ${THRESHOLD_WARNING} )); then
            WARNING=1
            return 1
        else
            OK=1
            return 0
        fi
    }
    
    ### Check if the disk is passed as command line else select root (/)
    if [ ${#disks[@]} -lt 1 ]; then
            echo "No disk is provided, Selecting root disk as default"
    		disks[=]="/"
    fi
    
    ### Create email content
    echo "Attention:
     
    One or more disks are low in space on host \"${HOSTNAME}\".
    " >> ${mail_content}
    
    
    echo ":: CHECK CONDITION"
    echo "-- Warning if free disk is below: ${THRESHOLD_WARNING}%"
    echo "-- Critical if free disk is below: ${THRESHOLD_CRITICAL}%"
    
    echo ":: CHECKING DISKS"
    echo "-- Total disk to check: ${disks[@]}"
    
    ### Calling function check_disk_space for all disk one by one
    for disk in "${disks[@]}"; do
        check_disk_space ${disk}
        if [ ${CRITICAL} -eq 1 ];then
            echo "  => Disk \"${disk}\" is in critical state" | tee -a  ${mail_content}
            CRITICAL_ALERT=1
            CRITICAL=0
        elif [ ${WARNING} -eq 1 ];then
            echo "  => Disk \"${disk}\" is in warning state" | tee -a ${mail_content}
            WARNING_ALERT=1
            WARNING=0
        else
            echo "  => Disk \"${disk}\" is ok"
        fi
    done
    
    ### Finish mail content
    
    echo "
    --
    Thanks
    $HOSTNAME" >>  ${mail_content}
    
    ## Notify if at least one disk is in warning or critical state
    if [ ${CRITICAL_ALERT} -ne 0 ]; then
        [[ $ENABLE_EMAIL_ALERT -eq 1 ]] && send_notification CRITICAL	
    elif [ ${WARNING_ALERT} -ne 0 ]; then
        [[ $ENABLE_EMAIL_ALERT -eq 1 ]] && send_notification WARNING
    else
        echo "All disk(s) are okay!"
    fi
    
    #####################      End of Script     ############################

    Step 2: Script Commadn Line Arguments

    You can execute the above script manually or using the system crontab. This script required arguments, that can be passed as command line arguments.

    1. Critical Threshold: Set this parameter to send a critical email alert if the free space (%) is less than this. Use -c command line option to provide custom value. The default value is “10”, if not provided.
    2. Warning Threshold: Set this parameter to send a warning email alert if the free space (%) is less than this but more that the critical value. Use -w command line option to provide custom value. The default value is “20”, if not provided.
    3. Disks to Check: Use -d command line option to provide volume name or mount point to check for free space. You can provide multiple disks by providing them multiple times.

    Step 2: Execute Script Manually

    You can run this script manually or automate it using the system’s crontab. Here are a few examples of how to run this script:

    • Run this script without any arguments.
      bash check_disk_space.sh 
      

      This is similar to:

      bash check_disk_space.sh -c 10 -w 20 -d / 
      
    • Next, you can change the warning and critical alert levels as per your requirements. For example, send a warning notification if the free space is less than 30% and send a critical notification if the free space is less than 15%.
      bash check_disk_space.sh -c 15 -w 30 -d / 
      
    • You can also use the device name instead of the mount point.
      bash check_disk_space.sh -c 15 -w 30 -d /dev/sda1 
      
    • This script also allows the monitoring of multiple disks.
      bash check_disk_space.sh -c 15 -w 30 -d / -d /mnt -d /dev/sda1 
      

    Step 4: Adjust Variables in the Script

    You can adjust a few variables in the script to customize the environment.

    • Set the below option to “1”, to send email notifications.
      ENABLE_EMAIL_ALERT=1
      
    • If the email notification is enabled, Set your email address for sending the notification
      NOTIFICATION_EMAIL="youremail@example.com"
      
    • Overwrite the default hostname, that will be added in notifications. The default value is the hostname of the system.
      HOSTNAME="web-server1"	
      

    Step 4: Schedule Script with Crontab

    You can also schedule this script using the system’s crontab to automate. Edit the crontab:

    crontab -e 
    

    Add the below crontab entry to run this script hourly.

    ## Monitor disk space
    0   *   *   *   *   bash check_disk_space.sh -w 30 -c 15  -d /

    Save the cron file and close it.

    Conclusion

    In this tutorial, we have provided a bash script that will check for free disk space and send an email notification to the system administrator. This script can monitor multiple disks at a time. Also, you can set custom thresholds for the warning and critical levels.

    The post Shell Script to Monitor Disk Space and Send Alert appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/shell-script-to-check-disk-space-and-send-alert/feed/ 0
    How to Check Open (Listening) Ports in Linux https://tecadmin.net/how-to-check-listening-ports-in-linux/ https://tecadmin.net/how-to-check-listening-ports-in-linux/#respond Mon, 26 Dec 2022 11:55:42 +0000 https://tecadmin.net/?p=33161 In Linux, a port is a logical connection point for transmitting data between a client and a server. To ensure the security and functionality of a system, it is important to know which ports are open and listening for incoming connections. Ports are identified by a number, ranging from 0 to 65535. There are three [...]

    The post How to Check Open (Listening) Ports in Linux appeared first on TecAdmin.

    ]]>
    In Linux, a port is a logical connection point for transmitting data between a client and a server. To ensure the security and functionality of a system, it is important to know which ports are open and listening for incoming connections. Ports are identified by a number, ranging from 0 to 65535.

    There are three categories of ports in a Linux system:

    • Well-known ports: These are the ports that are reserved for specific services and are assigned by the Internet Assigned Numbers Authority (IANA). Some examples of well-known ports are 22 for SSH, 80 for HTTP, and 443 for HTTPS.
    • Registered ports: These are the ports that are registered with the IANA for specific purposes but are not reserved for specific services. These ports are typically used by specific applications or protocols.
    • Dynamic and/or private ports: These are the ports that are not reserved or registered with the IANA and can be used by any application or protocol. These ports are typically used for temporary or ephemeral connections.

    In this article, we will cover several methods for checking open ports in Linux.

    How to Check Open Ports in Linux

    To check open or listening ports in Linux, you can use the `netstat`, `ss`, `lsof`, and `nmap` commands.

    1. Using the `netstat` Command
    2. The `netstat` command is a utility that displays network connections, routing tables, and a variety of network statistics. To check open ports in Linux with `netstat`, follow these steps:

      Open a terminal window and run the following command:

      sudo netstat -tulpn 
      

      The -t flag displays TCP connections, the -u flag displays UDP connections, the `-l` flag displays listening sockets, the `-p` flag displays the PID and name of the process, and the `-n` flag displays numerical addresses instead of symbolic names.

      Output
      Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 534/sshd: /usr/sbin tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 347/systemd-resolve tcp6 0 0 :::80 :::* LISTEN 245266/apache2 tcp6 0 0 :::22 :::* LISTEN 534/sshd: /usr/sbin udp 0 0 0.0.0.0:5353 0.0.0.0:* 409/avahi-daemon: r udp 0 0 0.0.0.0:52848 0.0.0.0:* 409/avahi-daemon: r

      The output of the command will show a list of open ports and the corresponding processes.

    3. Using the `lsof` Command
    4. The `lsof` command is a utility that lists open files on a system. To check open ports in Linux with lsof, follow these steps:

      Open a terminal window and run the following command:

      sudo lsof -i 
      

      The -i flag specifies the Internet address of a file.

      Output
      COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd-r 347 systemd-resolve 13u IPv4 25561 0t0 UDP 127.0.0.53:domain systemd-r 347 systemd-resolve 14u IPv4 25562 0t0 TCP 127.0.0.53:domain (LISTEN) avahi-dae 409 avahi 12u IPv4 26700 0t0 UDP *:mdns avahi-dae 409 avahi 14u IPv4 26702 0t0 UDP *:52848 sshd 534 root 3u IPv4 27554 0t0 TCP *:ssh (LISTEN) sshd 534 root 4u IPv6 27589 0t0 TCP *:ssh (LISTEN) sshd 4675 root 4u IPv4 136177 0t0 TCP 192.168.1.210:ssh->192.168.1.10:52623 (ESTABLISHED) apache2 245266 root 4u IPv6 686069 0t0 TCP *:http (LISTEN) apache2 245269 www-data 4u IPv6 686069 0t0 TCP *:http (LISTEN)

      The output of the command will show a list of open ports and the corresponding processes.

      You can also check for the specific port number:

      sudo lsof -i :80 
      

    5. Using the `ss` command
    6. The `ss` command is a utility that displays network sockets and their associated connections. To check open ports in Linux with `ss`, follow these steps:

      Open a terminal window and run the following command:

      ss -tulpn 
      

      The `-t` flag displays TCP connections, the `-u` flag displays UDP connections, the `-l` flag displays listening sockets, the `-p` flag displays the PID and name of the process, and the `-n` flag displays numerical addresses instead of symbolic names.

      The output of the command will show a list of open ports and the corresponding processes.

    7. Using the `nmap` Command
    8. The `nmap` command is a utility that scans networks for hosts and services. To check open ports in Linux with `nmap`, follow these steps:

      Open a terminal window and run the following command:

      nmap -p- localhost 
      

      The `-p-` flag specifies a range of ports to scan. The `-` indicates that all ports should be scanned.

      The `localhost` argument specifies the target host to scan.

      The output of the command will show a list of open ports on the target host.

    Conclusion

    In this article, we covered several methods for checking open ports in Linux. We covered using the `netstat`, `ss`, `lsof`, and `nmap` commands to display a list of open ports and the corresponding processes. By using these methods, you can easily check open ports on your Linux system.

    I hope this helps you understand how to check open ports in Linux. If you have any further questions, please don’t hesitate to ask.

    The post How to Check Open (Listening) Ports in Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/how-to-check-listening-ports-in-linux/feed/ 0
    How to Open Port for a Specific Network in FirewallD https://tecadmin.net/open-port-for-a-specific-network-in-firewalld/ https://tecadmin.net/open-port-for-a-specific-network-in-firewalld/#respond Thu, 22 Dec 2022 13:11:28 +0000 https://tecadmin.net/?p=20826 In FirewallD, the `--source` option allows you to specify a network or an IP address as the source for applying the rules. It is used to specify the network or IP address that is allowed to access the port or service that is being opened. The `--add-source` option is used to specify multiple networks or [...]

    The post How to Open Port for a Specific Network in FirewallD appeared first on TecAdmin.

    ]]>
    In FirewallD, the `--source` option allows you to specify a network or an IP address as the source for applying the rules. It is used to specify the network or IP address that is allowed to access the port or service that is being opened. The `--add-source` option is used to specify multiple networks or IP addresses as the source for the rules being applied. It is used in conjunction with the `--permanent` option to add multiple sources to a rule already configured in the firewall.

    Open Port for Single IP/Network

    For example, the following command will open port `80` for the network `192.168.1.0/24`:

    firewall-cmd --permanent --zone=public --add-port=80/tcp --source=192.168.1.0/24 
    

    In this case, the `--source` option specifies that the network 192.168.1.0/24 is allowed to access port 80.

    You can also use the `--source` option to specify a single IP address as the source. For example:

    firewall-cmd --permanent --zone=public --add-port=80/tcp --source=192.168.1.100 
    

    This will open port 80 for the IP address 192.168.1.100.

    Reload the FirewallD configuration to apply the changes. You can do this by running the following command:

    firewall-cmd --reload 
    

    Open Port for Multiple IP/Network

    You can use the `--add-source` option instead of `--source` to add multiple sources to the rule.

    For example, the following command will add the network 192.168.2.0/24 as an additional source for the rule that opens port 80:

    firewall-cmd --permanent --zone=public --add-source=192.168.2.0/24 --add-port=80/tcp 
    

    You can add multiple sources by separating them with a space. For example:

    firewall-cmd --permanent --zone=public --add-source=192.168.2.0/24 192.168.3.0/24 --add-port=80/tcp 
    

    This will add the networks 192.168.2.0/24 and 192.168.3.0/24 as additional sources for the rule that opens port 80.

    Note: You can use the `--source` option instead of `--add-source` to specify a single source for the rule. The –source option will overwrite any existing sources for the rule, while –add-source will add the specified source to the existing list of sources.

    Reload the FirewallD configuration to apply the changes. You can do this by running the following command:

    firewall-cmd --reload 
    

    You can verify that the port has been opened by using the firewall-cmd command with the –list-ports option. For example `firewall-cmd --zone=public --list-ports` will list all the ports that are open in the public zone.

    Conclusion

    In conclusion, FirewallD is a powerful tool that can be used to control incoming and outgoing network traffic on a Linux system. It allows you to open specific ports for specific networks, providing an additional layer of security for your system. To open a specific port for a specific network in FirewallD, you will need to install and start the FirewallD service, and then use the firewall-cmd command with the –permanent, –zone, –add-port, and –source options. You can then verify that the port has been opened by using the –list-ports option. By following these steps, you can easily open a specific port for a specific network in FirewallD and improve the security of your system.

    The post How to Open Port for a Specific Network in FirewallD appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/open-port-for-a-specific-network-in-firewalld/feed/ 0
    How To Customize Bash Prompt (PS1) In Linux https://tecadmin.net/how-to-customize-bash-prompt-ps1-in-linux/ https://tecadmin.net/how-to-customize-bash-prompt-ps1-in-linux/#respond Wed, 21 Dec 2022 06:19:35 +0000 https://tecadmin.net/?p=22778 In Linux, PS1 is an environment variable that specifies the format of the command prompt displayed in the terminal. It stands for “Prompt String 1” and it is used to customize the appearance of the prompt. By default, the bash prompt includes the current username, hostname, and current working directory, followed by the `$` symbol [...]

    The post How To Customize Bash Prompt (PS1) In Linux appeared first on TecAdmin.

    ]]>
    In Linux, PS1 is an environment variable that specifies the format of the command prompt displayed in the terminal. It stands for “Prompt String 1” and it is used to customize the appearance of the prompt.

    By default, the bash prompt includes the current username, hostname, and current working directory, followed by the `$` symbol for a regular user or the `#` symbol for the root user. The prompt is displayed on the command line, and it indicates that the terminal is ready for input.

    You can customize the bash prompt by modifying the value of the PS1 variable. For example, you can use special characters and codes to change the colors, font styles, and other formatting options of the prompt. You can also include other information, such as the current time or the git branch name, in the prompt.

    In this tutorial, we will discuss how to change the PS1 bash prompt and make is colorful in Linux.

    Change Bash Prompt (PS1) in Linux

    To customize the bash prompt (PS1) in Linux, you can use the following steps:

    • Open the `~/.bashrc` file in a text editor. This file is located in your home directory and contains configuration settings for the bash shell.
    • Find the line that sets the value of `PS1`. It will look something like this:

      PS1='[\u@\h \W]\$ '

      Check the below screenshot:

      How to Change PS1 Prompt in Linux

    • Modify the value of PS1 to customize the appearance of the prompt. You can use the following special characters to include information in the prompt:
      • `\u`: The username of the current user
      • `\h`: The hostname up to the first .
      • `\H`: The full hostname
      • `\w`: The current working directory
      • `\W`: The basename of the current working directory
      • `\$`: This code represents the prompt symbol, which is $ for a regular user and # for the root user.
    • For example, to customize the prompt to display the current working directory and the $ symbol, you could use the following value for PS1:

      PS1='\w \$ '

      Check the below screenshot:

      Changing PS1 Prompt in Linux

    • Save the `~/.bashrc` file and exit the text editor.
    • Run the following command to apply the changes to your current session:
      source ~/.bashrc 
      

    Your bash prompt will now be customized according to the value you set for PS1.

    Make Colorful Bash Prompt (PS1) in Linux

    To customize the bash prompt (PS1) in Linux, you can use special characters and codes to add colors and other formatting options.

    Here’s an example of a bash prompt that includes a red username, a green hostname, and a blue current working directory:

    PS1='\[\e[0;31m\]\u\[\e[m\] \[\e[0;32m\]\h\[\e[m\]@\[\e[0;34m\]\w\[\e[m\]\$ '

    To set the bash prompt in your current session, you can simply copy and paste the above code into the terminal and press Enter. To make the change permanent, you can add the same line to the .bashrc file in your home directory.

    How to Make Colourful PS1 Prompt in Linux

    Here’s a breakdown of the different color codes used in the example above:

    • \[\e[0;31m\] – This code sets the text color to red. The 0;31 value specifies the color, with 31 representing red.
    • \[\e[0;32m\] -This code sets the text color to green. The 0;32 value specifies the color, with 32 representing green.
    • \[\e[0;34m\] -This code sets the text color to blue. The 0;34 value specifies the color, with 34 representing green.
    • \[\e[m\] – This code resets the text color to the default value. So the remaining text will be default in color.

    You can use other codes to customize the bash prompt with different colors and formatting options. For example, to make the text bold, you can use \[\e[1m\] before the text and \[\e[m\] after the text. You can find a list of all the available codes in the PROMPTING section of the bash man page.

    I hope this helps! Let me know if you have any questions.

    The post How To Customize Bash Prompt (PS1) In Linux appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/how-to-customize-bash-prompt-ps1-in-linux/feed/ 0
    Copy and Paste Content in Vi or Vim Editor https://tecadmin.net/copy-and-paste-content-in-vim-editor/ https://tecadmin.net/copy-and-paste-content-in-vim-editor/#respond Mon, 19 Dec 2022 17:56:08 +0000 https://tecadmin.net/?p=31681 VIM stands for Vi Improved, and it is one of the most popular text editors for Linux. It is based on the older vi text editor and has many improvements, making it even more powerful and user-friendly. VIM is a cross-platform text editor, meaning it can be used on different operating systems and distributions. It [...]

    The post Copy and Paste Content in Vi or Vim Editor appeared first on TecAdmin.

    ]]>
    VIM stands for Vi Improved, and it is one of the most popular text editors for Linux. It is based on the older vi text editor and has many improvements, making it even more powerful and user-friendly. VIM is a cross-platform text editor, meaning it can be used on different operating systems and distributions. It is a highly customizable text editor, allowing you to customize it to your liking. It also has a lot of features that regular text editors don’t have, such as syntax highlighting, auto-indentation, and tab completion.

    VIM is also known to be very fast and efficient, making it perfect for developers who need to work quickly and accurately. In short, VIM is a great text editor for Linux, perfect for both novices and experienced users alike.

    Copy/Paste Contents in Vim Editor

    Vim is the improvised edition of the Vi editor. Copy and paste is an essential part of the computer user in day-to-day work. We can quickly copy and paste content in Vi/Vim editor using shortcuts. To copy and paste text in the vi/vim editor, follow these steps:

    • Open the file in vi/vim by typing `vi filename` in the terminal.
    • Press Esc to enter normal mode.
    • To copy text, first, you need to select the text that you want to copy. You can do this by moving the cursor to the beginning of the text and then pressing v to enter visual mode. Then, use the arrow keys to highlight the text that you want to copy.
    • Once you have selected the text, press `y` to copy it. This will copy the text to the clipboard.
    • To paste the copied text, move the cursor to the location where you want to paste the text and press `p` to paste it.

    Alternatively, you can also use the yank and put commands to copy and paste text in vi/vim. To copy text using the yank command, type `yy` to copy the current line or `yw` to copy the current word. To paste the copied text using the put command, type `p`.

    To copy more than one line, simply enter the number of lines before yy. For example to copy 5 lines simply hit `5yy` or to copy 100 lines type `100yy`.

    I hope this helps! Let me know if you have any further questions.

    The post Copy and Paste Content in Vi or Vim Editor appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/copy-and-paste-content-in-vim-editor/feed/ 0
    How to Install Skype on Ubuntu 22.04 https://tecadmin.net/how-to-install-skype-on-ubuntu-22-04/ https://tecadmin.net/how-to-install-skype-on-ubuntu-22-04/#respond Wed, 05 Oct 2022 05:28:33 +0000 https://tecadmin.net/?p=32031 Have you heard of the famous video chatting application? Since the beginning of the internet, we have seen many applications and websites for video chatting. Most of them are now obsolete and abandoned. Skype is a voice-calling app with video-calling functionality. If you are an avid user of the popular VoIP service, then this tutorial [...]

    The post How to Install Skype on Ubuntu 22.04 appeared first on TecAdmin.

    ]]>
    Have you heard of the famous video chatting application? Since the beginning of the internet, we have seen many applications and websites for video chatting. Most of them are now obsolete and abandoned. Skype is a voice-calling app with video-calling functionality. If you are an avid user of the popular VoIP service, then this tutorial is for you! This article will help you install Skype on your Debian system with ease.

    Prerequisites

    • A Ubuntu 22.04 system with Desktop Access.
    • You must have sudo privileged account access to your system.

    How to Install Skype on Ubuntu 22.04

    You can choose one of the below two methods for installing Skype on the Ubuntu 22.04 LTS system. The first method will use modern snap packages for installing Skype.

    • Method 1: Install Skype using Snap Package
    • The Snap daemon is default installed on Ubuntu 22.04 system. Also, the Skype package is available on Snapcraft, which helps to quickly install it on a Linux system.

      Open a terminal on your system, which can be found under applications, or use key combination CTRL + ALT + T. Then type the below command to install Skype from snap packages.

      sudo snap install skype --classic 
      

      This will take a few seconds to complete the installation. Once the command finishes, you can start using Skype.

    • Method 2: Install Skype using Debian Package
    • This is the traditional method to install packages using the Debian package. The Skype team provides a Debian package, that can be downloaded from its official download page.

      You can also use the command line to download the latest Skype Debian package. Simply execute the following commands to download the Skype Debian package:

      wget https://repo.skype.com/latest/skypeforlinux-64.deb 
      

      Then use the apt install command to install the locally downloaded package file.

      sudo apt install ./skypeforlinux-64.deb 
      

      That’s it. This will complete the Skype installation on the Ubuntu system.

    Launch Skype Application

    You have successfully installed Skype on the Ubuntu system. Search for the Skype launcher under the applications to start the Skype application. You will see the Skype launch as shown below image.

    How to Install Skype on Ubuntu 22.04
    Launch Skype on Ubuntu

    Click the launch icon on your system. This will open Skype application on your Ubuntu system. Now, you can start using Skype.

    How to Install Skype on Ubuntu 22.04
    Skype running on Ubuntu

    Conclusion

    Moreover, as there are various third-party sites that offer scripts and packages to install individual software on Debian (or any other operating system), we also have listed some reliable sites where you can get these scripts to install Skype on Ubuntu 22.04 or any different version of Debian that supports snap packages.

    The post How to Install Skype on Ubuntu 22.04 appeared first on TecAdmin.

    ]]>
    https://tecadmin.net/how-to-install-skype-on-ubuntu-22-04/feed/ 0