<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gabriel Pacheco</title>
    <description>The latest articles on DEV Community by Gabriel Pacheco (@gabi1447).</description>
    <link>https://dev.to/gabi1447</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1439300%2Fa211f1ca-2b8e-43f3-81cd-c71f3cf5a574.jpeg</url>
      <title>DEV Community: Gabriel Pacheco</title>
      <link>https://dev.to/gabi1447</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gabi1447"/>
    <language>en</language>
    <item>
      <title>Creating Partitions and Mounting File Systems in Linux</title>
      <dc:creator>Gabriel Pacheco</dc:creator>
      <pubDate>Thu, 02 May 2024 12:40:49 +0000</pubDate>
      <link>https://dev.to/gabi1447/creating-partitions-and-mounting-file-systems-in-linux-39e9</link>
      <guid>https://dev.to/gabi1447/creating-partitions-and-mounting-file-systems-in-linux-39e9</guid>
      <description>&lt;p&gt;&lt;strong&gt;Block devices, partition tables, filesystem formats, fdisk, mountpoints&lt;/strong&gt;. There's a lot of concepts and commands that are involved and are required in the process of creating a partition in Linux and mounting it to a specific directory. It can be a bit overwhelming, but we are gonna try to dissect the task of creating a partition and mounting it step by step.&lt;/p&gt;

&lt;p&gt;First of all, we'll start by adding a storage device (HDD, SSD) to our machine, in my case, I'm gonna be adding a VDI (virtualbox disk image) to my VM, but if you have linux as your primary OS, you can try adding a physical device, the process it's gonna be the same. Once we got that taken care of, we can start getting our hands dirty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Listing block devices with &lt;code&gt;lsblk&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;We'll start by listing our block devices with the command &lt;code&gt;lsblk&lt;/code&gt;, if we only have one storage device plugged in, it's most probably gonna be identified in the system as &lt;code&gt;sda&lt;/code&gt;, which is where our &lt;code&gt;/boot&lt;/code&gt; partition and &lt;code&gt;/&lt;/code&gt; partition are located. We are gonna be looking for &lt;code&gt;sdb&lt;/code&gt;, we can also identify our block device by checking the size of it, in my case I added a 5GB VDI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwalxuywsnm8xta2f3aw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flwalxuywsnm8xta2f3aw.png" alt="lsblk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Partition tables and allocation of space with &lt;code&gt;fdisk&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Having checked that our storage device has been correctly identified by the OS, we can start by creating the partition table and allocating space to our partitions, in this demo I'll be creating two partitions under the same block device(&lt;code&gt;sdb&lt;/code&gt;). To perform the creation of the partition table in the block device and the allocation of space in each partition we'll be using the command line tool &lt;code&gt;fdisk&lt;/code&gt;. fdisk by default creates a &lt;code&gt;mbr&lt;/code&gt; partion table on our block device, but it also allow us to create a &lt;code&gt;gpt&lt;/code&gt; partitin table, so there's really no need to use &lt;code&gt;gdisk&lt;/code&gt;, but both utilities are very similar. &lt;/p&gt;

&lt;p&gt;What is &lt;code&gt;MBR&lt;/code&gt; and &lt;code&gt;GPT&lt;/code&gt;?&lt;br&gt;
MBR and GPT are two different ways of structuring our partition tables, and a partition table essentially acts like a map, telling the operating system where partitions (sections for your operating system, data, etc.) are located on the disk. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MBR (Master Boot Record):&lt;/strong&gt; The older standard partition table scheme with limitations of a maximum of four primary partitions and a 2 TB partition size limit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GPT (GUID Partition Table):&lt;/strong&gt; The newer and more advanced scheme offering theoretically unlimited partitions(128 technically, as we'll see later with the &lt;code&gt;fdisk&lt;/code&gt; command), support for larger partition sizes, and error correction mechanisms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Looks like a no brainer when it comes to deciding which one to use, but MBR for example has more compatibility with older operating systems, so it depends on what your needs are. For this demo, we'll choose GPT.&lt;/p&gt;

&lt;p&gt;Now that we've discussed a bit of theory let's start creating our partition table and our partition with &lt;code&gt;fdisk&lt;/code&gt;. You've probably heard multiple times that &lt;strong&gt;In Linux EVERYTHING is a file&lt;/strong&gt;, well, storage devices are no exception. Our &lt;code&gt;sdb&lt;/code&gt; block device file is stored under the directory &lt;code&gt;/dev&lt;/code&gt;, so we'll provide that path to the &lt;code&gt;fdisk&lt;/code&gt; command to modify it. We'll also need to provide root privileges to execute the command &lt;code&gt;sudo fdisk /dev/sdb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0405hi1rluphvvo1r2u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0405hi1rluphvvo1r2u.png" alt="fdisk options"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will prompt us a welcome message, and then if we press &lt;code&gt;m&lt;/code&gt;, it will provide us a list of actions to perform on our &lt;code&gt;sdb&lt;/code&gt; disk.&lt;br&gt;
As I said previously, we'll be using a GPT partition table, so we'll press &lt;code&gt;g&lt;/code&gt; to create it under the &lt;code&gt;Create a new label&lt;/code&gt; section.&lt;/p&gt;

&lt;p&gt;Ouput:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcrmitukgkin1xgugp5zc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcrmitukgkin1xgugp5zc.png" alt="creating gpt partition table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can press &lt;code&gt;p&lt;/code&gt; to view our partition table and check if it has been created.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxliu95edvtk9zm0mc96.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxliu95edvtk9zm0mc96.png" alt="printing partition table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can create our two partitions with &lt;code&gt;n&lt;/code&gt;. First it will ask us the partition number that we want to grant between 1-128, since we don't have any partitions created yet we'll select 1 and 2 consecutively. &lt;/p&gt;

&lt;p&gt;Then it will ask us for the first sector(sectors are the most atomic unit of storage in a storage device) where the partition will begin storing data. We can press enter, it will select the default value(the first sector available(2048)), we will do the same for the second partition(this will be a different sector number). &lt;/p&gt;

&lt;p&gt;Finally it will ask for the last sector of the partition, since we're gonna create two, in the first partition we can input &lt;code&gt;+2.5G&lt;/code&gt; to tell fdisk that we want a partition of 2.5G and then in the second partition we can just press enter and it will allocate the rest of the space left to that partition.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0wp31arn4ajguxtahg6x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0wp31arn4ajguxtahg6x.png" alt="creating partitions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we press &lt;code&gt;p&lt;/code&gt; again we'll see both of our partitions created, &lt;code&gt;sdb1&lt;/code&gt; and &lt;code&gt;sdb2&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fviruw3rus0y5fx3ywp5u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fviruw3rus0y5fx3ywp5u.png" alt="checking partitions on partition table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After making all this changes on our &lt;code&gt;sdb&lt;/code&gt; block device we can press &lt;code&gt;w&lt;/code&gt; to write the partition table to the disk and exit.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem1k3c24ejpnj7wrj2gt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fem1k3c24ejpnj7wrj2gt.png" alt="writing table to disk"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Informing the OS of partion table changes
&lt;/h2&gt;

&lt;p&gt;Now if we run a &lt;code&gt;lsblk&lt;/code&gt; again we'll see that our partitions are created:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnrjcqi071jr5m3xs9su.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvnrjcqi071jr5m3xs9su.png" alt="lsblk checking created partitions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But before proceeding with the rest of the demo we'll have to inform the OS of the changes we've made with the &lt;code&gt;sdb&lt;/code&gt; block device, and for that we'll use the command &lt;code&gt;sudo partprobe /dev/sdb&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filesystem formats
&lt;/h2&gt;

&lt;p&gt;Now we are ready to add a filesystem format to each partition. We won't be diving a lot into filesystem formats, but essentially they are responsible for creating a filesystem structure on your storage device mapping your files data to your physical storage space and tracking the location and attributes of files (filename, size, permissions, etc.).&lt;/p&gt;

&lt;p&gt;We will be creating both partitions with &lt;code&gt;ext4&lt;/code&gt; and &lt;code&gt;xfs&lt;/code&gt; filesystem formats. Again, we won't be diving into the types of existing filesystem formats, but ext4 and xfs are the ones that are mostly used due to their &lt;code&gt;journaling&lt;/code&gt; capabilities(they keep a track on disk of every filesystem modification before the actual change is made, in case a crash occurs and the state of the filesystem isn't lost) and also the size of partitions and file sizes they they are compatible with. You don't need to pay to much attention to this information, but knowing a bit more never hurts. My takeaway would be that &lt;code&gt;ext4&lt;/code&gt; is more focused on general purpose use and &lt;code&gt;xfs&lt;/code&gt; could be better if you are handling very large files and require high-performance storage and scalability (big data, media transfer).&lt;/p&gt;

&lt;p&gt;Now, getting back to the demo, we can make use of the &lt;code&gt;mkfs&lt;/code&gt; command to add a filesystem format to our partitions. We will use &lt;code&gt;sudo mkfs.ext4 /dev/sdb1&lt;/code&gt; and &lt;code&gt;sudo mkfs.xfs /dev/sdb2&lt;/code&gt; commands to create our file system formats in each partition. You'll see how the outputs of each command are different depending on the filesystem you chose.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjevstipox51wiregqu4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdjevstipox51wiregqu4.png" alt="filesystem formats"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Mounting file systems
&lt;/h2&gt;

&lt;p&gt;Now that we've created our partition table, our partitions and our filesystem formats in each partition we can mount the filesystems or map them to a specific path in our system. For this we'll create two folders in our &lt;code&gt;/mnt&lt;/code&gt; directory that will be our mountpoints, &lt;code&gt;/mnt/data&lt;/code&gt; and &lt;code&gt;/mnt/backup&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhgdwub1u4dhsdv352yan.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhgdwub1u4dhsdv352yan.png" alt="folders created"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To mount our partitions in these folders we'll make use of the command &lt;code&gt;mount&lt;/code&gt;. We'll be using the &lt;code&gt;xfs&lt;/code&gt; filesystem for the &lt;code&gt;data&lt;/code&gt; directory and the &lt;code&gt;ext4&lt;/code&gt; filesystem for the &lt;code&gt;backup&lt;/code&gt; directory, for that we'll use these commands: &lt;code&gt;sudo /dev/sdb1 /mnt/backup/&lt;/code&gt; and &lt;code&gt;sudo /dev/sdb2 /mnt/data/&lt;/code&gt;. If the commands don't output anything the filesystem mounting will be done. We can check how our partitions point to these directories running &lt;code&gt;lsblk&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgj31wnk8wdhp9kd3s2l0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgj31wnk8wdhp9kd3s2l0.png" alt="filesystems mounted"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we'll be able to store data both on the &lt;code&gt;/mnt/backup&lt;/code&gt; and &lt;code&gt;/mnt/data&lt;/code&gt; directories, but these mountpoints are temporary if we want to mount them permanently we'll have to modify the &lt;code&gt;/etc/fstab&lt;/code&gt; file and add an entry for each mounting point.&lt;/p&gt;

&lt;p&gt;Before entering this file to modify it we'll need the &lt;code&gt;UUIDs&lt;/code&gt; of each filesystem partition. For this we'll use the command &lt;code&gt;blkid&lt;/code&gt;.&lt;br&gt;
We can run &lt;code&gt;sudo blkid /dev/sdb1&lt;/code&gt; and &lt;code&gt;sudo blkid /dev/sdb2&lt;/code&gt; to obtain this parameters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w7fqykos4hrkvohx9gu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w7fqykos4hrkvohx9gu.png" alt="uuids"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can modify the &lt;code&gt;/etc/fstab&lt;/code&gt; file and add the entries for each partition. The entries are structured on the following way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9hewdesc5gk86gh81s5r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9hewdesc5gk86gh81s5r.png" alt="/etc/fstab entries"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1st field&lt;/strong&gt;: UUID of the partition filesystem, in my case the first UUID of the first entry belongs to the &lt;code&gt;/dev/sdb1&lt;/code&gt; partition. Make sure that this UUID matches the correct mountpoint, you can view this using &lt;code&gt;lsblk&lt;/code&gt; if you have forgotten which partition maps to which mounpoint.&lt;br&gt;
&lt;strong&gt;2nd field&lt;/strong&gt;: mountpoint&lt;br&gt;
&lt;strong&gt;3rd field&lt;/strong&gt;: filesystem format&lt;br&gt;
&lt;strong&gt;4th field&lt;/strong&gt;: This refers to a predefined set of commonly used mount options. These typically include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;rw: Allows both reading and writing from the filesystem (read-write access).&lt;/li&gt;
&lt;li&gt;user: Allows regular users (not just root) to mount and unmount the filesystem.&lt;/li&gt;
&lt;li&gt;auto: Instructs the system to automatically mount the filesystem at boot time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5th field&lt;/strong&gt;: This represents the dump frequency and is typically used by the dump command, an older utility for system backups. In modern systems with journaling filesystems (like ext4), this field often holds less significance.  A value of 0 here indicates that the filesystem should not be included in routine backups performed by dump.&lt;br&gt;
&lt;strong&gt;6th field&lt;/strong&gt;: This represents the filesystem check order during the fsck process, a utility that checks filesystems for errors. With journaling filesystems, the order might be less crucial as the journal itself helps maintain consistency. A value of 0 here often signifies that the filesystem should not be automatically checked by fsck at boot time.&lt;/p&gt;

&lt;p&gt;After adding both entries we can save and exit the /etc/fstab file and run the &lt;code&gt;mount -a&lt;/code&gt; command to automatically mount all filesystems specified in the &lt;code&gt;/etc/fstab&lt;/code&gt; file. If we have no output after executing it, we've successfully mounted our file systems permanently.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>learning</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understanding SSH Tunneling</title>
      <dc:creator>Gabriel Pacheco</dc:creator>
      <pubDate>Tue, 30 Apr 2024 12:34:46 +0000</pubDate>
      <link>https://dev.to/gabi1447/understanding-ssh-tunneling-914</link>
      <guid>https://dev.to/gabi1447/understanding-ssh-tunneling-914</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is SSH Tunneling and why is it useful?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Usually when we think of ssh, we think about logging into a remote machine securely using a public/private key pair to execute commands, change configuration files, setup a web server or perform different actions. &lt;/p&gt;

&lt;p&gt;In the case of SSH Tunneling the same thing happens, but with a slight difference. SSH Tunneling also involves establishing a connection between a local machine and a remote server, but the main purpose of this connection is not to directly gain access to the remote server. The remote server acts as an intermediary to forward access to a specific port or host. &lt;/p&gt;

&lt;p&gt;Let's say you are working at home using your PC, but you want to gain access, to a web server, a database or service that is running locally on another private network. Maybe this service that you are trying to gain access to is running on the remote machine, but it's not exposed to the internet, it's running on localhost for security reasons. &lt;/p&gt;

&lt;p&gt;How can you gain access to it then? This is where SSH Tunneling comes into play allowing you to connect to local resources through a "tunnel". As mentioned before, the remote server takes care of forward your request to a specific host and port. Let's dive more into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Local SSH Tunnels and Remote SSH Tunnels
&lt;/h3&gt;

&lt;p&gt;There's two different types of ssh tunnels that we'll be exploring and that can be used depending on what your needs are.&lt;/p&gt;

&lt;h4&gt;
  
  
  Local/Forward SSH Tunnels
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fohu92ahwu28erb1eyii3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fohu92ahwu28erb1eyii3.png" alt="local ssh tunnel diagram" width="800" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Essentially what we achieve by setting up a local ssh tunnel between two hosts, is mapping a specific port in our local machine to a service that is running in the remote server locally with a reserved port, this can be helpful if we want to for example administer a database that is running in a remote machine, but it's not exposed to the internet. It's worth mentioning, sticking to our database example, that the database doesn't need to be run in the remote machine that we are establishing the ssh connection with, the remote server as long as it has access to other machines inside the same private network it can forward the request to that specifig machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F195nasoxtqrsxpx4437n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F195nasoxtqrsxpx4437n.png" alt="local ssh tunnel forwarding" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above we're mapping port 3333 on localhost to port 3306&lt;br&gt;
on the local machine with ip 192.168.1.55 that is running on the same network as the remote machine(44.65.167.34), which is the one responsible to forward our traffic and act as an intermidiary between both, otherwise traffic between this two local machines wouldn't be possible.&lt;/p&gt;

&lt;p&gt;All this process is accomplished by executing the following command in local machine:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -N -L 127.0.0.1:3333:192.168.1.1 44.65.167.34&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With the &lt;code&gt;-N&lt;/code&gt; flag we are specifying to not log in the remote machine shell and execute any commands and with the &lt;code&gt;-L&lt;/code&gt; flag we are specifying that we want to perform a local ssh tunnel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftq1ef9i8hg0lourtn9u4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftq1ef9i8hg0lourtn9u4.png" alt="local ssh tunnel command" width="800" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Remote/Reverse SSH Tunnels
&lt;/h4&gt;

&lt;p&gt;Remote ssh tunnels are slightly complex to grasp in comparison to local ssh tunnels, but deep down, the functionality behind it, still remains the same, you still have a remote server forwarding traffic from A to B, that's basically it.&lt;/p&gt;

&lt;p&gt;Let's say you're running a web server locally, that is not exposed to the internet at home and you want to gain access to it, but at this moment you are at your workplace. If you have a home server setup at your place you can still access your web server, since they both belong to your home private network, even though as previously mentioned, the web server is running on localhost.&lt;/p&gt;

&lt;p&gt;To Accomplish this we will open a port in our home server that then will be mapped directly to the local machine and port that our web server is running on. This way everytime we access our home server on that specific port we will be redirected to our web server that is running locally. Anyone can access your web server this way, so it's not recommended to do this, this is just an example, but you can also think about establishing an ssh connection to your local machine located at home, the functionality is the same, in this case you would establish a ssh connection to your home server using the port that is mapped to your local machine.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftock64xjnmgjdt7kfneu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftock64xjnmgjdt7kfneu.png" alt="remote ssh tunnel diagram" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, the remote machine is sending a request to the remote server ip address on port 8888, and then the server is forwarding the traffic to the host with which it's connected to via ssh.&lt;/p&gt;

&lt;p&gt;To setup a remote ssh tunnel and allow this kind of traffic we will have to gain access to the &lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; file and set the parameter &lt;code&gt;GatewayPorts&lt;/code&gt; to &lt;code&gt;yes&lt;/code&gt;(you can also uncomment it) in the remote server and then run the following command in your local machine in which your web server is running (your web server could be running on another local machine and you can still forward the traffic to it, since they are in the same network, but for the sake of the example we will do it this way):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -N -R 8888:localhost:80 remote-server-ip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43vfl3bcxaotue90io8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F43vfl3bcxaotue90io8v.png" alt="remote ssh tunnel command" width="800" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>ssh</category>
      <category>learning</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
