Working with Group Permissions
Linux allows files to be associated with groups, enabling collaborative access control. In this step, you'll learn how to create a group, change a file's group ownership, and set appropriate group permissions.
First, let's create a new group. In a real-world scenario, this might represent a department or project team:
sudo groupadd developers
Now, let's create a file that will be shared with this group:
touch shared_config.txt
echo "## Development configuration settings" > shared_config.txt
echo "debug_mode=true" >> shared_config.txt
echo "log_level=verbose" >> shared_config.txt
Currently, this file belongs to your user and your primary group. Let's check:
ls -l shared_config.txt
You should see output similar to:
-rw-rw-r-- 1 labex labex 61 Oct 25 13:00 shared_config.txt
To change the group ownership of the file to the developers
group, use the chown
command:
sudo chown labex:developers shared_config.txt
The syntax is chown user:group filename
. Here, we're keeping the user as labex
but changing the group to developers
.
Check the ownership now:
ls -l shared_config.txt
You should see:
-rw-rw-r-- 1 labex developers 61 Oct 25 13:00 shared_config.txt
Notice that the group has changed to developers
, but the permissions are still the same. Let's modify them to allow group members to read but not write, while preventing access from others:
chmod 640 shared_config.txt
This sets:
- Owner: Read and write (
6
= 4+2
)
- Group: Read only (
4
)
- Others: No permissions (
0
)
Check the permissions again:
ls -l shared_config.txt
You should now see:
-rw-r----- 1 labex developers 61 Oct 25 13:00 shared_config.txt
Now members of the developers
group can read this configuration file, but they cannot modify it. Users who are not in the developers
group cannot access the file at all.
You can also use symbolic mode to achieve the same result:
chmod u=rw,g=r,o= shared_config.txt
This command explicitly sets:
- User/owner: Read and write
- Group: Read only
- Others: No permissions
To add a user to the developers
group, you would typically use:
sudo usermod -a -G developers username
However, this is beyond the scope of this particular lab, as it would require creating additional user accounts.