Linux access Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to manage file permissions and ownership on a Linux system. The lab covers three key topics: understanding file permissions, modifying file permissions with the chmod command, and changing file ownership with the chown command. These skills are essential for managing access to files and directories in a Linux environment. The lab provides practical examples and step-by-step instructions to help you become proficient in these fundamental Linux utilities.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/UserandGroupManagementGroup -.-> linux/sudo("`Privilege Granting`") linux/BasicFileOperationsGroup -.-> linux/chown("`Ownership Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") subgraph Lab Skills linux/cat -.-> lab-422534{{"`Linux access Command with Practical Examples`"}} linux/ls -.-> lab-422534{{"`Linux access Command with Practical Examples`"}} linux/sudo -.-> lab-422534{{"`Linux access Command with Practical Examples`"}} linux/chown -.-> lab-422534{{"`Linux access Command with Practical Examples`"}} linux/chmod -.-> lab-422534{{"`Linux access Command with Practical Examples`"}} end

Understanding File Permissions in Linux

In this step, you will learn about the fundamental file permissions in Linux and how to view and understand them.

In Linux, every file and directory has a set of permissions that determine who can read, write, and execute the file or directory. These permissions are divided into three categories: owner, group, and others.

To view the permissions of a file or directory, you can use the ls -l command. This will display the file permissions in the following format:

-rw-r--r-- 1 labex labex 0 Apr 12 12:34 example.txt

The first character indicates the file type (- for regular file, d for directory, l for symbolic link, etc.). The next nine characters represent the permissions, with the first three being the owner's permissions, the next three being the group's permissions, and the last three being the permissions for others.

The permissions are represented by the letters r (read), w (write), and x (execute). If a permission is not granted, a dash (-) is displayed instead.

For example, in the output above:

  • The owner (labex) has read and write permissions (rw-).
  • The group (labex) has read permissions (r--).
  • Others have read permissions (r--).

You can also use the stat command to view more detailed information about a file, including its permissions:

$ stat example.txt
  File: example.txt
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d	Inode: 131074      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1000/labex)   Gid: (1000/labex)
Access: 2023-04-12 12:34:56.123456789 +0000
Modify: 2023-04-12 12:34:56.123456789 +0000
Change: 2023-04-12 12:34:56.123456789 +0000
 Birth: -

This output provides more details about the file, including the access, modification, and change times, as well as the user and group ownership.

Understanding file permissions is essential for managing access to files and directories in a Linux system.

Modifying File Permissions with the chmod Command

In this step, you will learn how to use the chmod command to modify the permissions of files and directories in Linux.

The chmod command allows you to change the read, write, and execute permissions for the owner, group, and others. The basic syntax for the chmod command is:

chmod [options] mode file

Where mode is the new permission setting.

You can use symbolic mode or numeric mode to set the permissions. Symbolic mode uses letters to represent the permissions:

  • u for the owner
  • g for the group
  • o for others
  • a for all (owner, group, and others)
  • r for read
  • w for write
  • x for execute

For example, to give the owner read and write permissions, the group read permissions, and others no permissions, you would use:

chmod u=rw,g=r,o= example.txt

Numeric mode uses a three-digit number to represent the permissions:

  • The first digit represents the owner's permissions
  • The second digit represents the group's permissions
  • The third digit represents the permissions for others

Each digit is the sum of the following values:

  • 4 for read
  • 2 for write
  • 1 for execute

For example, to set the permissions to rw-r--r--, you would use:

chmod 644 example.txt

Try modifying the permissions of the example.txt file using both symbolic and numeric modes:

## Symbolic mode
chmod u=rw,g=r,o=r example.txt
## Numeric mode
chmod 644 example.txt

Example output:

-rw-r--r-- 1 labex labex 0 Apr 12 12:34 example.txt

Now the file example.txt has the following permissions:

  • The owner (labex) has read and write permissions (rw-)
  • The group (labex) has read permissions (r--)
  • Others have read permissions (r--)

Understanding how to use the chmod command is essential for managing file and directory permissions in a Linux system.

Changing File Ownership with the chown Command

In this step, you will learn how to use the chown command to change the ownership of files and directories in Linux.

By default, when a file or directory is created, it is owned by the user who created it. The chown command allows you to change the owner and/or group of a file or directory.

The basic syntax for the chown command is:

chown [options] owner[:group] file

Where owner is the new owner username, and group is the new group name.

For example, to change the owner of example.txt to the labex user, you can use:

sudo chown labex example.txt

To change both the owner and group, you can use:

sudo chown labex:labex example.txt

You can also use the recursive option -R to change the ownership of a directory and all its contents:

sudo chown -R labex:labex ~/project

This will change the ownership of the ~/project directory and all files and subdirectories within it to the labex user and group.

Let's try changing the ownership of the example.txt file:

## Change the owner to labex
sudo chown labex example.txt

## Change the owner and group to labex
sudo chown labex:labex example.txt

Example output:

-rw-r--r-- 1 labex labex 0 Apr 12 12:34 example.txt

Now the example.txt file is owned by the labex user and group.

Understanding how to use the chown command is essential for managing file and directory ownership in a Linux system.

Summary

In this lab, you learned about the fundamental file permissions in Linux and how to view and understand them. You discovered that every file and directory has a set of permissions that determine who can read, write, and execute the file or directory, which are divided into three categories: owner, group, and others. You also learned how to use the ls -l and stat commands to view the permissions of files and directories, and how understanding file permissions is essential for managing access to files and directories in a Linux system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like