One place for hosting & domains

      Linux

      How to Create a LEMP Stack on Linux


      LEMP stack refers to a development framework for Web and mobile applications based on four open source components:

      1. Linux operating system
      2. NGINX Web server
      3. MySQL relational database management system (RDBMS)
      4. PHP,
        Perl, or
        Python programming language

      NGINX contributes to the acronym “LEMP” because English-speakers pronounce NGINX as “engine-x”, hence an “E”.

      Before You Begin

      1. If you have not already done so, create a Linode account and Compute Instance. See our
        Getting Started with Linode and
        Creating a Compute Instance guides.

      2. Follow our
        Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

      Note

      The steps in this guide require root privileges. Be sure to run the steps below as root or with the sudo prefix. For more information on privileges, see our
      Users and Groups guide.

      How Does LEMP Differ from LAMP?

      LAMP is just like LEMP, except with Apache in place of NGINX.

      LAMP played a crucial role in the Web for
      over twenty years. NGINX was released publicly in 2004, largely to address faults in LAMP. LEMP use spread widely after 2008, and NGINX is now the second
      most popular Web server, after the
      Apache Web server that LAMP uses.

      Both LEMP and LAMP combine open source tools to supply the essentials for a Web application. This includes an underlying Linux operating system which hosts everything else, including:

      • The NGINX or Apache Web server that receives and responds to end-user actions.
      • The MySQL RDBMS which stores information including user profile, event histories, and application-specific content which has a lifespan beyond an individual transaction.
      • A programming language for business logic that defines a particular application.

      Abundant documentation and rich communities of practitioners make both LEMP and LAMP natural choices for development. The difference between them is confined to the Web server part of the stack.

      Apache Versus NGINX

      In broad terms, the two Web servers have much in common.
      NGINX is faster than Apache, but requires more expertise in certain aspects of its configuration and use, and is less robust on Windows than Apache. Apache works usefully “out of the box”, while, as we see below, NGINX demands a couple of additional steps before its installation is truly usable.

      RDBMS and Programming Language

      Two other variations deserve clarity in regard to the initials “M” and “P”.
      MariaDB is a drop-in replacement for MySQL. The differences between the two are explained in
      this tutorial. Everything you do with MySQL applies immediately with MariaDB as well.

      While several different programming languages work well in a LEMP stack, this guide focuses on PHP. However, nearly all the principles of LEMP illustrated below apply with Python or another alternative.

      LEMP Benefits

      LEMP has a deep track record of successful deliveries. Hundreds of millions of working Web applications depend on it.

      LEMP’s suitability extends beyond purely technical dimensions. Its flexible open-source licensing enables development teams to focus on their programming and operations, with few legal constraints to complicate their engineering.

      Install the LEMP Stack

      Linode’s support for LEMP begins with abundant documentation, including
      How to Install the LEMP Stack on Ubuntu 18.04.

      Rich collections of documentation are available to readers
      new to Linux and its command line. This guide assumes familiarity with the command line and Linux filesystems, along with permission to run as root or with sudo privileges. With the “L” (Linux) in place, the installation in this Guide focuses purely on the “EMP” layers of LEMP.

      Install “E”, “M”, and “P” Within “L”

      Different distributions of Linux require subtly different LEMP installations. The sequence below works across a range of Ubuntu versions, and is a good model for other Debian-based distributions.

      1. Update your host package index with:

        sudo apt-get update -y
        
      2. Now upgrade your installed packages:

        sudo apt-get upgrade -y
        
      3. Install software-properties-common and apt-transport-httpsto manage the PHP PPA repository:

        sudo apt-get install software-properties-common apt-transport-https -y
        
      4. Now provide a reference to the current PHP repository:

        sudo add-apt-repository ppa:ondrej/php -y
        
      5. Update the package index again:

        sudo apt update -y
        
      6. Install the rest of the LEMP stack:

        sudo apt-get install nginx php-mysql mysql-server php8.1-fpm -y
        

      The installation demands a small amount of interaction to give information about geographic location and timezone. Depending on circumstances, you may need to verify the country and timezone your server is located in.

      Start Services

      1. Start the “E” (NGINX), “M” (MySQL), and “P” (PHP) services:

        sudo service nginx start
        sudo service mysql start
        sudo service php8.1-fpm start
        
      2. Check on these services:

        sudo service --status-all
        

        You should see them all running::

        [ + ]  mysql
        [ + ]  nginx
        [ + ]  php8.1-fpm

      Verify PHP

      Verify the healthy operation of these services.

      1. For PHP, launch:

        php -version
        

        You should see:

        PHP 8.1.x (cli) (built: ...
        Copyright © The PHP Group ...
      2. Go one step further with verification of the PHP configuration through the command:

        php -m
        

        The result you see is:

        [PHP Modules]
        calendar
        Core
        ...
        mysqli
        mysqlnd
        ...

      This demonstrates that PHP is installed and that the modules needed to communicate with the rest of the LEMP stack are in place.

      Verify NGINX

      Verification of NGINX service is a little more involved. The first step is
      identification of the IP address of the host.

      1. Navigate a browser to a URL such as http://localhost or http://23.77.NNN.NNN, henceforth referred to as $LEMP_HOST

        Your Web browser shows a default display of:

        Welcome to nginx!
        If you see this page, the nginx web server is successfully installed and working.  ...
      2. With the default NGINX configuration verified, update it to enable PHP. Edit the file located at /etc/nginx/sites-enable/default and change this section:

        File: /etc/nginx/sites-enabled/default
        1
        2
        3
        4
        5
        
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        To become:

        File: /etc/nginx/sites-enabled/default
        1
        2
        3
        4
        5
        6
        7
        8
        9
        
        location / {
               # First attempt to serve request as file, then
               # as directory, then fall back to displaying a 404.
               try_files $uri $uri/ =404;
        }
        location ~ \.php {
               include snippets/fastcgi-php.conf;
               fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        }
      3. Back at the Linux command line, activate this new configuration with:

        service nginx restart
        
      4. Next, ensure that NGINX communicates with PHP by creating the file /var/www/html/php-test.php with contents:

        File: /var/www/html/php-test.php
        1
        2
        3
        
        <?php
        phpinfo();
        ?>
        
      5. Now direct your browser to http://$LEMP_HOST/php-test.php.

        Your browser shows several pages of diagnostic output, starting with:

        PHP Version 8.1.9
           System Linux ... 5.10.76-linuxkit #1 SMP Mon Nov 8 ...
           ...

      The location of /var/www/html/php-test.php is configurable. This means that a particular distribution of Linux and NGINX might designate a different directory. /var/www/html is common, especially for a new Ubuntu instance with NGINX “out of the box”. In practice, it’s common to modify the NGINX default a great deal. You can allow for tasks such as caching, special handling for static requests, virtual hosts, and logging security.

      Verify MySQL

      When you install MySQL according to the directions above, it doesn’t depend on authentication.

      1. No password is required. You only need one command:

        mysql
        

        And you see:

        Welcome to the MySQL monitor ...
      2. You can leave the MySQL monitor and return to the Linux command line with:

        \q
        

      Your LEMP stack is now installed, activated, and ready for application development. For a basic LEMP installation, this consists of placing programming source code in the /var/www/html directory, and occasionally updating the configurations of the LEMP layers.

      Use the LEMP Stack to Create an Example Application

      You can create a minimal model application that exercises each component and typical interactions between them. This application collects a record of each Web request made to the server in its backend database. A more refined version of this application could be used to collect:

      • Sightings of a rare bird at different locations.
      • Traffic at voting stations.
      • Requests for customer support.
      • Tracking data for a company automobile.

      The configuration and source below apply to LEMP environments. Even if your LEMP stack used different commands during installation, the directions that follow apply with a minimum amount of customization or disruption.

      Prepare a Database to Receive Data

      Start application development by configuring the database to receive program data.

      1. Re-enter the MySQL monitor with:

        mysql
        
      2. While connected to MySQL, create a database instance specific to this development:

        CREATE DATABASE model_application;
        
      3. Enter that database with:

        USE model_application;
        
      4. Define a table for the program data:

        CREATE TABLE events (
            timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            client_ip INT(4) UNSIGNED NOT NULL
        );
        
      5. Create a database account:

        CREATE USER 'automation'@'localhost' IDENTIFIED BY 'abc123';
        
      6. Now allow PHP to access it:

        GRANT ALL PRIVILEGES ON model_application.* TO 'automation'@'localhost' WITH GRANT OPTION;
        
      7. Quit MySQL:

        \q
        

      A polished application uses tighter security privileges, but this sample application adopts simple choices to maintain focus on the teamwork between the different LEMP layers.

      Create Application Source Code

      Create /var/www/html/event.php with the following content:

      File: /var/www/html/event.php
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      
      <?php
          $connection = new mysqli("127.0.0.1", "automation", "abc123", "model_application");
          $client_ip = $_SERVER['REMOTE_ADDR'];
          // INET_ATON() packs an IPv4 string representation into
          // four octets in a standard way.
          $query = "INSERT INTO events(client_ip)
      VALUES(INET_ATON('$client_ip'))";
          $connection->query($query);
          echo 'Your request has successfully created one database record.';
      ?>
      

      Verify Operation of the Application

      1. event.php is the only program source code for our minimal model application. With it in place, instruct your browser to visit http://$LEMP_HOST/event.php.

        You should see:

        Your request has successfully created one database record.
      2. You can also exercise the application from different remote browser connections. With a different browser, perhaps from a different desktop, again navigate to http://$LEMP_SERVER/event.php.

      View Collected Data

      The model application exhibits the expected behavior from a Web application and your browser reports success. Viewed through the Web browser, the application does the right thing.

      1. To confirm it updated the database, re-enter the MySQL monitor:

        mysql
        
      2. Enter the example application database:

        USE model_application;
        
      3. Pose the query:

        select timestamp, inet_ntoa(client_ip) from events;
        

        You should see output such as:

        +---------------------+----------------------+
        | timestamp           | inet_ntoa(client_ip) |
        +---------------------+----------------------+
        | 2022-08-03 02:26:44 | 127.0.0.1            |
        | 2022-08-03 02:27:18 | 98.200.8.79          |
        | 2022-08-05 02:27:23 | 107.77.220.62        |
        +---------------------+----------------------+

      This demonstrates the flow of data from a Web browser to the database server. Each row in the events table reflects one request from a Web browser to connect to the application. As the application goes into practical use, rows accumulate in the table.

      Application Context

      LEMP is a trustworthy basis for Web development, with decades of successful deliveries over a range of requirements. It directly supports only
      server-side processing. The model application above delivers pure HTML to the browser. However, LEMP is equally capable of serving up CSS and
      JavaScript, but does not build in tooling for these client-side technologies. Projects reliant on elaborate user interface effects usually choose a framework focused on the client side.
      React is an example of such a framework.

      Server-side orientation remains adequate for many applications, and LEMP fits these well. Server-side computation typically involves several functions beyond the model application above, including:

      • Account Management
      • Forms Processing
      • Security Restrictions
      • Analytic and Cost Reporting
      • Exception Handling
      • Quality Assurance Instrumentation

      Contemporary applications often build in a
      model-view-controller (MVC) architecture, and/or define a
      representational state transfer (REST) perspective. A commercial-grade installation usually migrates the database server to a separate dedicated host. Additionally, high-volume applications often introduce load balancers, security-oriented proxies,
      content delivery network (CDN) services, and other refinements. These functions are layers over the basic data flow between user, browser, business logic processing, and datastore that the model application embodies. The model application is a good first example.

      Conclusion

      You just installed a working LEMP stack, activated it, and created a model application. All the needs of a specific Web application have a place in this same model.



      Source link

      Configure Linux as a Router (IP Forwarding)


      A computer network is a collection of computer systems that can communicate with each other. To communicate with a computer that’s on a different network, a system needs a way to connect to that other network. A router is a system that acts as a intermediary between multiple different networks. It receives traffic from one network that is ultimately destined for another. It’s able to identify where a particular packet should be delivered and then forward that packet over the appropriate network interface.

      There are lots of options for off-the-shelf router solutions for both home and enterprise. In most cases, these solutions are preferred as they are relatively easy to configure, have lots of features, tend to have a user-friendly management interface, and may come with support options. Under the hood, these routers are stripped down computers running common operating systems, like Linux.

      Instead of using one of these pre-built solutions, you can create your own using any Linux server, like a Linode Compute Instance. Using routing software like iptables, you have total control over configuring a router and firewall to suit your individual needs. This guide covers how to configure a Linux system as a basic router, including enabling IP forwarding and configuring iptables.

      Use Cases for a Cloud-based Router

      Many workloads benefit from custom routing or port forwarding solutions, including those workloads hosted on cloud platforms like Linode. For example, it’s common practice for security-minded applications to connect most of their systems together through a private network, like a VLAN. These systems might need access to an outside network, like other VLANs or the public internet. Instead of giving each one their own interface to the other network, one system on the private network can act as a router. The router is configured with multiple network interfaces (one to the private VLAN and one to other network) and forwards packets from one interface to another. This can make monitoring, controlling, and securing traffic much easier, as it can all be done from a single system. Linode Compute Instances can be configured with up to 3 interfaces, each connecting to either the public internet or a private VLAN.

      • Connect systems on private VLAN to the public internet.
      • Connect systems on two separate private VLANs.
      • Forward IPv6 addresses from a /56 routed range.

      Configure a Linux System as a Router

      1. Deploy at least 2 Compute Instances (or other virtual machines) to the same data center. All systems should be connected to the same private network, like a
        VLAN. One system should be designated as the router and should also be connected to the public internet or a different private network. See
        Deploy Compute Instances.
      2. Enable IP forwarding on the Compute Instance designated as the router. See
        Enable IP Forwarding.
      3. Configure the routing software on that same instance (the router). This guide covers using iptables, but you can also use other software. See
        Configure iptables.
      4. Define a gateway on each system other than the router. This gateway should point to the router’s IP address on that network. See
        Define the Gateway.

      Deploy Compute Instances

      To get started, you can use the Linode platform to deploy multiple Compute Instances. These can mimic a basic application that is operating on a private VLAN with a single router. If you already have an application deployed and just wish to know how to configure ip forwarding or iptables, you can skip this section.

      1. Deploy 2 or more Compute Instances and designate one as the router. Each of these should be deployed to the same region. On the deployment page, you can skip the VLAN section for now. See
        Creating a Compute Instance to learn how to deploy Linode Compute Instances.

      2. On each Compute Instance other than the router, edit the instance’s configuration profile. See
        Managing Configuration Profiles for information on viewing and editing configuration profiles.

        • On the Compute Instance designated as the router, leave eth0 as the public internet and set eth1 to be configured as a VLAN. Enter a name for the VLAN and assign it an IP address from whichever subnet range you wish to use. For instance, if you wish to use the 10.0.2.0/24 subnet range, assign the IP address 10.0.2.1/24. By convention, the router should be assigned the value of 1 in the last segment.
        • On each Compute Instance other than the router, remove all existing network interfaces. Set eth0 as a VLAN, select the VLAN you just created, and enter another IP address within your desired subnet (such as 10.0.2.2/24 and 10.0.2.3/24).
      3. Confirm that
        Network Helper is enabled and reboot each Compute Instance for the changes to take effect.

      4. Test the connectivity on each Compute Instance to ensure proper configuration. Log in to each instance and confirm the following is true:

        • Ping the VLAN IPv4 address of another system within the same VLAN. Each Compute Instance should be able to ping the IP addresses of all other instances within that VLAN.

          ping 10.0.2.1
          
        • Ping an IP address or website of a system on the public internet. This ping should only be successful for the Compute Instance configured as the router.

          ping linode.com
          

      Enable IP Forwarding

      IP forwarding plays a fundamental role on a router. This is the functionality that allows a router to forward traffic from one network interface to another network interface. In this way, it allows computers on one network to reach a computer on a different network (when configured along with routing software). Forwarding for both IPv4 and IPv6 addresses are controlled within the Linux kernel. The following kernel parameters are used to enable or disable IPv4 and IPv6 forwarding, respectively.

      • IPv4: net.ipv4.ip_forward or net.ipv4.conf.all.forwarding
      • IPv6: net.ipv6.conf.all.forwarding

      By default, forwarding is disabled on most Linux systems. To configure Linux as a router, this needs to be enabled. To enable forwarding, the corresponding parameter should be set to 1. A value of 0 indicates that forwarding is disabled. To update these kernel parameters, edit the /etc/sysctl.conf file as shown in the steps below.

      1. Log in to the Linux system you intend to use as a router. You can use
        SSH or
        Lish (if you’re using a Linode Compute Instance).

      2. Determine if IPv4 forwarding is currently enabled or disabled. The command below outputs the value of the given parameter. A value of 1 indicates that the setting is enabled, while 0 indicates it is disabled. If you intend to configure IPv6 forwarding, check that kernel parameter as well.

        sudo sysctl net.ipv4.ip_forward
        

        If this parameter is disabled (or otherwise not in the desired state), continue with the instructions below.

      3. Open the file /etc/sysctl.conf using your preferred command-line editor, such as
        nano.

        sudo nano /etc/sysctl.conf
        
      4. Find the line corresponding with the type of forwarding you wish to enable, uncomment it, and set the value to 1. Alternatively, you can add the lines anywhere in the file.

        File: /etc/sysctl.conf
        1
        2
        3
        4
        5
        6
        7
        
        ...
        ## Configure IPv4 forwarding
        net.ipv4.ip_forward = 1
        
        ## Configure IPv6 forwarding
        net.ipv6.conf.all.forwarding = 1
        ...
      5. After the changes have been saved, apply the changes by running the following command or by rebooting the machine.

        sudo sysctl -p
        

      Configure iptables

      The iptables utility can serve as both a firewall (through the default filter table) and as a router (such as when using the nat table). This section covers how to configure iptables to function as a basic router. If you prefer, you can use any other firewall or routing software, such as
      nftables or a commercial application.

      1. Log in to the Linux system you intend to use as a router. You can use
        SSH or
        Lish (if you’re using a Linode Compute Instance).

      2. Review the existing iptables rules. If you are on a fresh installation of Linux and do not have any preconfigured rules, the output of the below command should by empty.

        iptables-save
        

        If do receive output, look for any rules that might interfere with your intended configuration. If you are unsure, you may want to consult your system administrator or the
        iptables documentation. If needed, you can flush your iptables rules and allow all traffic.

        iptables -F
        iptables -X
        iptables -t nat -F
        iptables -t nat -X
        iptables -t mangle -F
        iptables -t mangle -X
        iptables -P INPUT ACCEPT
        iptables -P OUTPUT ACCEPT
        iptables -P FORWARD ACCEPT
        
      3. Configure iptables to allow port forwarding. This is the default setting for many systems.

        iptables -A FORWARD -j ACCEPT
        
      4. Next, configure NAT (
        network address translation) on iptables. This modifies the IP address details in network packets, allowing all systems on the private network to share the same public IP address of the router. Add the following iptables rule, replacing 10.0.2.0/24 with the subnet of your private VLAN.

        iptables -t nat -s 10.0.2.0/24 -A POSTROUTING -j MASQUERADE
        

        You can also forgo specifying any specific subnet and allow NAT over all traffic by using the command below.

        iptables -t nat -A POSTROUTING -j MASQUERADE
        
      5. By default, iptables rules are ephemeral. To make these changes persistent, install the iptables-persistent package. When you do this, the rules saved within /etc/iptables/rules.v4 (and rules.v6 for IPv6) are loaded when the system boots up. You can continue making changes to iptables as normal. When you are ready to save, save the output of
        iptables-save to the /etc/iptables/rules.v4 (or rules.v6) file. For more information, see the relevant section with the
        Controlling Network Traffic with iptables guide.

        iptables-save | sudo tee /etc/iptables/rules.v4
        

      Define the Gateway

      The last step is to manually adjust the network configuration settings for each Compute Instance other than the router.

      1. Log in to the
        Cloud Manager and disable
        Network Helper for each non-router Compute Instance you’ve deployed. While Network Helper was useful for automatically configuring the VLAN IP addresses, the configuration files controlled by Network Helper now need to be manually edited.

      2. Log in to each Linux system that is not designated as the router. You can use
        SSH or
        Lish (if you’re using a Linode Compute Instance).

      3. Edit the configuration file that contains the settings for the private VLAN interface. This name and location of this file depends on the Linux distribution you are using. See the
        Manual Network Configuration on a Compute Instance series of guides and select the specific guide for your distribution. For a system running
        ifupdown on Debian 10, the network configuration is typically stored within /etc/network/interfaces.

        sudo nano /etc/network/interfaces
        
      4. Within this file, adjust the parameter that defines the gateway for the VLAN interface. The value should be set to the IP address assigned to the router’s VLAN interface, such as 10.0.2.1 if you’ve used the example in this guide. For a system running
        ifupdown on Debian 10, you can add the gateway parameter in the location shown in the example below.

        File: /etc/network/interfaces
        1
        2
        3
        4
        
        ...
        iface eth0 inet static
            address 10.0.2.2/24
            gateway 10.0.2.1
      5. After those settings have been saved, restart the Compute Instance or run the corresponding command to apply the changes. Continuing to use
        ifupdown as an example, run the command below to apply the new network configuration settings.

        sudo ifdown eth0 && sudo ip addr flush eth0 && sudo ifup eth0
        

      Test the Connection

      To verify the configuration settings are correct, run the same tests that were used within the last step of the
      Deploy Compute Instances section. Specifically, ping a public IP address or domain from a Compute Instance within the private VLAN (that’s not designated as the router). This ping should now complete successfully, indicating that the network traffic was successfully forwarded through the router to the public internet.

      ping linode.com



      Source link

      How to Mount a File System on Linux


      Mounting or unmounting a file system on Linux is usually straightforward, except when it isn’t. This article teaches you how to mount and unmount file systems, as well as list available and currently mounted file systems. It also explains how to handle the case where file systems won’t unmount because they are in use.

    • You can list the currently mounted file systems from a Linux command line with a simple mount command:

      mount
      

      The following is on an Ubuntu 22.04 LTS Linode, logged in as root:

      sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
      proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
      udev on /dev type devtmpfs (rw,nosuid,relatime,size=441300k,nr_inodes=110325,mode=755,inode64)
      devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
      tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=99448k,mode=755,inode64)
      /dev/sda on / type ext4 (rw,relatime,errors=remount-ro)
      securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
      tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)
      tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k,inode64)
      cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
      pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
      bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
      systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18031)
      hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
      mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
      debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime)
      tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,noexec,relatime)
      fusectl on /sys/fs/fuse/connections type fusectl (rw,nosuid,nodev,noexec,relatime)
      configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,noexec,relatime)
      none on /run/credentials/systemd-sysusers.service type ramfs (ro,nosuid,nodev,noexec,relatime,mode=700)
      tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=99444k,nr_inodes=24861,mode=700,inode64)
    • You can list the static file system information by displaying /etc/fstab:

      cat /etc/fstab
      

      The two static file systems for this instance are the root disk and the swap disk:

      # /etc/fstab: static file system information.
      #
      # Use 'blkid' to print the universally unique identifier for a
      # device; this may be used with UUID= as a more robust way to name devices
      # that works even if disks are added and removed. See fstab(5).
      #
      # <file system> <mount point>   <type>  <options>       <dump>  <pass>
      /dev/sda        /               ext4    errors=remount-ro 0     1
      /dev/sdb        none            swap    sw                0     0
    • You can also list and search for file systems using the findmnt command:

      findmnt
      

      The basic output shows the file system tree:

      TARGET                                SOURCE     FSTYPE     OPTIONS
      /                                     /dev/sda   ext4       rw,relatime,errors=remount-ro
      ├─/sys                                sysfs      sysfs      rw,nosuid,nodev,noexec,relatime
      │ ├─/sys/kernel/security              securityfs securityfs rw,nosuid,nodev,noexec,relatime
      │ ├─/sys/fs/cgroup                    cgroup2    cgroup2    rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot
      │ ├─/sys/fs/pstore                    pstore     pstore     rw,nosuid,nodev,noexec,relatime
      │ ├─/sys/fs/bpf                       bpf        bpf        rw,nosuid,nodev,noexec,relatime,mode=700
      │ ├─/sys/kernel/debug                 debugfs    debugfs    rw,nosuid,nodev,noexec,relatime
      │ ├─/sys/kernel/tracing               tracefs    tracefs    rw,nosuid,nodev,noexec,relatime
      │ ├─/sys/fs/fuse/connections          fusectl    fusectl    rw,nosuid,nodev,noexec,relatime
      │ └─/sys/kernel/config                configfs   configfs   rw,nosuid,nodev,noexec,relatime
      ├─/proc                               proc       proc       rw,nosuid,nodev,noexec,relatime
      │ └─/proc/sys/fs/binfmt_misc          systemd-1  autofs     rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18031
      ├─/dev                                udev       devtmpfs   rw,nosuid,relatime,size=441300k,nr_inodes=110325,mode=755,inode64
      │ ├─/dev/pts                          devpts     devpts     rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
      │ ├─/dev/shm                          tmpfs      tmpfs      rw,nosuid,nodev,inode64
      │ ├─/dev/hugepages                    hugetlbfs  hugetlbfs  rw,relatime,pagesize=2M
      │ └─/dev/mqueue                       mqueue     mqueue     rw,nosuid,nodev,noexec,relatime
      └─/run                                tmpfs      tmpfs      rw,nosuid,nodev,noexec,relatime,size=99448k,mode=755,inode64
        ├─/run/lock                         tmpfs      tmpfs      rw,nosuid,nodev,noexec,relatime,size=5120k,inode64
        ├─/run/credentials/systemd-sysusers.service
        │                                   none       ramfs      ro,nosuid,nodev,noexec,relatime,mode=700
        └─/run/user/0                       tmpfs      tmpfs      rw,nosuid,nodev,relatime,size=99444k,nr_inodes=24861,mode=700,inode64
    • You can restrict the output various ways, as described in man findmnt, to show only specific devices, mount points, or file system types, such as:

      findmnt -t ext4
      

      This lists only ext4 file systems:

      TARGET SOURCE   FSTYPE OPTIONS
      /      /dev/sda ext4   rw,relatime,errors=remount-ro
    • If you’re only interested in block devices, you can list them with lsblk:

      lsblk
      

      Once again, this only lists our Linode’s root and swap disks:

      NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
      sda    8:0    0 24.5G  0 disk /
      sdb    8:16   0  512M  0 disk [SWAP]
    • You can mount file systems for a single session using the mount command, and permanently by editing /etc/fstab. Mounting needs to be done by an administrator, either by logging in as root or by using the sudo command. There are some cases where mounting is done automatically, like when you insert a USB flash drive. Here are a few examples using the mount command, plus the preparatory mkdir command to create the mount point.

      Most modern distros automatically mount USB drives when you insert them.

      The network file system (NFS) supports mounting remote file systems as shares for local access.

      You can add the -l (lazy) switch to umount to instruct the system to unmount the device when it’s free. Alternatively, the -f (force) switch makes the system unmount the device right away, at the possible risk of corrupting the file system. The -f switch is primarily intended to unmount unreachable NFS shares.

      Mounting a file system on Linux is generally a straightforward two-step process: create a mount point directory, and use the mount command to mount the device at the mount point. Unless the file system is in use, unmounting is even simpler, requiring only the umount command. File system mounting and unmounting requires you to be logged in as root, or use the sudo prefix to temporarily take on root privileges.

      You may wish to consult the following resources for additional information
      on this topic. While these are provided in the hope that they will be
      useful, please note that we cannot vouch for the accuracy or timeliness of
      externally hosted materials.



      Source link