Permissions of Files

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we'll dive into the world of file permissions in Linux. We'll explore three essential commands: chown, touch, and chmod. These tools are crucial for managing access to files and directories on a Linux system. By the end of this lab, you'll have a solid grasp on creating files, changing file ownership, and modifying file permissions.

Achievements

After completing this lab, you'll be able to:

  • Use chmod to modify file permissions
  • Use chown to change file ownership
  • Use touch to create new files and update timestamps of existing files

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills linux/mkdir -.-> lab-270252{{"`Permissions of Files`"}} linux/sudo -.-> lab-270252{{"`Permissions of Files`"}} linux/touch -.-> lab-270252{{"`Permissions of Files`"}} linux/chown -.-> lab-270252{{"`Permissions of Files`"}} linux/chmod -.-> lab-270252{{"`Permissions of Files`"}} shell/comments -.-> lab-270252{{"`Permissions of Files`"}} shell/cond_expr -.-> lab-270252{{"`Permissions of Files`"}} shell/globbing_expansion -.-> lab-270252{{"`Permissions of Files`"}} end

Creating a New File

Let's start by creating a new file using the touch command. This versatile command can create new, empty files and update the timestamps of existing ones.

First, make sure you're in the right directory:

cd ~/project

Now, let's create a new file named example.txt:

touch example.txt

This command creates an empty file called example.txt in your current directory. To confirm that the file was created, use the ls command:

ls

You should see example.txt listed in the output.

Changing the Ownership of a File

Now that we've created a file, let's learn how to change its ownership. The chown command allows us to modify both the user and group ownership of a file.

First, let's check the current ownership of our example.txt file:

ls -l example.txt

You should see output similar to this:

-rw-rw-r-- 1 labex labex 0 Jul 29 15:11 example.txt

This output shows that:

  1. -rw-rw-r-- represents the file permissions (we'll explore this more in Step 4).
  2. The first labex is the current owner of the file.
  3. The second labex is the current group of the file.
  4. 0 is the file size in bytes.
  5. Jul 29 15:11 is the last modified date and time.
  6. example.txt is the file name.

Now, let's change the ownership of the file to the root user:

sudo chown root:root example.txt

Here's what this command does:

  • sudo runs the command with root privileges.
  • chown is the command to change ownership.
  • root:root specifies the new owner and group (both set to root).
  • example.txt is the target file.

Let's verify the change:

ls -l example.txt

You should now see that both the owner and group have changed to root:

-rw-rw-r-- 1 root root 0 Jul 29 15:11 example.txt

Changing the Ownership of a Directory

The chown command can also change the ownership of entire directories and their contents. Let's see this in action.

First, let's create a new directory with some files:

mkdir -p new-dir/subdir
echo "Hello, world" > new-dir/file1.txt
echo "Another file" > new-dir/subdir/file2.txt

mkdir -p creates the new-dir directory and its subdir subdirectory. The echo commands create two files, file1.txt and file2.txt, respectively.

Now, let's check the current ownership:

ls -lR new-dir

ls -lR lists the contents of new-dir recursively.

You should see something like this:

new-dir:
total 4
-rw-rw-r-- 1 labex labex 13 Jul 29 09:15 file1.txt
drwxrwxr-x 2 labex labex 23 Jul 29 09:15 subdir

new-dir/subdir:
total 4
-rw-rw-r-- 1 labex labex 13 Jul 29 09:15 file2.txt

Now, let's change the ownership of new-dir and all its contents to the root user:

sudo chown -R root:root new-dir

In this command:

  • The -R option tells chown to operate recursively, changing the ownership of all files and subdirectories within new-dir.

Let's verify the change:

ls -lR new-dir

You should now see:

new-dir:
total 4
-rw-rw-r-- 1 root root 13 Jul 29 09:15 file1.txt
drwxrwxr-x 2 root root 23 Jul 29 09:15 subdir

new-dir/subdir:
total 4
-rw-rw-r-- 1 root root 13 Jul 29 09:15 file2.txt

As you can see, the ownership of the directory and all its contents has changed to root.

Changing the Permissions of a File

In Linux, file permissions are represented by a series of letters or numbers. Let's explore how to read and change these permissions.

First, let's look at the current permissions of our example.txt file:

ls -l example.txt

You might see something like this:

-rw-rw-r-- 1 root root 0 Jul 29 15:11 example.txt

The -rw-rw-r-- part represents the file permissions:

  • The first character (-) indicates this is a regular file.
  • The next three characters (rw-) represent the owner's permissions (read and write, but not execute).
  • The next three (rw-) are for the group.
  • The last three (r--) are for others (everyone else).

Now, let's change these permissions using the chmod command:

sudo chmod 700 example.txt

In this command:

  • 700 is a numeric representation of permissions:
    • 7 (first digit) gives the owner read (4), write (2), and execute (1) permissions: 4+2+1=7
    • 0 (second digit) gives the group no permissions
    • 0 (third digit) gives others no permissions

Let's verify the change:

ls -l example.txt

You should now see:

-rwx------ 1 root root 0 Jul 29 15:11 example.txt

Changing the Permissions of a Directory

Changing permissions for directories works similarly to changing permissions for files. Let's practice by creating a new directory and modifying its permissions.

First, let's create a new directory and set some non-standard permissions:

mkdir ~/test-dir
chmod 700 ~/test-dir

Now, let's check the current permissions:

ls -ld ~/test-dir

You should see:

drwx------ 2 labex labex 4096 Jul 29 15:45 /home/labex/test-dir

Now, let's change the permissions:

chmod -R 755 ~/test-dir

In this command:

  • -R applies the change recursively to all files and subdirectories (though our directory is empty in this case).
  • 755 gives read, write, and execute permissions to the owner, and read and execute permissions to group and others.

Let's verify the change:

ls -ld ~/test-dir

You should now see:

drwxr-xr-x 2 labex labex 4096 Jul 29 15:45 /home/labex/test-dir

This clearly shows the change in permissions, from only the owner having access (700) to the owner having full access while others can read and execute (755).

Using Symbolic Notation for Permissions

While numeric notation is concise, symbolic notation can be more intuitive. Let's practice using symbolic notation to change file permissions.

First, let's create a new script file with some content:

cd ~/project
echo '#!/bin/bash\necho "Hello, World"' > script.sh

This command does two things:

  1. It creates a new file named script.sh. The .sh extension is commonly used for shell scripts. shell scripts are executable files that contain a series of commands that are executed in sequence.
  2. It writes two lines into this file:
    • #!/bin/bash (called a shebang) tells the system this is a bash script.
    • echo "Hello, World" is a command that will print "Hello, World" when the script runs.

Now, let's check its initial permissions:

ls -l script.sh

You should see something like:

-rw-rw-r-- 1 labex labex 32 Jul 29 16:30 script.sh

Let's try to run the script:

./script.sh

You should see a "Permission denied" error because the script doesn't have execute permissions yet.

Now, let's add execute permission for the owner:

chmod u+x script.sh

In this command:

  • u refers to the user (owner)
  • +x adds execute permission

Let's verify the change:

ls -l script.sh

You should now see:

-rwxrw-r-- 1 labex labex 32 Jul 29 16:30 script.sh

Now, let's try running the script again:

./script.sh

This time, you should see the output: "Hello, World"

This example clearly demonstrates why we need to add execute permissions to scripts, and the difference before and after adding these permissions.

Summary

In this lab, we've explored essential Linux commands for managing file permissions:

  1. We used touch to create new files.
  2. We learned how to use chown to change file and directory ownership, including recursive changes.
  3. We practiced using chmod with both numeric and symbolic notation to modify file and directory permissions.
  4. We saw practical examples of why permissions matter, such as needing execute permissions to run scripts.

These commands are crucial for maintaining security and controlling access in Linux systems. Remember to always be cautious when changing permissions, especially when using sudo, as incorrect changes can have significant consequences for system security and functionality.

Other Linux Tutorials you may like