[el7_blog]
/dev/urandom

links, hard and symbolic

Understanding Hard links
Linux stores administrative data about files in inodes. Every file on linux has an inode, and in the inode, important information about the file is stored:

  • The data block where the file contents are stored
  • The creation, access, and modification date
  • Permissions
  • File owners

Just one important piece of information is not stored in the inode: the name. Names are stored in the directory, and each filename knows which inode it has to address to access further file information.

When you create a file, you give it a name. Basically, this name is a hard link. On a linux file system, multiple hard links can be created to a file. This can be useful, because it enables you to access the file from multiple different locations. Some restrictions apply to hard links:

  • Hard links must exist all on the save device
  • You can't create hard links to directories
  • The number of aliases the original file has. When the last name is removed, the contents are also removed.

Understanding Symbolic Links
A symbolic link does not link directly to the inode but to the name of the file. This makes symbolic links much more flexible, but it also has some disadvantages. The advantages of symbolic links is that they can link to files on other devices, as well as on directories. The major disadvantage is that when the original file is removed, the symbolic link becomes invalid and does not work any longer.

Creating Links
Use the ln command to create links. It uses the same order as cp and mv; first you mention the source name, followed by the destination name. If you want to create a symbolic link, you use the option -s, and then you specify the source and target file or directory. One important restriction applies, however; to be able to create hard links, you must be the owner of the item that you want to link to. This is a new security restriction that has been introduced in EL7.

Command Explanation
ln /etc/hosts . Creates a link to the file /etc/hosts in the current directory
ln -s /etc/hosts . Creates a symbolic link to the file /etc/hosts in the current directory
ln -s /home /tmp Creates a symbolic link to the directory /home in the directory /tmp


The ls command will reveal whether a file is a link:

  • In the output of the ls -l command, the first character is an l if the file is a symbolic link.
  • If a file is a symbolic link, the output of ls -l shows the name of the item it links to after the filename
  • If a file is a hard link, ls -l shows the hard link counter

[root@el7_blog.local]# pwd
/tmp
[root@el7_blog.local]# ls -l
-rw-r--r--.  2 root root     0 Apr 18  2016 HTTPD_ERRORS
lrwxrwxrwx.  1 jobs users    5 Jan 25 16:03 home -> /home
drwxrwxrwt.  7 root root  4.0K Jan 25 16:05 .

Removing Links
Removing links can be dangerous. DO NOT USE -r or -f to remove links, even if they are subdirectories.

Change Ownership of Links
chown -h symbolic-link use the -h, -or- –no-dereference option, affect each symbolic link instead of any referenced file (useful only on systems that can change the ownership of a symlink)