Linux Essentials - Automatically mounting storage volumes with /etc/fstab

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today's video is proudly sponsored by lenode the note has been doing cloud computing since 2003 which is actually before amazon web services was even a thing on the notes platform you can get your server up and running in minutes and they include all the popular distributions such as debian fedora ubuntu and get this even arch linux and let's be honest what could be better than a linux focus cloud server provider that lets you tell all of your friends i run arch the note has multiple server plans available to make any app scalable and flexible you could use it to host a blog a vpn server a minecraft server and much more in fact lenode is the platform of choice to host the entire web presence of learnlinux tv in addition the note offers 24x7 365 support regardless of plan size so you can get help from a live person when you need it new users can get started right now with one hundred dollars towards your new account and i highly recommend you check them out because lenode is awesome and now let's get started with today's video [Music] [Music] hello again everyone and welcome back to learn linux tv in today's video we're going to talk about storage again because in a previous video i gave you guys all the basic details of getting started with adding additional storage volumes to your linux system things like formatting a file system mounting a volume things like that but what i didn't show you guys how to do yet was how to set it up such that your volumes will be automatically mounted when you start up your computer or your linux server and that's going to be the subject of today's video now before we get into the topic at hand i just want to mention real quick that i love making content for you guys it's incredibly fun i love doing these videos i have a blast and i love linux so it all works out but as much as i love this channel it's a lot of hard work so if you are able to become a patron and support this channel i would really appreciate it that's what matthew michael did he became a sponsor and chose the tier that provides a shout out as one of the perks and i'm giving him that shout out right now hi matthew and just like matthew you could become a patron and support the channel i would really appreciate it and among the other perks you can get early access to select videos and also add free versions of videos as well and this video right here was both an ad free video and an early access video so for patrons they got two benefits from this video all in one so go ahead and check out the patreon page for learnlinux tv i would really appreciate that now let's go ahead and get into the topic at hand and find out how to automatically have our storage volumes mounted when we start our server all right so here i am on my linux terminal and i'm ready to get started now before we actually get started there's a quick disclaimer that i need to give you guys and i think this is an important disclaimer anytime we're actually working with storage be very careful when working with storage if you enter any commands incorrectly you can accidentally wipe the wrong device your system might not boot the next time we need to be very careful on my end i'm not responsible for any data loss if you make a mistake or even if you do something wrong on purpose whatever the case is just be careful have good attention to detail and you should be fine secondly in order to follow along if you are going to follow along you could just watch but if you're going to follow along make sure you have an extra storage volume available and that you don't care about any of the data on that volume it doesn't really matter what kind of storage you add it could be a flash drive if it's a virtual machine you could add a new virtual disk all that matters is that it's disposable that it doesn't have any data on it that you want to keep you're free to remove everything on it and that's fine on my end i'm going to use a flash drive for the example in this video again it doesn't matter in fact if i enter the lsblk command you can see right here that we have disk sdb attached to the system it's not mounted if it was i would have had a mount point right about here but i don't i went ahead and unmounted it before i hit the record button i have one partition on that flash drive sdb1 as you see here and anytime you enter commands on your end just make sure you are checking the device name it'll most likely be different if you target the wrong device bad things will happen but anyway as you can see here i have a 16 gig flash drive with one partition and the partition itself is formatted with ext4 that's what i decided to go along with so on your end just make sure you have an ext4 file system ready to go if you don't make sure you watch my storage video like i mentioned earlier this video is actually going to piggyback off of that video normally the videos in this series can be watched in any order but this is just one of those exceptions where you really should watch that storage video first because the content that i'm about to go over piggybacks off of that video so here we have sdb and what i want to do is automatically mount this particular partition every time i start up the computer now what i'm going to do is enter the mount command and as you'll remember from the previous video the mount command will give you a listing of everything that's mounted but there's quite a few things beyond storage so it's going to be quite a bit of output so i am going to grep for sdb again you can use the lsblk command to find out what your device name actually is i'm just grabbing for that device name and we can see that it's not mounted that's important because we want to make sure that we mount the file system first and that we're able to access it before we go ahead and automate the process of mounting that at boot time but when it comes to storage we can't actually use it until we mount it and we can't even mount it until we have a designated place to mount it to a directory is basically what we need to attach storage to and that directory it can be anywhere now as far as which directory in particular you should mount your storage volumes too there's always a bit of debate when it comes to things in linux and that's no exception there's two directories like i've gone over in the previous video on storage there's slash mnt and then there's also the directory slash media generally speaking slash media is for your temporary storage volumes you know things like flash drives that you might not always have attached basically want to plug in a flash drive use it and then remove it but when it comes to our use case we want to use the slash mnt or slash mount folder like you see here that's going to be the target so now we know where to mount it but how do we actually set that up there's a dedicated file for this purpose and it's known as the fstab file you might also hear it referred to as the fstab file if i cap the contents of it it's located at slash etsy fstab like you see here the content is wrapped because the font size is really large so it's not going to make any sense at all right now so i think it's better if i open it in an editor i'm going to need to use sudo because well i need to have root privileges in order to edit that particular file i'm going to use nano and in case you're curious vim is my preferred text editor but nano is a bit easier to explain in videos that's why i use that then the file that i want to edit is slash etsy slash fstab like you see here now i'm going to drop the font size a bit here and i don't want to make it too much smaller than that because it might be hard to see but i think it's easier to understand this file if the lines are not wrapping so we'll just work with this so what is the fstab file anyway if you're not already aware the fstab file is responsible for automatically mounting storage volumes when you start your linux computer or server each one of these lines is a different storage volume you could actually mount disks here network storage volumes there's all kinds of things that you can mount with the fstab file and you're going to have some things here automatically because well the root file system as we see here is part of this file so if i was to comment out this bottom line right here where it mounts the root file system my server or computer will not boot it just won't it won't be able to find the root file system and that's an important takeaway here if you make a mistake your computer will not boot so just be careful you absolutely won't want to comment out this line in fact you'll probably not want to edit any of these lines here because we want to leave those lines alone the only thing we want to do at this point is add a new line to this file now what i like to do on my end is add a line break in between the content that's already here and the content that i'm about to add and there's actually two ways that we can automatically mount a file system i'm going to show you the easiest way or simplest way and then i'll show you a different method utilizing a uuid that's a little bit more involved it's not too bad it is the preferred way so just bear with me and i mentioned uuids we can see a few of them here but we'll get to that we're not really worried about what those mean just yet now what you want to do is add the storage volume to a new line in this file to have it automatically be mounted and the first thing you're going to do is type the device name and its location so in my case it was slash dev sdb1 and the next thing you do is you type a location where you want it to be mounted to so in my case i'm going to type slash mnt and then i'm going to name it my disk now i didn't create this directory yet so we don't want to actually have this mount anything just yet at least until i create this directory but that's where i want to mount this particular volume to and we'll create this directory shortly even though we're not going to change the lines up here you can kind of follow along with those lines to understand what i'm about to do so i mentioned that this is the root file system so this field right here is going to be associated with the target directory basically where you want to mount the storage volume to and i'm following the same format down here so then if you look at this that's a file system we're clarifying that this particular disk is formatted with ext4 now we already know that slash dev sdb1 is formatted with ext4 we saw that in the output a while back and i also mentioned earlier in the video that i went along with ext4 for that particular storage volume so we know that we should type ext4 right here if we type something incorrect it's not going to mount it next i'm going to add the options for this particular volume now in my case i'm just going to add defaults now i'm not going to go over all the various options that you can have in this field that would be a very long video but defaults gives you well the defaults but what does it mean exactly when i include defaults right here well actually defaults is inclusive of multiple other options i'm just going to paste them right here you don't have to type these in on your end i'm just giving you an example according to debian's documentation defaults refer to all of these options you can see that each option is separated by a comma now a few of these we will be going over some of them we won't at this point just understand that the options that you apply here control the type of access that you give for example our w is read write and i think you know exactly what that means and then there's other options here but it's much easier to just include defaults and as you can see i'm just following along here we have some additional options but we're not going to go too far into detail on those and then for swap of course we have defaults which i actually mirrored right here that's a good way to do it if you don't have any specific preferences then default is probably the best thing to add here next what i'm going to do is add this field right here and then also this one but what i'm going to do instead is change that last character to a 1. but what exactly do those numbers mean in the first place so this field right here refers to dump and it's not something that we really use all that much anymore so i'm not going to go into too much detail there i think you'll probably almost if not always see that set to zero that just determines whether or not the entire file system needs to be dumped but more specifically what dump has to do with is backup again we don't actually use that so much anymore but that's what it's for the last digit here refers to the order of file system integrity checking basically when you have a problem or when fsck the file system checker needs to run it's going to basically run in a certain order zero means it's not going to run it's just not really going to check it at all and you might have seen this on other linux distributions where sometimes if you start it up it's doing a file system check maybe a routine file system check or if it lost power it shut off abruptly maybe it's going to be doing an integrity check on the next startup just to make sure everything is fine but there's a certain order you want that to happen in so the root file system should always be first so technically we could argue and i'm not going to get into distribution choices here but this last digit here on this line this one right here i changed it to a one that means it's going to be the very first thing that's checked if there's a problem that makes sense it's the root file system it's the most important file system it should be the one that's checked first if anything has to be checked so here i probably shouldn't set this to one this isn't the root file system i can set it to two instead as i'm doing right here i'll leave that up to you but what i'm going to do is just revert everything to zero you can always run a file system check manually if you want to run one and now at this point we've gone over every field in the fstab file now i'm going to save this file in the case of nano bringing up the save dialog was ctrl o and now to exit is control x we definitely don't want to reboot at this point because well i didn't create that directory yet that's not good we definitely want to make sure that we create that so what i'm going to do is run sudo mkdir mnt slash mydisk that's the name of the directory i decided to go along with that's what's in the fstab file now without this directory existing then fstab would not be able to find it and that would be a problem so i'm going to press enter and make sure that that directory exists back in the fstab file again we have the directory right here that needs to exist and we are mounting slash dev slash sdb1 so let's go ahead and have some fun so what i'm going to do right now is mount my flash drive and the way i'm going to do it is type sudo and then mount slash mnt my disk and i'm going to press enter and it worked but how did it work i didn't even tell it what i wanted to mount specifically what i want to do is mount slash dev slash sdb1 but i didn't type that i just typed sudo mount and then the target directory now the reason why that works is because we have identified a match of that directory to slash dev sdb1 in the fstab file and to make the output simple i'm going to grep for my disk and here we have that new line that we added to the fstab file now when i typed sudo mount slash mnt my disk this directory the target directory that i clarified here in the command was found in the fstab file so the system knows that this directory is associated with this particular device right here so that's allowed me to simplify the mount command quite a bit that's pretty cool so now what i'm going to do is unmount that and i'm going to show you how to test the fstab file because we definitely want to make sure that the fstab file has no errors that's really important the command that i'm about to give you will not detect all errors that you might have so don't run the following command and assume that everything is okay if you see no errors it's just one more check you definitely want to make sure you are visually checking the file as well but what i'm going to do is run sudo mount dash a i'm going to mount everything that is in that fstab file that's not already mounted now we can go ahead and ignore this duplication right here this is a desktop linux distribution so i think it's a little bit confused as far as what i'm doing here because it tried to automatically mount it and i manually mounted it as you see here but either way when we ran this command right here we didn't see any errors that's a good sign that doesn't mean we don't have any mistakes at all it just means that mistakes in the fstab file are a lot less likely now when i ran it it noticed that this device right here was not mounted and went ahead and mounted it if this directory didn't exist it wouldn't have been able to do that that's one issue that it might be able to find again just double check it but so far everything looks good but unfortunately we do have a real problem here that we haven't fixed now i know that the font size here is quite large but i want to make sure that you guys are able to see this i talked about the uuids before we see one right here for example but right there i'm actually calling out the system path for that device which is technically valid it did work i was able to mount it so it looks like everything is okay but the problem is sdb1 as you see here that is not static if i restart this computer and that device gets a different device name then this entire line is going to be invalid and you might actually get an emergency console when you try to boot your server if it's trying to boot a device that it's not finding so if this flash drive for example was given the name sdc as the device name so the partition in that case would be sdc1 then it's not going to find sdb1 that's especially true when you are inserting and removing flash drives because those device names can be utilized quite quickly so what do you do instead well that's where the universally unique identifier comes into play which is what uuid stands for so here for the root file system and again we know it's the root file system we have a single forward slash right here it's actually looking for a uuid and i'm not even going to try to say all of this in the video but this identifier this name right here is what it's looking for it's not looking for slash dev sda1 or whatever it might be it's looking for this so if i was to exit out of this file and for simpler output i'm just going to use the df command here which is another way to see what's mounted technically df is disk free it tells you how much space is available on each of your file systems but it also lists all the file systems that are mounted as well and right here we have a single forward slash and this is the device name right here that is trying to mount now it's beyond the scope to explain why this is so different than sdb1 on your end it might not be different there's other ways that you can manage disks i'm not going to get into just yet but if this was to change well that's not good because if it's not able to find it when it goes to boot it then that's a problem but in the fs tab file it's not even mentioning slash dev slash mapper slash data root it's referencing the uuid instead and the reason for that is because the uuid will never change no matter what so let's go ahead and see that so i'm going to run the blk id or block id command i want to know what the uuid is of the storage volumes on my system and i'm using sudo because i want to see everything i don't want anything to be hidden from me i want to see all the information so there's quite a bit of output here but the block id command blk id what that does is it gives you the uuids for all of the storage volumes on your machine so now you can actually see what the uuid is and that tells you what you need to reference if you want to use that instead of the device name and you definitely should use the uuid instead of the device name now if i scroll up a bit here we can see that we have dev mapper data root right here at the top and then it gives us the uuid so what we're going to do right now is compare this to what's in the fstab file so go ahead and memorize every single character okay i was kidding no way don't do not even try to memorize a uuid it's just not even humanly possible well it is humanly possible but we have better things to do anyway it starts with a4 ends with e i think that's good enough and right here we have a uuid that starts with a4 ends in e so it matches and that's how we know what a uuid is for a volume we simply run the sudo blk id command and that gives us that information now in our case we were in the process of setting up this volume right here sdb1 so now we know what the uuid actually is for that particular device so i'm going to copy that now what i'm going to do is run sudo umount i want to unmount that and we can see that it's not mounted so next i'll edit the fstab file i'm going to go all the way down here to the bottom and instead of slash dev slash sdb1 i'm going to remove all of that i'll type uuid equal sign you don't want double quotes or anything crazy like that here just the uuid i'll paste it in and there it is that makes it quite a bit longer but you know what i like this better let's go ahead and save it and exit out i'll run sudo mount dash a again and we can see here at the very bottom slash dev slash sdb1 is mounted to slash mnt mydisk even though i referred to it under its uuid it was still able to mount that and again the reason why we like uuids is because we really don't know if this particular device name is always going to be attached to this particular device it probably won't be so we really don't want to rely on that instead we should actually use the uuid which is what we placed in the fstab file and that's what i recommend you place in your fstab file for your volume again you just run the blk id command preferably with sudo grab the uuid copy it paste it into the fstab file and just make sure that you test it because if you don't test it you're not really sure it's working and then you reboot your server your server will not appreciate having an error in the fstab file which will cause problems up to and including a failure to boot so take your time now before i close out this video there's another option with the fstab file that i want to make sure you guys are aware of so what i'm going to do is on that particular disk one more time and let's open up the fstab file yet again in nano and what we're going to do is go all the way down here and then all the way to the end where it shows defaults as the option i'm going to add a comma and then no auto now earlier when i showed you all of the options that are included in defaults auto was one of them auto is just one of the defaults when you use defaults what auto means is that the file system is going to be automatically mounted no auto is the reverse it means that when the system starts it will not be automatically mounted so then you might be wondering why would i want to add something to the fstab file only to not have it automatically be mounted we'll get to that for now just understand that no auto is the opposite of auto so what i'm going to do is save the file and close out again that particular device is not mounted and it's still not mounted even though i ran sudo mount dash a as you see here which is supposed to mount everything that it finds in the fstab file it didn't actually mount the disc that we added to the fstab file it omitted that and that makes sense again at the end we added no auto so now going back to the question of what's the point of adding no auto to make it not automatically mount yet still included in the fstab file what's the game and the thing is automatically mounting file systems is not the only benefit of the fstab file yes it's a main benefit and probably the number one reason why it even comes up in a google search to begin with but it's not the only use case i can run sudo mount mnt my disk i didn't even have to give it the uuid or the device name and it worked and as you can see it's mounted so already we could see the benefit so even though i chose to add the no auto option here it was still worthwhile to add this particular storage volume to the etsy fstab file because now i'm able to simplify the mount command i don't have to type the device name i only need to refer to the target directory and it'll be mounted so you could add no auto here if for some reason you don't want this particular file system to be automatically mounted perhaps this is a backup drive that you are going to be attaching to your server regularly it's not going to be attached all the time but regularly this will help you ensure that that particular device will always be mounted to the same place when you call the command which makes scripting easier it makes the command line easier it's just a good idea and if nothing else another administrator can look at the fstab file and they would immediately know the typical devices that you mount on a regular basis by simply looking at this one file another thing that we can glean from this is that even though default includes the option auto you've essentially overridden that by including no auto another option inside the defaults is rw so for example rw read and write you can override that as well by changing that to ro for read only and that's not a bad idea the last thing that i'll mention in this video is that i highly recommend that you default everything to read only unless you have a very specific reason to allow right access to that particular volume if you have a company share that has some very important things in there you really shouldn't give everyone and everything read and write access to that only the people and the servers that need to be able to write to it and the value there is that people can't delete everything on a storage volume and i have seen this happen someone coming in and accidentally deleting everything in the volume they can't do that if they don't even have access to write or change the contents of that volume anyway so read only is a better default than what's currently in defaults which is read write again if you have no specific need to make changes to the content then read only is definitely better so there you go now you know how to automatically mount file systems and storage volumes when your linux server starts up that's awesome you also know how to not automatically mount things but still benefit from the convenience of the fstab file in addition this was your introduction to the fstab file so if you weren't already familiar with that file now you are so let me know what your thoughts are i look forward to hearing what you guys have to say let me know in the comments down below and i'll see you again very soon thanks for watching [Music] you
Info
Channel: LearnLinuxTV
Views: 9,836
Rating: 4.9814529 out of 5
Keywords: Linux, Tutorial, Review, Howto, Guide, Distribution, Distro, Learn Linux, operating system, os, open-source, open source, gnu/linux, LearnLinuxTV, LearnLinux.tv, fstab, /etc/fstab, storage, linux storage, mount, automatically mount, boot, linux tutorial, linux tutorial 2021, open source software, how to, volume, uuid, disk, mount disk, mounting, automount, for beginners, linux tutorials, mount command, mount command line, command line, linux command line, server, linux server, ubuntu, debian
Id: A7xH74o6kY0
Channel Id: undefined
Length: 30min 28sec (1828 seconds)
Published: Tue Jul 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.