Linux Tips Ep 3 : Creating Links and File Permissions

In today’s episode of Linux Commands Explained, we’ll look at how to make soft and hard links, as well as how to list, set, and change file permissions in Kali Linux.

To get the previous episodes of Linux Commands Explained click the link below:

Creating Soft and Hard links in Kali Linux.

To begin with, let’s define what a “soft link” and a “hard link” are. A soft link, also known as a symbolic link, serves as an actual link to a file, whereas a hard link serves as a mirror copy. When we remove the original file, the soft link is rendered worthless since it will be connecting to a non-existent file, however with a hard link, the file will still exist because it is a mirror copy and will not be affected if the original file is deleted.

A soft link has the following characteristics:

  • It allows the linking of files and folders.
  • The path to the original file is included, but not the contents.
  • The inode number and file permissions are different from the original.

A hard link has the following characteristics:

  • It confines one to only linking files.
  • The inode number and file permissions are the same as the original file.
  • If the file permissions on the original file change, the linked file will be updated as well.
  • It has the same contents as the original file, and the contents are preserved even if the original file is moved or deleted.

Here’s an example of how to make a soft link:

To create a soft link, we use the ln -s command to create a soft link. It needs two arguments : ln -s <path to source of the file> <path to where we want to create the link> .
To display the inode number, run the command ls -i. The original file and the linked file have distinct inode numbers, as shown in the image above. We may also see the path of the original file when we list the contents of the folder containing the linked file.
The soft link is rendered worthless when we shred (destroy) the original file because it links to a non-existent file.

Here’s an example of how to make a hard link:

To create a hard link, we use the ln command. It needs two arguments : ln <path to source of the file> <path to where we want to create the link> . The inode number for the original file and the hard linked file are the same, as illustrated in the image.

When we make modifications to the contents of the hard linked file, the changes are reflected in the original file as well. The image below is a good example of this.

You’re probably wondering what the point of a hard link since it functions similarly to the copy command. The difference between a hard link and a copy command is whenever we copy a file, we duplicate it, and if we edit the duplicated file, the change only affects that one file, however when we alter a hard link, the change affects all the files that share that hard link.

The original and hard linked files have the same permissions, as seen by the red selection. The green highlight grants read and write permission to the original file’s group users. We can see from the cyan selection that the permission change affects the hard linked file as well.

File Permissions in Kali Linux.

When it comes to permissions, Kali Linux files and folders have three sections:

  • User –> u
  • Group –> g
  • Others –> o

This is shown clearly in the picture below:

The cyan selections represent user permissions, the red selections represent group permissions, and the pink selections represent others permissions.

Types of permissions abbreviations and their meaning;

  • r -> read
  • w -> write
  • x -> execute
  • l -> soft link
  • d -> directory
  • S or s -> SUID or SGID permission
  • T or t -> sticky bit permission

We use the ls -l command to show the file permissions and also show the user and group owners of a particular file and directory.

The cyan selection indicates the file’s permissions, the green selection indicates how many links are associated to that particular file, the pink selection indicates the file’s owner, and the red selection indicates the file’s access group.

To view the available users we use the users command whereas to see the available groups we use the groups command.

Use users to display users in a system and groups to displays the available groups.

To change the ownership of a file or directory we use the chown command. It takes two arguments that is., sudo chown [user]:[group] <file or directory>.

Let’s restore the file ownership.

To add or remove permission to a file or q we user the chmod command which mean change mode. It takes two arguments that is., sudo chmod [option] <file or directory>.

The u+x in the command above shows that we are specifically adding execution permission to the user represented by u.

Below are examples of chmod usage:

  • sudo chmod o+r <file> –> gives write permission to others.
  • sudo chmod g+rw <file> –> gives read and write permission to the group.
  • sudo chmod o-rwx <file> –> removes read, write and execute permissions from the others.
  • sudo chmod u-w <file> –> removes write permissions to the user.
  • sudo chmod g+rx,u+rwx,o+r <file> –> this command gives read and execute permissions to the group, gives read, write and execute permissions to the user and gives read permission to the others.
  • chmod u+w or u+rw or u+rwx <file> –> adding permissions to user.
  • chmod g+w or g+rw or g+rwx <file> –> adding permissions to groups.
  • chmod o+w or o+rw or o+rwx <file> –> adding permissions to others.
  • chmod u-w or u-rw or u-rwx <file> –> removes permissions to user.
  • chmod g-w or g-rw or g-rwx <file> –> removes permissions to groups.
  • chmod o-w or o-rw or o-rwx <file> –> removes permissions to others.

Numbers can also be used to change the permissions of files and directories. Here are some numbers and what they represent when they’re used with chmod:

  • 0 : no permission.
  • 1 : execute (x)
  • 2 : write (w)
  • 3 : execute + write (wx)
  • 4 : read (r)
  • 5 : read +execute (rx)
  • 6 : read + write (rw)
  • 7 : read + write + execute (rwx)

Below is an example of the above:

This removes the user’s execute permission as well as the group’s and others’ permissions.

Below are some more examples of chmod usage:

  • sudo chmod 777 <file>
  • sudo chmod 666 <file>
  • sudo chmod 222 <file>
  • sudo chmod 544 <file>

Hope this articles was helpful.

Happy hacking !!

Share this article
Charles Koome
Charles Koome

I am a cyber security enthusiast and love all kinds of tech stuff. I love playing CTFs and doing penetration testing(websites and mobile applications).

Articles: 9

Leave a Reply

Your email address will not be published. Required fields are marked *