Managing File Groups
In Linux, files and directories are not only associated with a specific user, but also with a group. Understanding and managing file groups is essential for controlling access permissions and ensuring secure file sharing.
Understanding File Groups
Every file and directory in Linux is associated with a group. When a file or directory is created, it inherits the primary group of the user who created it. The group ownership can be viewed using the ls -l
command.
$ ls -l
-rw-r--r-- 1 labex_user labex_group 100 Apr 12 12:34 example.txt
In the example above, the file example.txt
is owned by the group labex_group
.
Adding Users to Groups
To add a user to a group, use the usermod
command with the -a -G
options. The syntax is:
sudo usermod -a -G [group_name] [username]
Example:
$ sudo usermod -a -G labex_group new_user
In the example above, the user new_user
is added to the labex_group
group.
Changing Group Ownership
To change the group ownership of a file or directory, use the chown
command with the :[new_group]
syntax. The syntax is:
chown :[new_group] [file_or_directory]
Example:
$ ls -l example.txt
-rw-r--r-- 1 labex_user labex_group 100 Apr 12 12:34 example.txt
$ sudo chown :new_group example.txt
$ ls -l example.txt
-rw-r--r-- 1 labex_user new_group 100 Apr 12 12:34 example.txt
In the example above, the group ownership of the example.txt
file is changed from labex_group
to new_group
.
By understanding and managing file groups, you can effectively control access permissions and ensure secure file sharing among users on your Linux system.