Basic File Permissions and Ownership in Linux
File Permissions
File permissions in Linux determine who can read, write, or execute a file. Each file or directory has an associated set of permissions that defines what actions can be performed by the owner, the group, and others.
Permission Types
- Read (r): Permission to read the contents of the file or directory.
- Write (w): Permission to modify the contents of the file or directory.
- Execute (x): Permission to execute the file or traverse the directory.
Permission Representation
Permissions are represented in two ways:
- Symbolic notation: A string of characters such as
rwxr-xr--
. - Octal notation: A numeric representation like
755
.
The symbolic notation consists of ten characters, with the first character indicating the file type (-
for regular files, d
for directories, etc.), followed by three sets of three characters each representing the permissions for the owner, group, and others.
-rwxr-xr--
In this example:
rwx
: The owner has read, write, and execute permissions.r-x
: The group has read and execute permissions.r--
: Others have read-only permissions.
Using chmod
to Change Permissions
The chmod
command is used to change the permissions of a file or directory. You can use both symbolic and octal notation to set permissions.
Symbolic Notation
To change permissions using symbolic notation:
chmod [permissions] [filename]
Example:
chmod u+x,g-w,o=r file.txt
This command adds execute permission for the owner, removes write permission for the group, and sets read-only permission for others on file.txt
.
Octal Notation
To change permissions using octal notation:
chmod [permissions] [filename]
Example:
chmod 755 file.txt
This command sets read, write, and execute permissions for the owner, and read and execute permissions for the group and others on file.txt
.
File Ownership
Every file or directory in Linux has an owner and an associated group. The owner is typically the user who created the file, and the group consists of users who share the same permissions.
Using chown
to Change Ownership
The chown
command is used to change the ownership of a file or directory. You can change both the owner and the group using this command.
Changing Owner
To change the owner of a file:
chown [new_owner] [filename]
Example:
chown alice file.txt
This command changes the owner of file.txt
to the user alice
.
Changing Group
To change the group of a file:
chown :[new_group] [filename]
Example:
chown :developers file.txt
This command changes the group of file.txt
to developers
.
Changing Both Owner and Group
To change both the owner and the group of a file:
chown [new_owner]:[new_group] [filename]
Example:
chown alice:developers file.txt
This command changes the owner of file.txt
to alice
and the group to developers
.
Comments
Post a Comment