Linux: Make current directory and all newly create files and folders group writable

There are two main ways to change the permissions on a directory and its future contents using the command line to allow the group to write:

1. Using chmod:

  • Change permissions on the existing directory:

Bash

sudo chmod g+w html

This command adds write permission (+w) for the group (g) to the html directory.

  • Make future contents writable by the group:

Bash

sudo chmod g+w -R html

The -R flag applies the chmod command recursively, meaning it affects the directory itself and all files and subdirectories within it.

2. Using chown and umask:

  • Change the group ownership of the directory:

Bash

sudo chown :www-data html

This changes the group owner of the html directory to www-data.

  • Set the umask to allow group writable new files and directories:

Bash

sudo umask 002

The umask command sets the default permissions for newly created files and directories. Setting it to 002 gives read, write, and execute permissions to the owner and group, while leaving others with no access.

setfacl for new and future files/folders to specified user/group

To ensure that newly created files and directories within the “html” directory are owned by the “www-data” user and group, you can modify the default ACL (Access Control List) settings. Here’s the corrected setfacl command:

setfacl -Rdm u::rwx,g::rwx,o::rx,u:www-data:rwx,g:www-data:rwx html

This command sets default ACLs recursively (-R) for the “html” directory:

  • d: specifies default ACLs.
  • u::rwx sets the default permissions for the owner to read, write, and execute.
  • g::rwx sets the default permissions for the group to read, write, and execute.
  • o::rx sets the default permissions for others to read and execute.
  • u:www-data:rwx sets the default permissions for the “www-data” user to read, write, and execute.
  • g:www-data:rwx sets the default permissions for the “www-data” group to read, write, and execute.
See also  Guide: How to Mount and Unmount WSL Drives from Windows

This ensures that any new files or directories created within the “html” directory inherit these permissions, including ownership by the “www-data” user and group.

Troubleshooting

If you’re logged in as the user “xxx” who belongs to the “www-data” group, and you’re still experiencing issues with files being created as “xxx.xxx” instead of “www-data.www-data“, there might be a few reasons for this behavior:

  1. Primary Group: The primary group of the user “xxx” might not be set to “www-data“. Even though “xxx” belongs to the “www-data” group, by default, the files will be created with the primary group of “xxx“. You can check the primary group of the user “xxx” by running the following command:
id xxx

If the primary group is not set to “www-data“, you can change it using the following command:

sudo usermod -g www-data xxx

Replace “xxx” with the actual username if different.

  1. umask Settings: The umask settings might be affecting the default permissions and ownership of the files being created. Ensure that the umask value is not overriding the group ownership when files are being created. You can check the umask value by running:
umask

The umask value might be set in the user’s shell profile (like ~/.bashrc or ~/.bash_profile). Ensure it doesn’t restrict group write permissions.

  1. Directory ACLs: Confirm that the ACLs are correctly applied to the directory where you’re creating files. Check the ACLs of the parent directory with getfacl to ensure that the default ACLs are set correctly to inherit the group ownership of “www-data“.
getfacl /var/www/html

If all these settings seem correct and you’re still facing issues, it’s possible there might be other system-specific configurations affecting the file creation behavior.

See also  Limit projects in Jenkins for individual user

Choosing the right option:

  • If you only need to allow the group to write to existing files and directories, use chmod.
  • If you want the group to have write access to both existing and future content, use chmod -R or the combination of chown and umask.

Additional notes:

  • Always use sudo when modifying system files and directories.
  • Make sure you are replacing www-data with the actual name of your group if it’s different.
  • Be cautious when changing permissions, as granting unnecessary access can be a security risk.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.