LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-10-2022, 11:41 PM   #1
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Rep: Reputation: 73
Cloud server permissions: What is right?


I don't know much about this. I have a Ubuntu 20.04 Server for my little homework webpage.

I just made an upload form for students to send me files.

I had public_html permissions set to 750. That didn't work.

I tried 755, upload kind of worked, except the uploaded file could not be written to the target directory

/public_html/uploads20BE/php/uploads/

PHP error was: permission denied

Only after I set permissions in public_html to 777 could PHP send my file to the target directory.

Do I need different permissions for every folder??
 
Old 02-11-2022, 12:12 PM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Pedroski View Post
I don't know much about this. I have a Ubuntu 20.04 Server for my little homework webpage.

I just made an upload form for students to send me files.

I had public_html permissions set to 750. That didn't work.

I tried 755, upload kind of worked, except the uploaded file could not be written to the target directory

/public_html/uploads20BE/php/uploads/

PHP error was: permission denied

Only after I set permissions in public_html to 777 could PHP send my file to the target directory.

Do I need different permissions for every folder??
Consider which files your server needs to be able to read to serve a page, and you can answer if the permissions need to be applied recursively or not, to files and folders.

But, more importantly, also consider file ownership, also group membership! It's usually (almost always) safer to adjust that, to make things work.
chmod 777, otoh, is extremely unsafe and shoul really never be used, server or not.

As which user does your server software and PHP run?
While the master process is probably root, the workers usually run as www-data:
Code:
$> ps aux | grep -E 'nginx|php'
root       581  0.0  0.2 214184  7192 ?        Ss    2021   8:20 php-fpm: master process (/etc/php/7.3/fpm/php-fpm.conf)
www-data  3816  0.1  0.7 217168 27392 ?        S    14:51   0:21 php-fpm: pool www
www-data  6485  0.1  0.9 222228 34288 ?        S    Feb10   2:16 php-fpm: pool www
www-data 22339  0.2  0.7 215612 27000 ?        S    08:01   1:38 php-fpm: pool www
root     29139  0.0  0.0  18852   784 ?        Ss   11:42   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 29140  0.0  0.1  19440  5888 ?        S    11:42   0:24 nginx: worker process
In this scenario
Code:
#> chown -R www-data:www-data /public_html
might already help (after undoing all your chmod shenanigans).
 
1 members found this post helpful.
Old 02-11-2022, 06:41 PM   #3
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thanks for your reply.

I can imagine, 777 is maybe not so good (good thing it is only a homework page), but it was the only way I could get PHP to park the file where I wanted it!

On my laptop, I have /var/www/html/ set to: owner: pedro, group www-data

That works at home. I always try everything here first, then upload when I know it works.

So I set the server the same: /var/www/mywebpage.com/public_html/ owner: pedro, group www-data (This is the first time I have a cloud server, before I only had shared hosting.)

You seem to be saying, I should make the owner www-data? (and add myself to group www-data??)

On the server, pedro is a sudoer.

If I set everything to owner www-data, when it comes to editing files on the server, will I have problems??

Following advice from LQ, I often run these commands, because when I copy a file into /var/www/html/ at home, www-data can't touch it.

Quote:
sudo chown -R pedro /var/www/html/
sudo chgrp -R www-data /var/www/html/
sudo chmod -R 770 /var/www/html/
sudo chmod g+s /var/www/html/
I thought the last one would help set the correct permissions, but it does not seem to do that.

I also found these commands on the internet:

Quote:
find /var/www/ -type d -print0 | xargs -0 chmod 755
find /var/www/ -type f -print0 | xargs -0 chmod 644
What would be the best permissions commands for the cloud server in this situation??

Last edited by Pedroski; 02-11-2022 at 06:48 PM.
 
Old 02-11-2022, 11:04 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
The directory to which the uploads are to be saved needs to be writable by the web user…the one that is running the PHP script. I usually do that by setting the owner of that directory to the web user. Permissions to 755. Just that one directory…not “everything”.
You (pedro) should still be able to read the files therein.

DO NOT set permissions to 777!

Last edited by scasey; 02-11-2022 at 11:05 PM.
 
1 members found this post helpful.
Old 02-12-2022, 01:05 AM   #5
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Aha!

And who is running the PHP script??

A student clicks a button on the webpage and the data are sent.

PHP checks if the student number is in the database.

PHP checks out the uploaded file. Bigger than 9MB will be rejected, smaller than 1MB will be rejected.

The web user is www-data?? I am not clear on this point.
 
Old 02-12-2022, 05:44 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Pedroski View Post
And who is running the PHP script??

A student clicks a button on the webpage and the data are sent.
Your web server is running the script.
AFAIK, PHP is automatically started by your web server, so it runs as the same user.
Web servers ususally run as www-data - look at its config file to make sure.
 
2 members found this post helpful.
Old 02-12-2022, 04:10 PM   #7
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thank you very much!

I set:

Quote:
find /var/www/mywebpage.com/public_html/ -type d -print0 | xargs -0 chmod 755
find /var/www/ -type f -print0 | xargs -0 chmod 644
chown -R www-data:www-data /var/www/mywebpage.com/public_html/
Now, the upload is working fine, also other pages which write data to, or read data from, MySQL work normally.

To open a page for editing, I need to be sudo. I was worried, when I save it, sudo will be the owner, but the owner remains www-data.

Lesson learned: on a server owner, group should be www-data!
 
Old 02-13-2022, 08:46 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Pedroski View Post
To open a page for editing, I need to be sudo. I was worried, when I save it, sudo will be the owner, but the owner remains www-data.
You can also edit it with
Code:
sudo -u www-data ...
You could also add yourself (pedro) to the group www-data, but then you'd need to use 755 resp. 664 permissions for the public_html area.
 
Old 02-13-2022, 09:30 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
Quote:
Originally Posted by Pedroski View Post
Lesson learned: on a server owner, group should be www-data!
Only for the specific directories and files which need to be written by the web server. Everything else should belong to other accounts and groups, as read-only access is all that's needed for basic web services. Think in terms of least privilege and privilege separation.

You can have files and directories shared by multiple accounts, but have to hop through several hoops with EXT4 to set up sharing.
 
2 members found this post helpful.
Old 02-17-2022, 08:49 PM   #10
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
PROBLEM

The owner and group of public_html is www-data

Now I can't upload!

From Filezilla:

Quote:
Error: /var/www/mywebpage.com/public_html/20BE1cw/login.php: open for write: permission denied
Error: File transfer failed
How do I get my files to the server now??

Can I login as www-data? Impersonate www-data??
 
Old 02-17-2022, 09:00 PM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
Quote:
Originally Posted by Pedroski View Post
Can I login as www-data? Impersonate www-data??
No, and no, at least not in any way that will reduce the trouble.

If two or more accounts are to share write access to part of the file system then you'll have to use the appropriate permissions. The chown, chgrp and chmod you showed in the first part of #3 were very close.

Code:
sudo chown -R pedro:www-data /var/www/html/
sudo find /var/www/html/ -type d -exec chmod u=rwx,g=rwxs,o=rx {} \;
sudo find /var/www/html/ -type f -exec chmod u=rw,g=rw,o=r {} \;
If either you or www-data still have trouble, and if it is because of the umask, then that too will have to be addressed.
 
1 members found this post helpful.
Old 02-17-2022, 09:04 PM   #12
Pedroski
Senior Member
 
Registered: Jan 2002
Location: Nanjing, China
Distribution: Ubuntu 20.04
Posts: 2,116

Original Poster
Rep: Reputation: 73
Thanks! I'll try it right now!

Yes, thanks again, now I can upload!

pedro@ebs-105224:/var/www/mywebpage.com/public_html/20BE1cw$ ls -al
total 300
drwxrwsr-x 3 pedro www-data 4096 Nov 17 07:50 .
drwxrwsr-x 21 pedro www-data 4096 Feb 12 12:12 ..
-rw-rw-r-- 1 pedro www-data 2450 Oct 14 10:34 20BE1leitfile.html.php
-rw-rw-r-- 1 pedro www-data 21053 Oct 14 10:34 20BE1wW1.html.php
-rw-rw-r-- 1 pedro www-data 10985 Nov 17 12:12 20BE1wW11.html.php
-rw-rw-r-- 1 pedro www-data 21300 Oct 14 10:34 20BE1wW2.html.php
-rw-rw-r-- 1 pedro www-data 21350 Oct 14 10:34 20BE1wW3.html.php
-rw-rw-r-- 1 pedro www-data 20342 Oct 14 10:34 20BE1wW3html.php
-rw-rw-r-- 1 pedro www-data 18523 Oct 14 10:34 20BE1wW4.html.php
-rw-rw-r-- 1 pedro www-data 26821 Oct 14 10:34 20BE1wW5.html.php
-rw-rw-r-- 1 pedro www-data 22267 Oct 14 10:34 20BE1wW6.html.php
-rw-rw-r-- 1 pedro www-data 16538 Oct 14 10:34 20BEsW1.html.php
-rw-rw-r-- 1 pedro www-data 18854 Oct 14 10:34 20BEsW2.html.php
-rw-rw-r-- 1 pedro www-data 2211 Oct 14 10:34 changePW.php
-rw-rw-r-- 1 pedro www-data 2632 Oct 14 10:34 changePW_form.php
-rw-rw-r-- 1 pedro www-data 1482 Oct 14 10:34 checkboxes_fieldset.html
-rw-rw-r-- 1 pedro www-data 407 Oct 14 10:34 conn.php
-rw-rw-r-- 1 pedro www-data 809 Oct 14 10:34 function.js
-rw-rw-r-- 1 pedro www-data 1406 Oct 14 10:34 holiday_week_page.html
-rw-rw-r-- 1 pedro www-data 3879 Nov 17 12:09 index.php
-rw-rw-r-- 1 pedro www-data 1406 Oct 14 10:34 jsclock.html.php
-rw-rw-r-- 1 pedro www-data 5863 Nov 17 09:38 login.php
-rw-rw-r-- 1 pedro www-data 3566 Oct 14 10:34 login.php.backup
-rw-rw-r-- 1 pedro www-data 3568 Oct 14 10:34 login.php.backup2
-rw-rw-r-- 1 pedro www-data 5636 Nov 17 07:36 login.php~
drwxrwsr-x 2 pedro www-data 4096 Nov 17 07:51 php
-rw-rw-r-- 1 pedro www-data 2445 Oct 14 10:34 register.php
-rw-rw-r-- 1 pedro www-data 3067 Oct 14 10:34 register_form.php
-rw-rw-r-- 1 pedro www-data 1226 Oct 14 10:34 register_success.html.php
pedro@ebs-105224:/var/www/mywebpage.com/public_html/20BE1cw$

Last edited by Pedroski; 02-17-2022 at 09:14 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Cloud 5: Dispelling cloud myths, the cloud security excuse, and the fight for cloud supremacy LXer Syndicated Linux News 0 06-20-2014 04:12 PM
LXer: Cloud 5: NSA not killing cloud, cloud IT jobs, rise of cloud brokers LXer Syndicated Linux News 0 03-02-2014 09:51 AM
LXer: Cloud 5: Netflix's cloud-connected brain, 5 cloud myths and from cloud to fog LXer Syndicated Linux News 0 02-21-2014 02:20 PM
LXer: It's a cloud, cloud, cloud, cloud world LXer Syndicated Linux News 0 07-23-2013 05:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration