Tag: tutorials

  • Linux Tutorial Series – 150 – Review

    Here is the video version, if you prefer it:

    Let’s review what we learned:

    • Permanent storage device is the physical device your data is stored on
    • Partitions are subdivisions of the whole disk
    • A filesystem is a hierarchy of files and directories you are accustomed to interacting
    • Files and directories are represented as inodes
    • Link is a pointer to a file or a directory
    • Hard links point to the underlying data and symbolic links are like shortcuts
    • Devices (represented as files) are located in the /dev folder
    • Mounting is the process of attaching your device (your device’s storage) in the Linux directory structure; unmounting is the reverse process
    • Unmounting is important because it writes the data in the buffer to the device
    • Hash functions are used for checking file integrity

    Talk soon!

  • Linux Tutorial Series – 149 – A whole bunch of device related information

    Here is the video version, if you prefer it:

    In this post, I will devote a few sentences to device-related topics that, in my opinion, aren’t that important. Don’t get me wrong, you could write a couple of posts on each one of these topics, but they are not that important in your everyday use. Even though you will most likely never do anything with the information presented in this post, it does paint the picture of the Linux operating system and thus I decided to write up a few sentences on each of the topics I thought warranted it. My 2 cents would be: Read through the contents of those posts once, know that these things exist and if you ever need them, use Google to find exactly what you need for your particular purpose. The references are listed in the order in which the topics appear.

    Partitioning disk devices

    You can partition (and re-partition) your permanent storage devices from the command line. Although I never had the need to do this (except when I was installing my operating system), you maybe might, and that’s when you can call the big ol’ Google for help. Just know that you can do this.

    df and free

    The df command tells you how much free space you have on your disk drive. The free command displays the amount of free memory (RAM and swap). (Shotts, 2019)

    Example:

    mislav@mislavovo-racunalo:~/Linux_folder$ df

    Filesystem 1K-blocks Used Available Use% Mounted on

    udev 4001344 0 4001344 0% /dev

    tmpfs 804232 87256 716976 11% /run

    /dev/sda2 483076568 75734076 382733828 17% /

    tmpfs 4021140 43720 3977420 2% /dev/shm

    tmpfs 5120 4 5116 1% /run/lock

    tmpfs 4021140 0 4021140 0% /sys/fs/cgroup

    tmpfs 804228 14316 789912 2% /run/user/1000

    mislav@mislavovo-racunalo:~/Linux_folder$ free

    total used free shared buff/cache available

    Mem: 8042284 4422848 662332 519756 2957104 2791520

    Swap: 8259580 1225432 7034148

    sysfs

    sysfs provides a uniform view for attached devices based on their actual hardware attributes. (Ward, 2014)⁠

    You can go to /sys/devices to see all the devices. It differs from the /dev directory because /dev is designed for interacting with devices, while sysfs is designed to view device information and manage the device.

    lsof

    The lsof command lists open files and the processes using them. By files, I mean both regular files and files that represent other non-files, such as network resources. (Ward, 2014)⁠

    udev

    udev is a device manager for the Linux kernel. (“udev,” n.d.)

    udev manages devices nodes in the /dev directory and also handles all user space events raised when hardware devices are added or removed from the system. Term clarification: user space is the space of the user (different from the kernel space, which only the kernel can access) and you can think of events as messages that are sent when certain actions happen on the system.

    UUID

    UUID, short for Universally Unique Identifier, is a type of serial number used to identify filesystems. (Ward, 2014)⁠

    Displaying a list of mounted filesystems

    To display a list of mounted filesystems, use the mount command without any arguments. (Shotts, 2019)⁠

    An example:

    mislav@mislavovo-racunalo:~$ mount

    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=4001344k,nr_inodes=1000336,mode=755)

    The format of the listing is as follows: device on mount_point type filesystem_type (options) .

    Repairing filesystems

    In order to repair a filesystem, you will most likely use a program called fsck. fsck stands for “file system check”. If you encounter this, you are dealing with a bad problem. May the force of Google and good luck be with you in resolving it!

    What the fsck?

    As I said in the last post, if you ever encounter fsck, that means that you are dealing with a bad issue with your filesystem. Therefore, people sometimes utter the words: “What the fsck?” when dealing with fsck. (Shotts, 2019)⁠

    /etc/fstab

    /etc/fstab lists the devices to be mounted at the time the computer starts (also known as boot time). (Shotts, 2019)⁠

    Here is my /etc/fstab file:

    mislav@mislavovo-racunalo:~$ cat /etc/fstab

    # /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>

    # / was on /dev/sda2 during installation

    UUID=4b74cfa0-d49e-4b15-848b-7dd92f41b018 / ext4 errors=remount-ro 0 1

    # swap was on /dev/sda3 during installation

    UUID=8f39c51a-de75-466c-91a2-f3bf06b5fa89 none swap sw 0 0

    The rows that begin with a # are comments and are to be ignored. The first column is the device (identified by its UUID), the second is the mount point, the third is the file system type, the fourth is the options, the fifth specifies if and when a filesystem is to be backed up with the dump command and the sixth is the order in which filesystems should be checked with the fsck command (they are checked at boot (computer start up) time).

    md5sum – What is it used for?

    Let’s talk about something that you may encounter sometime. It’s the md5sum command. md5sum is a hash function.

    Hash functions are relevant because for different inputs they produce different outputs. Even a slight difference in the input will produce massively different output. You can, for example, use the md5sum command to verify that your file is the same file on the website available for download by calculating the md5sum of your file with the md5sum command and comparing it to the md5sum of the original file (available somewhere on the website where you downloaded the file). If those two match, you can be very confident that you have the same file. If those two do not match, then you have a different file than the original.

    An example:

    mislav@mislavovo-racunalo:~/Linux_folder$ md5sum aba.txt

    51ecc1d1bf86853c0c5c6863f403c10c aba.txt

    That’s it for this post.

    Hope you learned something useful!

    A convenient cheatsheet:

    A reader suggested adding this Linux command cheatsheet in order to help everyone out. The cheatsheet contains more than just device-related commands. It could serve as a good reference to people using the Linux command line, so I put it here.

    References

    Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Page 29; Pages 208-210; Page 220; Pages 206-208

    udev. (n.d.). Retrieved February 12, 2020, from https://en.wikipedia.org/wiki/Udev

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 47-48; Pages 172-174; Pages 76-77

  • Linux Tutorial Series – 148 – The dd command

    Here is the video version, if you prefer it:

    The dd command is used to copy blocks of files from one location to another. (Shotts, 2019) Its syntax is as follows:

    dd if=input_file of=output_file

    You can do cool stuff with it, for example, burn an operating system on a USB: (“How to create a bootable Ubuntu USB flash drive from terminal?,” n.d.)⁠

    Warning: The dd command is very powerful and if you misspecify either the if or the of argument, you can do damage to your devices. Don’t use this command lightly.

    Hope you learned something useful!

    References

    How to create a bootable Ubuntu USB flash drive from terminal? (n.d.). Retrieved February 12, 2020, from https://askubuntu.com/questions/372607/how-to-create-a-bootable-ubuntu-usb-flash-drive-from-terminal

    Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 220-221

  • Linux Tutorial Series – 147 – Why is unmounting important?

    Here is the video version, if you prefer it:

    When you attach a new device to your machine, it is most likely automatically mounted (at least it is on my Debian 10). However, when you want to stop using the device, it is always a good idea to right click on it (in the GUI) and press something along the lines of “Safely remove device”. Why is it so?

    You see, in operating systems, there are these things called buffers. Think of buffers like this. Let’s say you were transporting a bunch of wooden planks. The guy who has the truck to transport it comes to your driveway, takes all the planks at your driveway and drives them to the destination. The problem is that it takes a very long time for the driver to go from your house to the destination. So, if you were to carry planks on a plank-by-plank basis and you always waited for the driver with only one plank in your hands, the job would take a very, very long time. But, you can carry planks on the driveway and once the driver arrives, he picks up all the planks on the driveway and drives them to the destination. In this analogy, your driveway is the buffer.

    This driving wooden planks analogy can be used to explain writing data to an attached device (such as an USB or an external hard drive). Your operating system has a buffer to which it writes the data and then it writes the data from the buffer to your external device, because data can be written much faster from your operating system to the buffer than from the buffer to your external device. The buffer exists to equalize the speed difference.

    So now we come to the reason why unmounting is important – unmounting makes sure that all of the data in the buffer is written to the external device. This happens, again, because writing to the buffer is a lot faster than it is to transfer data from the buffer to an external device.

    Hope you learned something interesting today!

  • Linux Tutorial Series – 146 – Unmounting a device

    Here is the video version, if you prefer it:

    To unmount a device, type in the following command: (Ward, 2014)⁠

    umount mountpoint

    where mountpoint is the mount point of the device. Mind the spelling of the command.

    Hope you learned something useful!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 76

  • Linux Tutorial Series – 145 – Mounting a device

    Here is the video version, if you prefer it:

    In today’s Linux distributions, mounting is usually done automatically. I never had to mount anything manually on my personal computer. However, sometimes you will have to mount something (or a tutorial will ask you to do so), so here is how it is done:

    mount -t type device mountpoint

    As we can see, it is done with the mount where type is the filesystem type (ex4, FAT, NTFS, …), device is the device file (remember the /dev folder discussion) and the mountpoint is the point in our Linux directory structure where we want to place the device (the device’s storage). (Ward, 2014)⁠

    Now you know how to mount a device. Again, you will rarely ever do this, but when you do, you know what you’re doing and what each of those arguments to the mount command mean. Sounds like you had a great Linux teacher!

    Thank you for reading!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 76

  • Linux Tutorial Series – 144 – Mounting and unmounting

    Here is the video version, if you prefer it:

    Today we will talk about something more conceptual yet again. I know that you might be thinking: “Why this conceptual stuff again? It’s been a lot of that lately and I’m tired of that…”. I get it. But be patient – this knowledge will pay off. I am trimming the fat – the things you don’t need to know – but it pays to know these particular concepts. Especially mounting and unmounting because even though you’ll probably never do those manually, there will be some situations where you will have to mount or unmount (an example – “burning” an operating system on an USB) and you have to know what is going on.

    Still with me? Good. So what is mounting? Mounting is the process of attaching your device(your device’s storage)in the Linux directory structure. As we know, everything in Linux starts from the root folder (/). Every other directory is accessible from the root folder by navigating from the root folder to the other folders hierarchically below it. When you insert a new device (say, a USB drive) you have to place it in a directory so that it is accessible from the root directory. The directory in which the new device resides is called its mount point. That process (when you assign a directory accessible from the root folder to a new device) is called mounting. Unmounting is the reverse process from mounting – removing the association between a directory in the directory hierarchy and the device (device’s storage).

    So basically, when you mount, you make your device’s storage accessible to your Linux computer and when you unmount, you make your device’s storage inaccessible. Not all devices that are mounted need to have storage, but that doesn’t matter for our practical applications. (“What is meant by mounting a device in Linux?,” n.d.)⁠

    I modeled my explanation after (“understanding ‘mount’ as a concept in the OS [duplicate],” n.d.)⁠ , which you can read as well for a second perspective.

    Thank you for reading!

    References

    understanding “mount” as a concept in the OS [duplicate]. (n.d.). Retrieved February 12, 2020, from https://unix.stackexchange.com/questions/3247/understanding-mount-as-a-concept-in-the-os

    What is meant by mounting a device in Linux? (n.d.). Retrieved February 12, 2020, from https://unix.stackexchange.com/questions/3192/what-is-meant-by-mounting-a-device-in-linux

  • Linux Tutorial Series – 143 – Device files

    Here is the video version, if you prefer it:

    Device files are files that are actually device I/O (input/output) interfaces. (Ward, 2014)⁠ If you take the trip back down the memory lane, when I was explaining what each Linux directory is for, I said that /dev contains files which represent devices. They are located in the /dev folder. They can also be called device nodes.

    Let’s see what I get with ls -l when I go to /dev:

    mislav@mislavovo-racunalo:/dev$ ls -l

    total 0

    crw-r--r-- 1 root root 10, 235 Jan 2 18:11 autofs

    drwxr-xr-x 2 root root 280 Feb 12 07:48 block

    drwxr-xr-x 2 root root 60 Feb 12 07:48 bsg

    crw-rw---- 1 root disk 10, 234 Feb 11 16:19 btrfs-control

    drwxr-xr-x 3 root root 60 Nov 9 17:37 bus

    drwxr-xr-x 2 root root 3520 Feb 12 07:48 char

    crw------- 1 root root 5, 1 Jan 2 18:11 console

    A bunch of files representing devices.

    There is a special “device” called /dev/null, which doesn’t represent any device, but rather, it represents “void”. If you send any data to it, as in:

    mislav@mislavovo-racunalo:/dev$ ls -l > /dev/null

    nothing happens. More specifically, the kernel ignores the information sent to /dev/null.

    One important thing to note is that not all devices are represented with device files.

    Hope you learned something new!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 46

  • Linux Tutorial Series – 142 – What file systems are out there?

    Here is the video version, if you prefer it:

    We talked about filesystems in a previous article. We said that, approximately, they are comprised of a database which keeps track of files and of data pool, where files are kept.

    Here are some of the more popular filesystems: (“List of file systems,” n.d.)⁠

    • ext4 (fourth extended filesystem), used in Linux
    • FAT (File Allocation Table), used for USBs
    • NTFS (New Technology File System), used in Windows

    References

    List of file systems. (n.d.). Retrieved February 12, 2020, from https://en.wikipedia.org/wiki/List_of_file_systems

  • Linux Tutorial Series – 141 – Symbolic and hard links – explaining the difference with ln

    Here is the video version, if you prefer it:

    We talked about permanent storage devices, partitions, file systems and inodes. Today we are going to put all that knowledge to use and talk about the difference between symbolic and hard links.

    Before we go into this, let me say that this is important to understand. If you ever encounter broken links, you have to know what is going on conceptually. So pay attention.

    As we already know, a file is represented as an inode. More accurately, an inode tells you where the file is in the data pool (among other things), but in essence a file is represented by an inode.

    We also already know what links are – links are pointers to files. There are two kinds of links – symbolic (also called soft links) and hard links. Let’s explain the difference.

    Let’s use an example to illustrate this. This example is modeled after (“What is the difference between a hard link and a symbolic link?,” n.d.)

    I already have two files to play with. Here are their contents:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat ab.txt

    AB

    Mustard is how we transfer wealth

    Ab

    aB

    ab

    mislav@mislavovo-racunalo:~/Linux_folder$ cat a.txt

    A

    a

    aa

    AA

    Aa

    aA

    Some line

    Now let’s create some links (both hard and symbolic):

    mislav@mislavovo-racunalo:~/Linux_folder$ ln ab.txt ab-hard

    mislav@mislavovo-racunalo:~/Linux_folder$ ln -s a.txt a-symbolic⁠

    The first command creates a hard link and the second command (with the -s option) creates a symbolic link.

    We can now access the files with the links to the files:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat ab-hard

    AB

    Mustard is how we transfer wealth

    Ab

    aB

    ab

    mislav@mislavovo-racunalo:~/Linux_folder$ cat a-symbolic

    A

    a

    aa

    AA

    Aa

    aA

    Some line

    Let’s look at ls -l output:

    mislav@mislavovo-racunalo:~/Linux_folder$ ls -l

    total 40

    ...

    -rw-r--r-- 2 mislav mislav 46 Feb 9 16:59 ab-hard

    -rw-r--r-- 2 mislav mislav 46 Feb 9 16:59 ab.txt

    ...

    lrwxrwxrwx 1 mislav mislav 5 Feb 12 07:33 a-symbolic -> a.txt

    -rw-r--r-- 1 mislav mislav 26 Jan 13 05:18 a.txt

    ...

    Look at the second column. It now says that link count for ab.txt is 2, while link count for a.txt is 1. How can this be? Shouldn’t link count for both ab.txt and a.txt be 2?

    No, because hard links point to the underlying data and symbolic links are like shortcuts (shortcuts you may have used in Windows). So hard links point to the underlying data (which is an inode), while symbolic links point to the file.

    I think that the difference is best explained with this figure, modeled after (“What is the difference between a hard link and a symbolic link?,” n.d.)⁠.

    Here’s what happens when we rename ab.txt (the one with the hard link):

    mislav@mislavovo-racunalo:~/Linux_folder$ mv ab.txt ab-new.txt

    mislav@mislavovo-racunalo:~/Linux_folder$ cat ab-hard

    AB

    Mustard is how we transfer wealth

    Ab

    aB

    ab

    Since ab-hard points to the underlying data, it still works, even though the original filename changed.

    Here’s what happens if we rename a.txt (the one with the symbolic link):

    mislav@mislavovo-racunalo:~/Linux_folder$ mv a.txt a-new.txt

    mislav@mislavovo-racunalo:~/Linux_folder$ cat a-symbolic

    cat: a-symbolic: No such file or directory

    Since symbolic links just point to the filename, our a-symbolic link is not working anymore. We now call it a broken link.

    That’s it for today. Hope you learned something useful! Maybe you don’t see it yet, but when you will be dealing with broken links, it will pay greatly that you understand what is going on under the hood.

    References:

    What is the difference between a hard link and a symbolic link? (n.d.). Retrieved February 12, 2020, from https://askubuntu.com/questions/108771/what-is-the-difference-between-a-hard-link-and-a-symbolic-link