00:00:00 - Welcome to nugget 104.6. What we're going to learn
00:00:03 - about - links. We're going to create and change hard links and
00:00:07 - soft links, or symbolic links. It's a pretty simple nugget,
00:00:12 - but it's one of those really cool things in Linux that makes
00:00:15 - your job as an administrator much easier. So like I said, we're going to talk about
00:00:18 - hard links, soft, or symbolic links and we're going to use the
00:00:21 - command Ln. Alright, so let's get to the command line and take care
00:00:25 - of this nugget pretty quick.
00:00:28 - Okay, in my folder here, I've created three different files. Now,
00:00:34 - this is the original file I made, it's a text file. Let's look
00:00:37 - in it, it just has stuff in it here.
00:00:40 - Just a bunch of text, okay. So it's just a text file.
00:00:45 - And I've created a hard link to that file and a soft link, or
00:00:49 - a symbolic link. Again, soft link, or symbolic link, pretty much mean
00:00:53 - the same thing. I'm going to explain what the difference is, what they mean,
00:00:56 - how you can identify them. And then we'll talk about how we
00:00:59 - create these and maybe why we create these. Alright? So again, file.txt
00:01:04 - is the first file that I made. It's the only actual
00:01:07 - file that I made in this directory. Let's do an ls-l and see what it
00:01:11 - looks like.
00:01:13 - Okay, this is a hardlink.txt.
00:01:17 - Now, if you look, they're same size,
00:01:20 - exact same size, same permission, same owner, two different file
00:01:25 - names. And if you remember from our section on hard drives,
00:01:28 - every file has an inode, right? Every file has an inode. Well
00:01:33 - both of these file names point to the same inode, right? What
00:01:40 - I'm going to do, I'm going to do ls-li to show inodes.
00:01:45 - And you'll see this file.txt, is pointing to this inode.
00:01:50 - 600432.
00:01:54 - This is pointing to the exact same inode. For all intents
00:01:58 - and purposes, it doesn't matter which file I created first, they
00:02:02 - are both linked to this inode, or this file. If I edit this
00:02:07 - file, here, let's do that.
00:02:11 - Let's edit this file and we'll get rid of all this stuff down here.
00:02:18 - We'll save it, ls-l, you'll see I changed both files.
00:02:24 - See, it was 147 bytes, now it's 33 bytes.
00:02:27 - I changed both files by just changing one, because
00:02:31 - these names are both linked to the same thing. Now in contrast,
00:02:36 - this is a soft link. And what this does, this just points, instead
00:02:41 - of pointing to the inode,
00:02:43 - what this soft link does, it points to this file name, okay.
00:02:47 - So if we rename this file, like here, let me show you. If I
00:02:51 - cat softlink.txt, see it's pointing to file.txt.
00:02:57 - So it just printed that right out, alright? But what if we
00:03:04 - change the name of file.txt to newfile.txt.
00:03:10 - Let's look. Now it's changed, because now this is a broken link, okay.
00:03:15 - Now see, as far as hardlink and newfile, they don't care,
00:03:19 - right. Because they're pointing to the same inode.
00:03:22 - ls-li. See, they're still pointing to the same inode. It
00:03:25 - doesn't matter what we name them when it comes to hard linking.
00:03:28 - But for a soft link, this is now broken. If we try to cat this,
00:03:33 - it's going to say there's no such file or directory, because
00:03:35 - it's basically just pointing to the file name, file.txt, and
00:03:39 - that no longer exists. So that's the difference between a
00:03:42 - soft link and hard link. A hard link is linking to a specific
00:03:46 - inode, or the same file. These are both pointing to the same
00:03:49 - file. If we delete one, the file is still there, because the other
00:03:53 - name is pointing to it. But with a soft link, all it's doing
00:03:56 - is pointing to the file name. So this is now broken, okay. And
00:04:02 - that's really the only difference between hard links and soft links. Next, I'll
00:04:05 - show you how to make them. So let's
00:04:10 - get rid of everything, so there's nothing up my sleeve here.
00:04:14 - We're going to --
00:04:22 - this is my file, okay. Just use vi, write quit. So now, we
00:04:27 - have a file, okay. Now, if we're going to hardlink, we would
00:04:31 - ls-li. See, they're still pointing to the same inode. It
00:04:33 - the source, file.txt, and the destination. What we
00:04:38 - want to link it to.
00:04:42 - hard.txt.
00:04:45 - We do this, see now we have two files that are exactly the same and you
00:04:49 - can see that they're literally exactly the same, they're both
00:04:51 - pointing to the same inode.
00:04:53 - Now there's a catch here. First, I'll show you how to do the symbolic
00:04:57 - link. Ln-s, source file, let's say file.txt
00:05:03 - to
00:05:06 - softy.txt
00:05:09 - and see, now what's happened, is softy is linked to file.txt.
00:05:12 - So you're probably thinking, okay, why would you ever
00:05:16 - want to do a soft link then? Because if we change one of the names
00:05:20 - of those files a soft link is broken, it's done. Well, if it's pointing
00:05:25 - to the same inode, you can't have a hard link that links
00:05:30 - across file systems. For example, do you remember our other mounted
00:05:35 - partition, that we did in an earlier nugget?
00:05:41 - We did this when we were talking about quotas and groups last
00:05:43 - time. But there's a file in here called bigfile.txt.
00:05:46 - Now this is on another partition completely, okay. So,
00:05:51 - the things that hard links are limited with, is you can't link
00:05:54 - across different devices. So, we try to do this,
00:06:04 - to home/spowers/hardlinked/bigfile.txt
00:06:18 - It's going to say invalid cross device link, see, because it
00:06:22 - can't point to an inode on a different drive. I mean,
00:06:25 - there can't be, you know, a pointer to another drive located on this drive.
00:06:30 - It just doesn't work, it has to be on the same device. So that
00:06:32 - the inodes are all the same. That's where soft links, or symbolic
00:06:37 - links, come in handy.
00:06:42 - ln-s/mnt/hard_drive/bigfile.txt to /home/spowers/softlinkedbigfile.txt.
00:06:48 - This will work just fine, see?
00:06:55 - So softlinkedbigfile points to that file and now we can look at it
00:07:00 - if we want. Softlinkedbigfile. This is HUGE!!!!! Okay, so that's the content of
00:07:07 - this file, and it's soft linked over.
00:07:10 - Now, you can see where hard links can be very useful. So you can have
00:07:15 - a file located on one inode, you know, so if you change
00:07:18 - it, regardless of which file you change, it's modified.
00:07:22 - But soft links are much more versatile in how they work across
00:07:25 - different directories, or not different directories,
00:07:28 - but different devices, okay. So that's a big key, and now where
00:07:32 - would you use this in every day system management? A great
00:07:37 - example is found in Red Hat. Like in Red Hat, or Centos Red Hat
00:07:42 - Enterprise Linux, or in Centos or any of those Red Hat distributions.
00:07:47 - What they do is they will link -- see if you want to edit the
00:07:50 - grub configuration file, we learned that it's in
00:07:55 - /boot/grub/ directory, right? It's in here, it's the menu.lst.
00:07:59 - Well, that isn't really standard for where most configuration
00:08:03 - files are kept. Normally they're kept in the etc directory, or in the et cetera
00:08:07 - directory. So, what happens on Red Hat, but
00:08:11 - we can do that if we want, go to the etc directory.
00:08:15 - We'll type sudo, because we don't have write access as the user spowers
00:08:18 - in the etc directory. And we'll -- ln-s.
00:08:24 - Now, why would I say minus s? Why wouldn't I -- well let's
00:08:26 - do it, then we'll explain why I wouldn't do it with a hard link. We're
00:08:30 - going to do boot/grub/menu.lst
00:08:33 - and we're going to call it
00:08:37 - grub.conf, okay. So we do that,
00:08:46 - and you'll see now, in the etc directory is a file called
00:08:49 - gub.conf, that points to boot/grub/menu.lst. Now, this is a
00:08:54 - standard you'll find, like I said, in Red Hat based distributions,
00:08:58 - but it's really convenient for me. That way, if I want to edit the
00:09:01 - grub configuration, it's easy to remember. It's in the etc
00:09:05 - directory, grub.conf. That's easier to remember than boot/grub/menu.lst.
00:09:09 - Alright, so that's one of the things that
00:09:12 - symbolic linking is used for, and why is it a symbolic link
00:09:15 - and not a hard link? Well, in my particular case a hard link
00:09:19 - would have worked, because my boot folder is just a folder inside
00:09:23 - the root directory. But it's very common for the boot directory
00:09:29 - to be a separate partition, a very small partition, a boot partition.
00:09:32 - In which case, we wouldn't be able to hard link to the etc
00:09:36 - directory, or the etc directory, because it would be a different
00:09:40 - device, alright? So that's where a symbolic link comes into play.
00:09:43 - And that's just one example of ways that you can use symbolic
00:09:47 - linking to make it a lot easier to edit files. A lot of times,
00:09:51 - if there's a file I edit very often, like in my Linux terminal
00:09:54 - server, what I do is, I will link the configuration
00:09:58 - file, which is nested very deep into var/lib/tft/b/boot,
00:10:04 - all kinds of stuff, I'll just make a symbolic link of that
00:10:07 - file in my home directory, and then I can edit it from there, and
00:10:10 - that's a lot easier then typing that long path, okay. So that's
00:10:14 - all there is to symbolic linking. I guess if there's one more
00:10:17 - thing, the only other thing I can think of, is when you give
00:10:21 - in an argument, like let's say we're going to ln-s file.txt
00:10:25 - and we're -- you know what, let me a folder, so I can make my point.
00:10:32 - Folder. And let's say I'm going to do ln-s file.txt, and the
00:10:39 - destination is going to be a folder name, instead of an actual
00:10:43 - name of a file, so I'm -- basically, I'm saying, make a symbolic link
00:10:48 - of file.txt, in this folder. Press enter, and then if
00:10:52 - you look
00:10:54 - inside the folder, called folder, what it's done,
00:11:00 - it's created the link and called it file.txt,
00:11:04 - but, it's pointed it to something that doesn't exist, right? All we
00:11:08 - told it to do, is link to file.txt. We didn't give
00:11:12 - it a path. So you have to be extremely careful. The file.txt
00:11:15 - by default, if you don't put the name of a file
00:11:19 - at the end, it will just name it the same file in that folder. But,
00:11:22 - if you're going to do something in a folder, or basically, in
00:11:26 - general, it's good to put the full path. Why isn't this working?
00:11:29 - Because it's looking for a file named file.txt in the
00:11:33 - same location. So let's get rid of that.
00:11:39 - And now, how would we properly do this? Well, we would do ln-s
00:11:43 - home/spowers/file.txt folder
00:11:49 - and then, ln-l folder,
00:11:55 - And we'll see, instead of just pointing to something called
00:11:58 - file.txt, it's pointing to the full path, home/spowers/file.txt
00:12:03 - and now it's a working symbolic link.
00:12:06 - So you have to be careful that when you do this, that you're actually
00:12:10 - pointing to something that exists. Because it will create a
00:12:13 - file for you, or a symbolic link for you, even if the file isn't
00:12:17 - there. It just creates that broken link, alright? So you have to be careful
00:12:20 - and then you have to make sure that if your destination's a folder,
00:12:23 - it's just going to make a link the same name. If we want it to
00:12:26 - name it something different, we would type
00:12:30 - ln-s, we would type the full path,
00:12:32 - folder/newlink.txt
00:12:38 - and now it creates it with that new name.
00:12:41 - Alright, I think you learned everything there is to know about symbolic
00:12:44 - linking, but let's go back through it, and make sure we covered everything
00:12:47 - in the L-pick objectives.
00:12:49 - You know what, I did forget one thing. So let's go over
00:12:52 - it and then we'll check the objectives. Alright. In this folder I
00:12:56 - just have one file called file.txt Now, I'm going to
00:12:59 - make a link to of it,
00:13:03 - ln file.txt hardlink.txt
00:13:05 - ln file.txt hardlink.txt Alright?
00:13:07 - So we see that. Now what if I did copy file.txt
00:13:13 - to
00:13:15 - copyoflink.txt or
00:13:19 - copyoffile.txt.
00:13:22 - What you're going to see, it looks exactly the same. And this is where
00:13:26 - you can be confused with copying versus linking. So if you
00:13:30 - have a folder full of these three files, you would really have
00:13:34 - no way to know what's a hard link and what's a file. Would you agree?
00:13:37 - They all look exactly the same. What you would have to do then, is
00:13:41 - ls-li
00:13:44 - and then it will show you the inode. So you know
00:13:48 - these two are hard linked to the same inode. And this
00:13:54 - is a copy. So if we edit this one,
00:14:08 - now this one has grown, these two haven't. But if we were to
00:14:12 - edit one of these --
00:14:25 - now you see that both of these have changed. Because these are really the
00:14:29 - same file, just linked to two different file names, okay. So you can get really
00:14:33 - caught up with, or you can really get confused with hard links versus
00:14:36 - copying a file. So it's always good to remember ls-li
00:14:40 - and that'll show you what inode they're tied to, alright.
00:14:44 - So that really is all of it, but still, let's go back over the objectives
00:14:47 - to make sure we have it all covered, and I didn't miss anything else.
00:14:51 - Okay, we talked about hard links. We talked about what that means,
00:14:54 - what it looks like, and how you can't go across devices with hard
00:14:57 - links. We learned about soft or symbolic links, how you create
00:15:01 - them and some advantages. Like you can symbolically link between
00:15:05 - two different devices, which is really nice for things like
00:15:07 - configuration files. And then we learned about the tool to
00:15:10 - do it all is ln. And then as a last little add on, I talked about
00:15:15 - copy versus linking. Because they're very very different and
00:15:20 - sometimes they're hard to tell the difference, but just remember
00:15:23 - that ls-li shows the inode, that a specific file
00:15:27 - is attached to, okay. I hope that this has been informative for you,
00:15:31 - and I'd like to thank you for viewing.