0% found this document useful (0 votes)
70 views7 pages

File Ownership & Permission in Linux

Uploaded by

wikaka6903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views7 pages

File Ownership & Permission in Linux

Uploaded by

wikaka6903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

File Ownership & Permission in Linux.

md 2024-01-16

Paths, Links, File Ownership & Permission in Linux


In Linux, understanding links, paths, file ownership, and permissions is crucial for effective file and system
management.

1. Links:

Hard Links:

A hard link is an additional reference to an existing inode (data structure on a filesystem that
stores information about a file or directory).
Changes to the data in one hard link are reflected in all other hard links.
Removing any hard link does not affect the actual data until the last reference is removed.

Symbolic (Soft) Links:

A symbolic link is a separate file that contains a path pointing to the target file or directory.
If the original file is deleted, the symbolic link becomes dangling.
Symbolic links can span filesystems.

Examples:

# Create a hard link


ln original_file hard_link

# Create a symbolic link


ln -s target_file symbolic_link

Certainly! Let's step into the practical realm of file linking with a real-world scenario involving a text
document.

A. Hard Links - The Document Duplication Trick

Imagine you have a document called report.txt that contains important information. Now, you want to
create a backup copy without using extra disk space.

Creating a Hard Link:

ln report.txt backup-report.txt

You've just created a hard link named backup-report.txt. The beauty here is that both report.txt and
backup-report.txt share the same data on the disk. If you edit either file, the changes will be reflected in
both.

Removing One Link:

1/7
File Ownership & Permission in Linux.md 2024-01-16

rm backup-report.txt

Even if you remove the backup-report.txt link, the original report.txt still holds all the information. Hard
links provide a space-efficient way to have multiple references to the same data.

B. Soft Links - The Shortcut to Wisdom

Now, imagine you have a frequently updated document called latest-report.txt, and you want to create a
shortcut for quick access.

Creating a Soft Link:

ln -s latest-report.txt shortcut-report.txt

The shortcut-report.txt is a symbolic link, acting like a shortcut. If you update latest-report.txt, the
changes are instantly visible when accessing shortcut-report.txt.

Removing the Original Shortcut Target:

rm latest-report.txt

Uh-oh! The shortcut-report.txt still exists, but it's pointing to nothing. Soft links are excellent for creating
aliases or shortcuts, but they depend on the original target's existence.

In practical terms, hard links are like creating efficient document backups, while soft links act as handy
shortcuts to essential files. Use them wisely to streamline your file management tasks!

2. Paths:

Absolute Path:

Specifies the full path from the root directory.


Starts with a /.
Example of an absolute path:

Relative Path:

Specifies the path relative to the current directory.


Does not start with a /.

Examples:

# Absolute path
/home/user/documents/file.txt

2/7
File Ownership & Permission in Linux.md 2024-01-16

# Relative path
./documents/file.txt

3. File Ownership:

Each file and directory in Linux has an owner and a group associated with it.

Owners can modify permissions and change ownership.

File ownership can be changed using chown.

Examples:

# Change ownership of a file


chown newowner:newgroup filename

4. File Permissions:

Basic Permissions:

Read (r): Allows reading or viewing a file.


Write (w): Allows modifying or deleting a file.
Execute (x): Allows executing a file or traversing a directory.

Permission Categories:

Owner (u): Permissions for the owner of the file.


Group (g): Permissions for the group associated with the file.
Others (o): Permissions for everyone else.

Examples:

ls -l gitspace.txt
- rwxrw-r--1 linuxlab virtualenv 457 Jan 10 11:55 gitspace.txt

File Information:

File Type: Regular File


Permissions: rwxrw-r--
Hard Link Count: 1
User Owner: linuxlab
Group Owner: virtualenv
File Size: 457 bytes
Modification Timestamp: Jan 10 11:55
File Name: gitspace.txt

Breakdown:

3/7
File Ownership & Permission in Linux.md 2024-01-16

File Type: The file is a Regular File.

Permissions: The permissions are set to allow the owner (linuxlab) to read, write, and execute, the
group (virtualenv) to read and write, and others to read only.

Hard Link Count: There is 1 hard link to the file.

User Owner: The file is owned by the user 'linuxlab.'

Group Owner: The file belongs to the group 'virtualenv.'

File Size: The file size is 457 bytes.

Modification Timestamp: The file was last modified on Jan 10 at 11:55.

File Name: The name of the file is 'gitspace.txt.'

# Change permissions of a file


chmod u+rwx,g+rw,o+r filename

Numeric Representation:

Each permission has a numeric value (read = 4, write = 2, execute = 1).


The sum of these values represents the permission level.

Examples:

ls -l gitspace.txt
- rwxrw-r--1 linuxlab virtualenv 457 Jan 10 11:55 gitspace.txt

Numerical Breakdown:

File Type: Regular File


Permissions: 764
Hard Link Count: 1
User Owner: linuxlab
Group Owner: virtualenv
File Size: 457 bytes
Modification Timestamp: Jan 10 11:55
File Name: gitspace.txt

In the octal representation:

rwx corresponds to 7 (read, write, and execute).


rw- corresponds to 6 (read and write).
r-- corresponds to 4 (read).

So, the octal representation for rwxrw-r-- is 764.

4/7
File Ownership & Permission in Linux.md 2024-01-16

# Numeric representation for rwxr-xr--


chmod 764 filename

A. File Ownership - Users and Groups

Imagine your Linux computer as a bustling city, and each file represents a building. Users are the occupants,
and groups are communities within the city.

Checking Ownership:

ls -l important_document.txt

In the output, the first column shows the file's owner (e.g., john), and the second column displays the
associated group (e.g., users).

Changing Ownership:

sudo chown jane:teamA important_document.txt

Using sudo (superuser privileges), you, as the system administrator, can transfer ownership to Jane and assign
the file to the 'teamA' group.

B. File Permissions - Access Shields

Now, each file in the city has access shields, determining who can view, modify, or run it. The shields have
layers for the owner, the group, and others.

Checking Permissions:

ls -l confidential_report.pdf

The output displays permission settings like -rw-r-----, where the first three characters are for the owner,
the next three for the group, and the last three for others.

Changing Permissions:

chmod u=rw, g=r, o= confidential_report.pdf

This command grants read and write permissions to the owner, read-only to the group, and no permissions to
others.

Practical Example
5/7
File Ownership & Permission in Linux.md 2024-01-16

Imagine you have a sensitive file called financial_data.xlsx in your city's finance department. You, as the
administrator, initially set strict permissions:

Initial Setup:

-rw------- 1 john users 2048 Jan 1 12:00 financial_data.xlsx

Here, John (the owner) has read and write permissions, while the 'users' group has no access.

Changing Ownership:

sudo chown jane:finance financial_data.xlsx

Now, Jane is the new owner, and the file belongs to the 'finance' group.

Modifying Permissions:

sudo chmod u=rw, g=r, o= financial_data.xlsx

The file's access shields now allow Jane to read and write, the 'finance' group to read, and others have no
access.

C. Checking and Displaying File Permissions in Linux: Using getfacl and stat Commands

To list the permissions of a specific user and group for a file in Linux, you can use the getfacl command. This
command is part of the Access Control List (ACL) utilities and provides a detailed list of permissions for a file,
including user and group permissions.

Here is an example:

getfacl filename

Replace "filename" with the actual name of the file you want to check. The output will include information
about the owner, group, and any additional users or groups with specific permissions.

For a more concise view focusing on just the user and group permissions, you can use the stat command:

stat -c "%U %G %a" filename

This command prints the username of the file owner, the group name, and the octal representation of the file
permissions.

6/7
File Ownership & Permission in Linux.md 2024-01-16

%U: File owner's username.


%G: File's group name.
%a: Octal representation of file permissions.

Adjust the "filename" accordingly to the file you are interested in.

These concepts are fundamental for managing files and directories effectively in a Linux environment.
Understanding and using them correctly ensures a secure and organized system.

7/7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy