I need to set the umask
value for one specific folder. I have a web application and when it creates some file the default permission is 700. But I need at least 755 permission for that file. How can one do that?
I need to set the umask
value for one specific folder. I have a web application and when it creates some file the default permission is 700. But I need at least 755 permission for that file. How can one do that?
you could use
setfacl
Where
name
is the group nameTo find which groups you or a specific user belong see In unix/linux how do you find out what group a given user is in via command line?
You cannot set umask per directory, it's a process-level value. If you need to prevent others from reading files in a directory, revoke the corresponding permissions bits.
For example, if you've a directory
/home/user/directory
with some files and directories which can get permissions like 777 from a process, set the permission bits of/home/user/directory
to something like 700. That will make it impossible for other users (excluding the superuser root) to descend in/home/user/directory
.I'm paranoid and set the permissions on
/home/user
to 750, so only I can read, write and descend in my home directory. This has as consequence that folders like/home/user/Public
cannot be accessed by others, but I can live with that.Per update of your question: still, you cannot control that in the filesystem (other than using a different filesystem type like FAT which is strongly discougared), you need to do that in your webapp. If your webapp is coded in PHP, you can change the umask on the fly using the
umask
function:You could put this in a configuration file, like the file containing the database connection password (thinking in apps like Wordpress).
Remember that it's a process value, some webservers allow you to set it in their configuration files, otherwise you could modify the startup scripts to set the desired umask. Remember that permissions like
755
and644
are quite dangerous for webapps, if the code is sensitive, everyone can read it.Provide anther solution implemented with shell hooks and
direnv
. The following solution may be more compatible in casesetfacl
is not available on your system. (e.g. macOS)Use
.envrc
toexport
customumask
value for specific dir, and theexport
ed env var will be unloaded when you leave that dir.Define a hook to change the
umask
value once working dir is changed.For the time being, direnv hook initialization for zsh doesn't support
chpwd
hook. If the pull request GH-514 has not been merged when you see this page. Please comment outeval "$(direnv hook zsh)"
and hookdirenv
onchpwd
manually with following code,Source: dynamic-umask-based-on-cwd.md
Another solution might be to just set the group id on files created in the directory, which makes the new files owned by the directory group id, instead of the group id of the user that created the files. So I think you could just do:
This sets the setgid special file permission, which would cause all files created in
/my/folder
to be owned by thewww-data
group, which has then hasrx
(5
) permission because of the parent directory.The special permission is defined in the first digit using octal notation. Add together the numbers below to combine options (e.g.
3
for both sgid and sticky bit)1
for the sticky bit2
for sgid4
for suidReference: https://en.wikipedia.org/wiki/Setuid
To change permissions for a folder use chmod. umask is for files.Set umask to what you need by
and change back when your done
Related, but maybe not applicable in this case, the following clip is from
.zshrc
:So for interactive use, something like that would work (but, obviously, not to provide security).