-2

I'm using CentOS on a virtual dedicated server.

When I go in as root, and change the chmod value of a folder it works. But, any files I place into that folder do not inherit the chmod I set.

This is what I used:

chmod -Rv 777 files

the return was this:

mode of `files' retained as 0777 (rwxrwxrwx)
mode of `files/1344188366-f7cad6aa87685db775817d4168c329ef-AUTO_INSURANCE2.csv'
changed to 0777 (rwxrwxrwx)

Okay so one file changed. But if I place any files into that folder they will have a different chmod value. How do I make chmod stick with every file and folder within that directory?

The files I want default chmod set to are being uploaded through XHR. I.E a file upload process from browser to server.

How do I set the chmod level on the application code in my PHP?

EDIT

I think I have found a reasonable solution to my problem. The answer below solves the file permission issue. But after running get_current_user() in php, I realized the user the script is running under is different than the user that the files I am trying to work on. The file user is apache because that's who created it. But the PHP script is a different user, the default user.

please delete me
  • 127
  • 1
  • 1
  • 5
  • So you want everyone in the whole world to be able to change all your files whenever they want? This is _not_ secure. – Michael Hampton Aug 05 '12 at 17:49
  • 2
    1. NEVER `chmod 777`. 2. Google: SGID. – quanta Aug 05 '12 at 17:50
  • I know I know, but the server itself is isolated and no one can access it. Those who can should be able to 777. Thanks for the concern though. – please delete me Aug 05 '12 at 17:51
  • Why do you want the mode 777 for a csv file anyway? Try as root `chmod 1777 files` and then upload a file. Is that what you want? – ott-- Aug 05 '12 at 19:12
  • Also, (if this is an ext filesystem) an acl would likely help here rather than fiddling with umasks. – Sirex Aug 05 '12 at 20:10
  • There is literally hundreds of different solutions online. I am going through each and everyone, editing and rebooting. By the end of this my server will be a piece of junk and I'll have to format. Can anyone please help me? Just simply want to change default chmod on a file directory. That's it. – please delete me Aug 05 '12 at 21:22
  • It may be easiest to just do this the "right" way, setting actual security policies and deciding who gets access to what, under which circumstances. Since there's very little detail about your particular situation, it's hard to advise further. – Michael Hampton Aug 05 '12 at 22:29
  • Why the down votes. It seems like if your not an advanced developer presenting complex problems on this board you get negged. What is wrong with having a simple problem when you are a beginner? Are beginners not allowed on Stackoverflow? Did not I show effort on my part to resolve this problem instead of just lookinf for a handout? Why do people keep doing this. – please delete me Aug 05 '12 at 22:38
  • 2
    So far you haven't even really said what problem you're trying to solve. You're too fixated on your chosen "solution" which unfortunately is the "wrong" solution for so many reasons it would take a book to list them all. You'll make progress when you take a step back and look more carefully at what you're trying to do (and maybe tell the rest of us). – Michael Hampton Aug 06 '12 at 00:13

2 Answers2

5

You'll need to modify the umask instead. This can be defined per-user (in ~/.profile) or system-wide (i.e. /etc/profile).

A great breakdown of this concept and use-cases is available here.

A more detailed breakdown of the unmask and octal permissions is available here.

Edit:

Your specific issue is how to set permissions on files uploaded via PHP through Apache. THe same concept of umask applies, except it needs to be defined in the right place. On a typical Apache installation, that place would be the environment variables file; envvars on some distributions. /etc/sysconfig/httpd on RHEL/CentOS.

Also see: How to override default permissions for files stored by Apache/PHP in /tmp?

ewwhite
  • 197,159
  • 92
  • 443
  • 809
  • 1
    This depends heavily on what you're doing and who's accessing the system. If you're doing this all as the root user, that's typically bad practice. Please use a non-root user. But what you're looking for can be accomplished with `umask 000`. Try entering that command, then doing the file creation/actions you need, and see if the behavior is what you want. If it is, you could place `umask 000` in your user's login script. – ewwhite Aug 05 '12 at 18:04
  • I tried > umask000 chmod -Rv 777 files but it didn't work. It changed the permission for the files currently in the directory to 777, but, did not take effect for future files. – please delete me Aug 05 '12 at 18:10
  • This probably won't work for root because the umask setting is overridden in /etc/profile. You should be doing the work on your VPS as another user, if possible. Also, how are your "future files" being created? If it's FTP, for example, there may need to be a change to the umask settings on your FTP server. – ewwhite Aug 05 '12 at 18:13
  • I tried using a different, non root user, with the same line but still no dice. Just the default 644 chmod setting. The files are being uploaded through XHR. – please delete me Aug 05 '12 at 18:16
  • If [XHR](http://en.wikipedia.org/wiki/XMLHttpRequest), then the umask needs to be set within your application code. The settings I provided above only cover shell/session operations. – ewwhite Aug 05 '12 at 18:21
  • 3
    When adding context to the question, please try to update the question and rephrase it accordingly so all of the information is in one place. Don't litter the comment list with bits and pieces. – Henk Langeveld Aug 05 '12 at 19:09
  • I made the changes to my post to reflect the XHR. @ewwhite I tried the solutions in the links you gave me but none of them worked. Files are still being uploaded as 644 instead of 777. I changed the umask in /etc/profile, I went into ssh and executed echo "umask 000" >> /etc/sysconfig/httpd, and rebooted my server after each step. Still nothing. I am actually surprised such a non meta task (or I would think changing default chmod would be run of the mill) could be so difficult. – please delete me Aug 05 '12 at 20:57
  • @SofianeMerah See my edits above. – ewwhite Aug 06 '12 at 00:11
  • Well by placing umask 000 at the bottom of /etc/sysconfig/httpd it now changes the permission of the files to 666. Its not 777 but I'll be happy with its all the read write permissions that I wanted? Thanks. – please delete me Aug 06 '12 at 03:37
  • Whats weird is that even though the file is 666 chmod, its still coming up false on a isreadable() php function.. *sigh* I'm almost at my limit. – please delete me Aug 06 '12 at 04:15
0

What you will need to do is set the group permissions on that folder...

setting it up like a collaborative folder:

chmod 2777 /what/ever/folder

That way any file placed in the folder takes on the group settings by default

or

setfacl -m d:g:whatever-group:rwx /what/ever/folder
Kenny Rasschaert
  • 9,045
  • 3
  • 42
  • 58
Jay
  • 36
  • 2
  • Yeah I tried that also. I also tried setting the umaks in all these directories /etc/profile
    /etc/bashrc
    /etc/sysconfig/httpd
    /etc/init.d/rc.d/httpd
    Yet still nothing. I added the php user to the apache group also. Nothing. I uploaded the file and manually chmoded it 777 for the hell of it and then tried to write to it with the php script. Crazy, but still did not work. This boggles me so much. The csv files are all user apache the php is user foo. I played around with the users and groups a lot. Nothing...
    – please delete me Aug 07 '12 at 00:50