< Zurück | Inhalt | Weiter >

6.2.1 Links

A link is simply a name attached to a collection of data—that is, to a file. In other words, every file has one set of data and at least one link (a name). But a file may have more than one link. In other words, two different filenames may point to the same data. When you do this, you appear to have two copies of the file, but a change to one also changes the other. Deleting one, however, does not delete the other. It merely deletes the link. The file itself is only deleted when the last link is gone.


6.2.1.1 Hard Links

Links come in two flavors: hard and symbolic. A hard link looks like a file in and of itself. Let’s show you a hard link, before and after.


$ ls -la total 12

drwxrwxr-x 2 mschwarz mschwarz 4096 Jul 8 10:11 .

drwx------ 50 mschwarz mschwarz 4096 Jul 8 10:11 ..

-rw-rw-r-- 1 mschwarz mschwarz 45 Jul 8 10:11 sample

$ cat sample This is a sample

file to demonstrate links.


$


As you can see, we have a directory with a single file in it, sample. Now let’s make a hard link to that file.


$ ln sample example

$ ls -la total 16

drwxrwxr-x 2 mschwarz mschwarz 4096 Jul 8 10:13 .

drwx------ 50 mschwarz mschwarz 4096 Jul 8 10:11 ..

-rw-rw-r-- 2 mschwarz mschwarz 45 Jul 8 10:11 example

-rw-rw-r-- 2 mschwarz mschwarz 45 Jul 8 10:11 sample

$ cat example This is a sample

file to demonstrate links.


$


Notice a few things here. First, other than the size and timestamps being

the same, there is nothing obvious to show that these two files are, in fact, the same file. Note also the number just ahead of the owning user and group names. In the first directory listing, sample had 1 in that position; now both sample and example have 2. This number is the link count. It tells you how many names are linked to the data associated with this name.

We have a couple more things to point out before we move on to soft links, which are going to be more important for our purposes.


$ chgrp wwwdev example

$ ls -la total 16

drwxrwxr-x 2 mschwarz mschwarz 4096 Jul 8 10:13 .

drwx------ 50 mschwarz mschwarz 4096 Jul 8 10:11 ..

-rw-rw-r-- 2 mschwarz wwwdev 45 Jul 8 10:11 example

-rw-rw-r-- 2 mschwarz wwwdev 45 Jul 8 10:11 sample

$ chmod o-r example

$ ls -la total 16

drwxrwxr-x 2 mschwarz mschwarz 4096 Jul 8 10:13 .

drwx------ 50 mschwarz mschwarz 4096 Jul 8 10:11 ..

-rw-rw---- 2 mschwarz wwwdev 45 Jul 8 10:11 example

-rw-rw---- 2 mschwarz wwwdev 45 Jul 8 10:11 sample

$ chgrp mschwarz sample

$ ls -la total 16



drwxrwxr-x

2

mschwarz

mschwarz

4096

Jul

8

10:13

.

drwx------

50

mschwarz

mschwarz

4096

Jul

8

10:11

..

-rw-rw----

2

mschwarz

mschwarz

45

Jul

8

10:11

example

-rw-rw----

2

mschwarz

mschwarz

45

Jul

8

10:11

sample

$










As you can see, a file can have only one set of owners and permissions, no matter how many links are made to it. Changing the owner or permissions of one link changes all hard links at the same time. In other words, the security of a file is like its data: A change to one link is a change to them all.

A link need not be in the same directory as the original name.


$ ln example /tmp/sample

$ ls -la total 16

drwxrwxr-x

2

mschwarz

mschwarz

4096

Jul

8

10:13

.

drwx------

50

mschwarz

mschwarz

4096

Jul

8

10:11

..

-rw-rw----

3

mschwarz

mschwarz

45

Jul

8

10:11

example

-rw-rw----

3

mschwarz

mschwarz

45

Jul

8

10:11

sample

$ ls -la /tmp









total 132









drwxrwxr-x

2

mschwarz

mschwarz

4096

Jul

8

10:23

.

drwx------

50

mschwarz

mschwarz

4096

Jul

8

10:11

..

-rw-rw-r--

1

mschwarz

mschwarz

118081

Jun

3

18:51

jLin.tar.gz

-rw-rw----

3

mschwarz

mschwarz

45

Jul

8

10:11

sample

$










Here we made a third link in a different directory; /tmp/sample is a third name for the same data file. Note that we made it from the example link, not the original filename. In fact, as far as the Linux filesystem is concerned, there is no “original” name. None of these names is more significant than any other. When you remove a filename, the link is destroyed and the file’s link count is decremented. If the link count goes to zero, the file is removed. That’s it. Nothing else.

Hard links have a couple of drawbacks. One of them is a genuine technical limitation and the other is more of a usability problem. The technical limitation is that a hard link cannot be made across mounted filesystems. In the simplest


case (we don’t want to muddy the waters with LVM1 or RAID2 at this point—most Linux distributions do not do LVM or RAID “out-of-the-box”), if you have more than one partition or disk drive, these are “mounted” at dif- ferent points on the directory tree. For example, Mr. Schwarz’s laptop’s mount table looks like this:


$ mount

/dev/hda2 on / type ext3 (rw) none on /proc type proc (rw)

usbdevfs on /proc/bus/usb type usbdevfs (rw)

/dev/hda1 on /boot type ext3 (rw)

none on /dev/pts type devpts (rw,gid=5,mode=620) none on /dev/shm type tmpfs (rw)

$


We have one large partition mounted at /, or root, and a small partition mounted at /boot. In all of our hard link examples so far, we have been making links on the root filesystem. Example 6.1 shows what happens when an attempt is made to hardlink between two different mounted devices.

This is what we mean when we say a link cannot cross filesystems.3

The other problem is more “touchy-feely.” With a hard link, you can see by the link count that other links exist, but you don’t know where they are. Symbolic links get you around both of these issues.


6.2.1.2 Symbolic Links, or Symlinks

In a sense, symbolic links are much simpler than hard links. A symbolic link is a file that contains the name of another file or directory. Because it is marked as a symbolic link, the system will replace it with the contents of the linked file. Example 6.2 will make this more clear.


image

1. Logical Volume Manager. This is a tool that lets you arbitrarily aggregate disk drives and partitions into a “logical volume” that may be mounted and unmounted as a unit. Such tools are commonly used in serious production servers, but are rare on workstations or simple Linux servers.

2. Redundant Array of Inexpensive Disks. Another heavy server feature that allows multiple disk drives to be linked up as if they were a single disk drive and to act as backup to one another silently and transparently.

3. In a similar vein, there are some networked filesystems that do not support hard links at all because the server or host system doesn’t support the concept. Attempts to make links on or to such systems will also fail.


image

Example 6.1 Attempt to hardlink between mounts

$ ln example /boot/sample

ln: creating hard link `/boot/sample' to `example': Invalid cross-device link

$


image


image

Example 6.2 Symlinking /etc/passwd

$ ls -la total 8

drwxrwxr-x

2 mschwarz mschwarz

4096

Jul

8

15:30

.

drwx------

50 mschwarz mschwarz

4096

Jul

8

15:29

..

$ ln -sf /etc/passwd passwd

$ ls -la total 8

drwxrwxr-x 2 mschwarz mschwarz 4096 Jul 8 15:31 .

drwx------ 50 mschwarz mschwarz 4096 Jul 8 15:29 ..

lrwxrwxrwx 1 mschwarz mschwarz 11 Jul 8 15:31 passwd -> /etc/passwd

$ cat passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin

...

etc.


image


What we did here works just like a hard link, but note the attributes on the file: The l indicates a symbolic link, and the permissions are read/write/execute for user, group, and other (or for “world” for short). How- ever, these permissions apply to the link, not to the file. Just as with hard links, there is only one set of permissions on the file, and these are on the file that is pointed to, not on the pointer.

One interesting difference between symlinks and hard links is that sym- links do not increment the link count on a file. If we remove /etc/password (a very bad idea, by the way), the symlink would be unaffected, but an attempt to open or read the symlink would not work, because it points at nothing that exists. This is called a dangling symlink.

Symlinks may refer to symlinks, and thus you need to be cautious to avoid circular symlink chains. All the shells we have used on Linux report circular symlinks as “too many levels of symbolic links,” which sounds like a process exceeding its stack limits but handling it gracefully.