2023

Archive page for 2023 by Linux Bash
  • Posted on

    If you're suffering with server lag, ping latency, degraded system functionality or generally slow response times you may need to add extra RAM to your server.

    Of course, adding RAM is a solution - but it's not the only option you have. Why not think about utilising swap space, this enables you to let your HDD act as a virtual-backup solution to out-of-memory situations.

    These days of course using your HDD for RAM doesn't seem as bad as what it sounds, with SSD's widely used the temptation to switch to swap space is quite understandable.

    Plus, for most use-cases, you will only need as much as 2-4GB of RAM in order to compile your favourite popular software title - such as Apache, or similar. To simply run the program may require less RAM thus less overhead for cost. You could use the package manager to install Apache to avoid having to compile it but why do that when you a) want to compile and b) can simply add swap space.

    To add swap space, follow these instructions:

    sudo fallocate -l 1G /swapfile
    

    Here, we are creating a swap file with a size of 1G. If you need more swap, replace 1G with the desired size.

    A good rule of thumb is to have 3x more swap than your physical RAM. So for 2GB of RAM you would have 6GB of swap space.

    If the fallocate utility is not available on your system or you get an error message saying fallocate failed: Operation not supported, use the dd command to create the swap file:

    sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
    

    Next, set the swap space files permissions so that only the root user can read and write the swap file:

    sudo chmod 600 /swapfile
    

    Next, set up a Linux swap area on the file:

    sudo mkswap /swapfile
    

    Activate the swap by executing the following command:

    sudo swapon /swapfile
    

    And that should do it. Check the memory is allocated by simply using free.

    sudo free -h
    

    Finally, make the changes permanent by adding a swap entry in the /etc/fstab file:

    echo  '/swapfile swap swap defaults 0 0' >> /etc/fstab
    

    Now, when your server reboots the swap space will be reconfigured automatically.


    And that's it! If you enjoyed this post please feel free to leave a like or a comment using the messaging options below.

  • Posted on

    When it happens that your VPS is eating data by the second and there is disk read/write issues one port of call you are bound to visit is searching and identifying large files on your system.

    Now, you would have been forgiven for thinking this is a complicated procedure considering some Linux Bash solutions for fairly simple things, but no. Linux Bash wins again!

    du -sh /path/to/folder/* | sort -rh

    Here, du is getting the sizes and sort is organising them, -h is telling du to display human-readable format.

    The output should be something like this:

    2.3T    /path/to/directory
    1.8T    /path/to/other
    

    It does take a while to organise as it is being done recursively however given 3-5mins and most scenarios will be fine.

  • Posted on

    So yeah, getting used to Bash is about finding the right way to do things. However, learning one-liners and picking up information here and there is all very useful, finding something you don't need to Google in order to recall is extremely important.

    Take the case of recursive find and replace. We've all been there, it needs to be done frequently but you're either a) scared or b) forgetful. Then you use the same snippet from a web resource again and again and eventually make a serious error and boom, simplicity is now lost on you!

    So here it is, something you can remember so that you don't use different methods depending on what Google throws up today.

    grep -Rl newertext . | xargs sed -i 's/newertext/newesttext/g'

    Notice, we use grep to search -R recursively with results fed to xargs which does a simple sed replace with g global/infinite properties.

    Say you want to keep it simple though and find and review the files before doing the "simple" replace. Well, do this.

    grep -R -l "foo" ./*

    The uppercase -R denotes to follow symlinks and the ./* indicates the current directory. The -l requests a list of filenames matched. Neat.

  • Posted on

    Safe and Secure SSH Connections

    In a modern world where cyber-warfare is common place and every-day users are targets from organised crime, it goes without saying that you are likely to run into problems rather quickly if you don't use every available means of security.

    The scope of this article is to connect via SSH Keys however you should also be doing some other more mundane tasks like encrypting the connection (preferably with a VPN on your router) and using altered ports, plus limiting access to SSH users, if you have them.

    So what is the safest way to connect to your remote Linux OS distribution, by command line? Well quite simply, it is done with SSH Keys which you generate so that the connection can be established. These keys are then used as a form of password and where the remote user has these pre-generated keys on their system, SSH shares them and if allowed, serves the connection.

    Generating Your Keys

    From command line on the machine you are connecting from, do the following:

    ssh-keygen - Leave as default values

    This creates files inside your home directories .ssh folder. This is a hidden folder that you usually don't need access to. To see what's inside, do ls .ssh from your home path.

    Now, do the following, from your home path:

    cat .ssh/id_rsa.pub

    This is your public password. Share this with unlimited amounts of remote servers and while you are using this account, you will have access.

    Sharing Your Keys

    On a mundane level, you can provide the key you generated via any method you like, only your machine and account will be able to use it.

    Now, take the output of cat .ssh/id_rsa.pub, and do echo "key-here" >> .ssh/authorized_keys and voila, the magic is done. You can now do ssh user@example.com, password-free.

    So that's one way of achieving passwordless login via SSH, although there is an easier way. Do:

    ssh-copy-id user@example.com
    

    This will auto-install the keys for you, assuming you can connect to the server via SSH using other authentication methods - such as password.

    Removing Keys

    To remove access to a users account, do vi .ssh/authorized_keys and delete the line corresponding to the users account.

    It really is that simple!

    Voila

    Congratulations, you're all set up! Don't forget, while it is perfectly safe to share your id_rsa.pub key, do so with caution. Using it on your website homepage may attract unwanted attention!

    Peace.

  • Posted on

    Get to Know Linux Bash in Under 30 Minutes: A Quick Guide for Beginners

    Linux Bash (Bourne Again Shell) is the default command-line interface for most Linux distributions and macOS. For new users, it might feel overwhelming at first, but once you understand the basics, Bash can become a powerful tool for managing your system, automating tasks, and improving productivity.

    In this quick guide, we’ll walk you through the essentials of Bash in under 30 minutes. Whether you're a beginner or just looking to refresh your knowledge, this guide will help you feel comfortable with the Linux command line.

    1. What is Bash?

    Bash is a command-line interpreter that allows users to interact with their operating system by entering text-based commands. It's a shell program that interprets and runs commands, scripts, and system operations. It’s often referred to as a command-line shell or simply a shell.

    Bash allows users to navigate their filesystem, run programs, manage processes, and even write complex scripts. Most Linux distributions come with Bash as the default shell, making it essential for anyone using Linux systems.

    2. Basic Commands

    Let’s start by covering some essential commands that you’ll use regularly in Bash:

    • pwd: Stands for “print working directory.” It shows you the current directory you're in.

      $ pwd
      /home/user
      
    • ls: Lists the contents of the current directory.

      $ ls
      Documents  Downloads  Pictures
      
    • cd: Changes the current directory. Use cd .. to go up one level.

      $ cd Documents
      $ cd ..
      
    • cp: Copies files or directories.

      $ cp file1.txt file2.txt
      
    • mv: Moves or renames files.

      $ mv oldname.txt newname.txt
      
    • rm: Removes files or directories.

      $ rm file.txt
      
    • mkdir: Creates a new directory.

      $ mkdir new_folder
      

    3. Navigating the Filesystem

    The filesystem in Linux is hierarchical, starting from the root directory (/). Here’s how you can move around:

    • Absolute paths start from the root. Example: /home/user/Documents
    • Relative paths are based on your current directory. Example: Documents (if you're already in /home/user).

    Use cd to navigate to any directory. To go to your home directory, simply type cd without arguments:

    $ cd
    

    To go to the root directory:

    $ cd /
    

    To navigate up one directory:

    $ cd ..
    

    4. Redirection and Pipelines

    Bash allows you to redirect input and output, as well as chain multiple commands together using pipes.

    • Redirection: Redirect output to a file using >. Use >> to append to a file.

      $ echo "Hello, World!" > hello.txt
      $ cat hello.txt  # Prints "Hello, World!"
      
    • Pipes (|): You can send the output of one command to another. For example:

      $ ls | grep "text"  # Lists all files containing "text"
      

    5. Wildcards

    Wildcards are symbols that represent other characters. They are useful for matching multiple files or directories.

    • *: Matches any number of characters.

      $ ls *.txt  # Lists all .txt files in the current directory
      
    • ?: Matches a single character.

      $ ls file?.txt  # Matches file1.txt, file2.txt, etc.
      
    • []: Matches a single character within a range.

      $ ls file[1-3].txt  # Matches file1.txt, file2.txt, file3.txt
      

    6. Managing Processes

    Bash allows you to interact with processes running on your system:

    • ps: Lists the running processes.

      $ ps
      
    • top: Provides a dynamic view of system processes.

      $ top
      
    • kill: Terminates a process by its ID (PID).

      $ kill 1234  # Replace 1234 with the actual PID
      
    • &: Run a command in the background.

      $ my_script.sh &
      

    7. Using Variables

    Bash allows you to define and use variables. Variables can store information such as strings, numbers, or the output of commands.

    • To define a variable:

      $ my_variable="Hello, Bash!"
      
    • To use a variable:

      $ echo $my_variable
      Hello, Bash!
      
    • You can also assign the output of a command to a variable:

      $ current_dir=$(pwd)
      $ echo $current_dir
      

    8. Writing Scripts

    One of the most powerful features of Bash is its ability to write scripts—sequences of commands that you can execute as a single file.

    1. Open a text editor and create a new file, such as myscript.sh.
    2. Add the following code to the script: bash #!/bin/bash echo "Hello, Bash!"
    3. Save and exit the editor.
    4. Make the script executable:

      $ chmod +x myscript.sh
      
    5. Run the script:

      $ ./myscript.sh
      

    The #!/bin/bash at the top of the file is called a "shebang" and tells the system which interpreter to use to execute the script.

    9. Learning More

    To become proficient in Bash, it’s important to keep experimenting and learning. Here are some useful resources to continue your Bash journey:

    • man pages: Bash comes with built-in documentation for most commands. For example:

      $ man ls
      
    • Bash Help: For a quick reference to Bash syntax and commands, use:

      $ help
      
    • Online Tutorials: Websites like LinuxCommand.org and The Linux Documentation Project provide comprehensive tutorials.


    Conclusion

    Mastering Bash doesn’t require an extensive amount of time or effort. By learning the basics of navigation, file management, process handling, and scripting, you can start using the Linux command line to automate tasks, manage your system more effectively, and boost your productivity.

    Now that you've learned the fundamentals, you can explore more advanced topics like loops, conditionals, and scripting techniques. Bash is an incredibly powerful tool that, once understood, can unlock a new world of efficiency on your Linux system.

    Happy exploring!

  • Posted on

    Linux is an open-source Operating System which is released with different flavours (or distros) under the guise of free-to-use software. Anybody can download and run Linux free-of-charge and with no restraints on the end-user; you could release, distribute and profit from Linux with relative ease with no worry of associated cost or licensing infringement.

    It is fair to say Linux has formidably and profoundly revolutionised and defined the process of interacting with electronic devices. You can find Linux in cars, refrigerators, televisions and of course, as a desktop-grade or headless operating system. Once you become accustomed to Linux, you quickly see just why all the top 500 supercomputers all run Linux.

    Linux has been around since the mid-1990’s and is is one of the most reliable, secure and hassle-free operating systems available. Put simply, Linux has become the largest open sources software project in the world. Professional and hobbyist programmers and developers from around the world contribute to the Linux kernel, adding features, finding and fixing bugs and security flaws, live patching and providing new ideas—all while sharing their contributions back to the community.

    Wikipedia

    Linux is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds.

    Direct Link to Linux on Wikipedia

    Open Source

    Linux is a free, open source operating system, released under the GNU General Public License (GPL). Anyone can run, study, modify, and redistribute the source code, or even sell copies of their modified code, as long as they do so under the same license.

    Command Line

    The command line is your direct access to a computer. It's where you ask software to perform hardware actions that point-and-click graphical user interfaces (GUIs) simply can't ask.

    Command lines are available on many operating systems—proprietary or open source. But it’s usually associated with Linux, because both command lines and open source software, together, give users unrestricted access to their computer.

    Installing Linux

    For many people, the idea of installing an operating system might seem like a very daunting task. Believe it or not, Linux offers one of the easiest installations of all operating systems. In fact, most versions of Linux offer what is called a Live distribution, which means you run the operating system from either a CD/DVD or USB flash drive without making any changes to your hard drive. You get the full functionality without having to commit to the installation. Once you’ve tried it out, and decided you wanted to use it, you simply double-click the “Install” icon and walk through the simple installation wizard.

    Installing Software on Linux

    Just as the operating system itself is easy to install, so too are applications. Most modern Linux distributions include what most would consider an app store. This is a centralized location where software can be searched and installed. Ubuntu Linux (and many other distributions) rely on GNOME Software, Elementary OS has the AppCenter, Deepin has the Deepin Software Center, openSUSE has their AppStore, and some distributions rely on Synaptic.

    Regardless of the name, each of these tools do the same thing: a central place to search for and install Linux software. Of course, these pieces of software depend upon the presence of a GUI. For GUI-less servers, you will have to depend upon the command-line interface for installation.

    Let’s look at two different tools to illustrate how easy even the command line installation can be. Our examples are for Debian-based distributions and Fedora-based distributions. The Debian-based distros will use the apt-get tool for installing software and Fedora-based distros will require the use of the yum tool. Both work very similarly. We’ll illustrate using the apt-get command. Let’s say you want to install the wget tool (which is a handy tool used to download files from the command line). To install this using apt-get, the command would like like this:

    sudo apt-get install wget
    

    The sudo command is added because you need super user privileges in order to install software. Similarly, to install the same software on a Fedora-based distribution, you would first su to the super user (literally issue the command su and enter the root password), and issue this command:

    yum install wget
    

    That’s all there is to installing software on a Linux machine. It’s not nearly as challenging as you might think. Still in doubt?

    You can install a complete LAMP (Linux Apache MySQL PHP) server on either a server or desktop distribution. It really is that easy.

    More resources

    If you’re looking for one of the most reliable, secure, and dependable platforms for both the desktop and the server, look no further than one of the many Linux distributions. With Linux you can assure your desktops will be free of trouble, your servers up, and your support requests minimal.

  • Posted on

    If you’ve ever used a Linux operating system used on most Virtual Private Servers, you may have heard of bash. It’s a Unix shell that reads and executes various commands.

    What Is Bash?

    Bash, short for Bourne-Again Shell, is a Unix shell and a command language interpreter. It reads shell commands and interacts with the operating system to execute them.

    Why Use Bash Scripts?

    Bash scripts can help with your workflow as they compile many lengthy commands into a single executable script file. For example, if you have multiple commands that you have to run at a specific time interval, you can compile a bash script instead of typing out the commands manually one by one. You then execute the script directly, when it’s necessary.

    Pro Tip Linux has a bash shell command manual. Type man command to find descriptions of all the technical terms and input parameters.

    Get Familiar With Bash Commands

    Bash is available on almost all types of Unix-based operating systems and doesn’t require a separate installation. You will need a Linux command prompt, also known as the Linux terminal. On Windows you would use something like PuTTy. It’s a program that contains the shell and lets you execute bash scripts. 

    1. Comments

    Comments feature a description on certain lines of your script. The terminal doesn’t parse comments during execution, so they won’t affect the output.

    There are two ways to add comments to a script. The first method is by typing # at the beginning of a single-line comment. # Command below prints a Hello World text echo “Hello, world!”

    2. Variables

    Variables are symbols that represent a character, strings of characters, or numbers. You only need to type the variable name in a command line to use the defined strings or numbers.

    To assign a variable, type the variable name and the string value like here: testvar=“This is a test variable”

    In this case, testvar is the variable name and This is a test variable is the string value. When assigning a variable, we recommend using a variable name that’s easy to remember and represents its value.

    To read the variable value in the command line, use the $ symbol before the variable name. Take a look at the example below:

    testvar=“This is a test variable”
    echo $testvar
    

    In order to let the user enter the variable contents use:

    read testvar
    echo $testvar
    

    3. Functions

    A function compiles a set of commands into a group. If you need to execute the command again, simply write the function instead of the whole set of commands.

    There are several ways of writing functions. The first way is by starting with the function name and following it with parentheses and brackets:

    function_name () {
        first command
        second command
    }
    

    Or, if you want to write it in a single line: function_name () { first command; second command; }

    4. Loops

    Loop bash commands are useful if you want to execute commands multiple times. There are three types of them you can run in bash – for, while, and until. The for loop runs the command for a list of items:

    for item in [list]
    do
        commands
    done
    

    The following example uses a for loop to print all the days of the week:

    for days in Monday Tuesday Wednesday Thursday Friday Saturday Sunday
    do
        echo “Day: $days”
    done
    

    On line 2, “days” automatically becomes a variable, with the values being the day names that follow. Then, in the echo command, we use the $ symbol to call the variable values.

    The output of that script will be as follows:

    Day: Monday
    Day: Tuesday
    Day: Wednesday
    Day: Thursday
    Day: Friday
    Day: Saturday
    Day: Sunday
    

    Notice that even with just one command line in the loop script, it prints out seven echo outputs.

    The next type of loop is while. The script will evaluate a condition. If the condition is true, it will keep executing the commands until the output no longer meets the defined condition.

    while [condition]
        do
    commands
    done
    

    5. Conditional Statements

    Many programming languages, including bash, use conditional statements like if, then, and else for decision-making. They execute commands and print out outputs depending on the conditions. The if statement is followed by a conditional expression. After that, it’s followed by then and the command to define the output of the condition. The script will execute the command if the condition expressed in the if statement is true.

    However, if you want to execute a different command if the condition is false, add an else statement to the script and follow it with the command.

    Let’s take a look at simple if, then, and else statements. Before the statement, we will include a variable so the user can input a value:

    echo “Enter a number”
    read num
    if [[$num -gt 10]]
    then
    echo “The number is greater than 10”
    else
    echo “The number is not greater than 10”
    

    OK, so that's it. The 5 building blocks of Bash in plain English. Simple, right?!

  • Posted on

    1) Ubuntu

    Ubuntu Logo

    – Ubuntu is by far the most popular Linux distribution with an intuitive GUI (Graphical User Interface) that is easy to learn and very familiar for Windows users.

    – It is essentially Debian-based and easy to install with top-notch commercial support although this is largely irrelevant if you can point and click with a basic understanding of how to interact with applications as you do in Windows.

    – Most preferred Linux distribution for non-tech people.

    Ubuntu Project Home Page

    2) CloudLinux

    CloudLinux – CloudLinux is on a mission to make Linux secure, stable, and profitable.

    – Based solely on the same platform as Red Hat Enterprise Linux for stable releases and usually command prompt based servers.

    – License fees are reasonable for small businesses so it is still a go-to option in the Linux flavour world.

    CloudLinux Project Home Page

    3) Red Hat Enterprise Linux (a.k.a. RHEL)

    Red Hat Enterprise Linux (RHEL) Logo

    – Another most famous and open-source Linux Distribution is Red Hat Enterprise Linux (RHEL). It is a stable, secure yet powerful software suited mostly to the server classification; however it does provide a wealth of tools, apps and other front end software, if not run headless.

    – RHEL was devised by Red Hat for commercial purposes. It offers tremendous support for Cloud, Big Data, IoT, Virtualization, and Containers.

    – Its components are based on Fedora, a community-driven project.

    – RHEL supports 64-bit ARM, Power, and IBM System z machines.

    – The subscription of Red Hat allows the user to receive the latest enterprise-ready software, knowledge base, product security, and technical support.

    Red Hat Enterprise Linux

    4) AlmaLinux

    AlmaLinux

    – Stable and open-source derivative of Red Hat Enterprise Linux, an easy way to run the commercial product in open-source format.

    – A popular free Linux distros for VPS and operationally compatible with RHEL.

    – AlmaLinux is a open-source distribution owned and governed by the community. As such, they are free and focused on the community's needs and long-term stability. Both Operating Systems have a growing community with an increasing number of partners and sponsors.

    – Considered the go-to Operating System of choice since CentOS announced the end-of-life for CentOS 8, in favour of being an upstream provider to RHEL (releasing software before RHEL)

    AlmaLinux Project Home Page

    5) Rocky Linux

    Rocky Linux Logo

    – Similar to AlmaLinux, this OS is a stable and open-source derivative of Red Hat Enterprise Linux. Use open-source instead of paying license fees.

    – A popular free Linux distros for VPS and operationally compatible with RHEL.

    – Rocky Linux is of course open-source and while they don't have the backing financially like AlmaLinux, it's still a worthy community contributed effort.

    Rocky Linux Project Home Page

    6) SUSE

    SUSE Logo

    – The subsequent widespread distribution is SLES which is based on OpenSUSE.

    – Both OpenSUSE & SUSE Linux Enterprise Server have the same parent company – SUSE.

    – SUSE is a german-based open-source software company.

    – The commercial product of SUSE is SLED and OpenSUSE is the non-commercial distro.

    SUSE Project Home Page

    7) Debian

    Debain Logo

    – It is open-source and considered a stable Linux distribution.

    – Ships in with over 51000 packages and uses a unified packaging system.

    – Used by every domain, including Educational Institutions, Companies, Non-profit, and Government organizations.

    – Supports more significant of computer architectures. It includes 64-bit ARM (Aarch64), IBM System z, 32-bit PC (i386), 64-bit PC (amd64), and many more.

    – At last, it is integrated with a bug tracking system. By reading its documentation and content available for web related to Debian helps you in its support.

    Debain Project Home Page


    Welcome to the world of open-source distros relevant today. It is all about the 7 best Linux Distros for VPS Hosting of 2023. Let us know which distribution you or your company using today. If you plan to purchase the Linux VPS Server and are confused between the Linux Distros, connect via the comments or lookup more content on here for some easy learning.