LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-18-2020, 06:02 AM   #1
bigred_247
LQ Newbie
 
Registered: Apr 2020
Location: essex
Distribution: amazon linux 2, ubuntu
Posts: 8

Rep: Reputation: Disabled
Can not restrict the Document Root directory for Apache - help required


Hi folks,

I'm pretty new to Linux and am trying to lock down Apache 2.4 but can not get the following command to work. FYI, I am using Amazon Linux 2 on AWS:

example found online:
find -L $DOCROOT -group $GRP -perm /g=w -print | xargs chmod g-w

my command:
find -L /var/www/html -group apache -perm /g=w -print | xargs chmod g-w

but I receive the error: "chmod: missing operand after ‘g-w’"

Can anybody shed some light or assist?
 
Old 04-18-2020, 07:42 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by bigred_247 View Post
Hi folks,

I'm pretty new to Linux and am trying to lock down Apache 2.4 but can not get the following command to work. FYI, I am using Amazon Linux 2 on AWS:

example found online:
find -L $DOCROOT -group $GRP -perm /g=w -print | xargs chmod g-w

my command:
find -L /var/www/html -group apache -perm /g=w -print | xargs chmod g-w

but I receive the error: "chmod: missing operand after ‘g-w’"

Can anybody shed some light or assist?
Most there is nothing for xargs to apply
You can use xargs -r, so it stops complaining

Last edited by bathory; 04-18-2020 at 08:20 AM. Reason: Wrong answer
 
Old 04-18-2020, 07:44 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Welcome.

I'd not trust plain -print, the files or directories might have spaces in them. You could use -print0 with find and --null with xargs but -exec is another option:

Code:
find -L /var/www/html -group apache -perm /g=w -print -exec chmod g-w {} \;
 
Old 04-18-2020, 08:51 AM   #4
bigred_247
LQ Newbie
 
Registered: Apr 2020
Location: essex
Distribution: amazon linux 2, ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Welcome.

I'd not trust plain -print, the files or directories might have spaces in them. You could use -print0 with find and --null with xargs but -exec is another option:

Code:
find -L /var/www/html -group apache -perm /g=w -print -exec chmod g-w {} \;

Thank you very much for the welcome and quick reply.


1) I have just tried run the command above but there was no return output. FYI, the 'html' directory is owned by root which is the default and by default it does not have the 'w' permission. See below:

[ec2-user@ip-172-31-22-72 www]$ find -L /var/www/html -group apache -perm /g=w -print -exec chmod g-w {} \;
[ec2-user@ip-172-31-22-72 www]$ ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root root 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel


2) My understanding is that if the directory was owned by the apache group, the above command 'g-w' would remove (w) write access from the apache group if it was the owner? With this in mind, I decided to change the owner of the 'html' directory from 'root' to 'apache' to test whether this command actually works. I then ran the same command but with a 'w-r' to see if it would work because the apache group already was missing the write 'w' permission. See output below:

[ec2-user@ip-172-31-22-72 www]$ sudo chgrp -R apache /var/www/html
[ec2-user@ip-172-31-22-72 www]$ ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root apache 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel

[ec2-user@ip-172-31-22-72 www]$ find -L /var/www/html -group apache -perm /g=w -print -exec chmod g-w {} \;
[ec2-user@ip-172-31-22-72 www]$ ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root apache 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel

[ec2-user@ip-172-31-22-72 www]$ find -L /var/www/html -group apache -perm /g=r -print -exec chmod g-w {} \;
/var/www/html
chmod: changing permissions of ‘/var/www/html’: Operation not permitted
[ec2-user@ip-172-31-22-72 www]$ sudo find -L /var/www/html -group apache -perm /g=r -print -exec chmod g-w {} \;
/var/www/html
[ec2-user@ip-172-31-22-72 www]$ ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root apache 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel
[ec2-user@ip-172-31-22-72 www]$ sudo find -L /var/www/html -group apache -perm /g=x -print -exec chmod g-w {} \;
/var/www/html
[ec2-user@ip-172-31-22-72 www]$ ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root apache 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel
[ec2-user@ip-172-31-22-72 www]$ sudo chgrp -R root /var/www/html
[ec2-user@ip-172-31-22-72 www]$ ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root root 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel
[ec2-user@ip-172-31-22-72 www]$


3) I even tried adding the 'w' write permission to root to see if i could first add then remove using the same command but testing it with the group 'root' but I was unable to assign it the 'w' permission. See below:

[ec2-user@ip-172-31-22-72 www]$ find -L /var/www/html -group root -perm /g=w -print -exec chmod g+w {} \;
[ec2-user@ip-172-31-22-72 www]$ ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root root 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel



4) Just to contextualise things, I'm trying to write a shell script that will run against a vanilla apache installation on Amazon Linux 2 of which one of the requirements is to "Ensure Group Write Access for the Document Root Directories and Files Is Properly Restricted"


Apologies for the long reply. Just trying to identify my thought process. I suspect this is user error on my part

Last edited by bigred_247; 04-18-2020 at 09:08 AM.
 
Old 04-18-2020, 09:01 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
If the directories are owned by another account, such as root, then you'll have to use that other account to make the changes using find and chmod there:

Code:
sudo find -L /var/www/html -group apache -perm /g=w -exec chmod g-w {} \;
Strictly speaking, the -print option is not needed there but when present it provides a little more verbosity to watch what is going on.

Later, if you are going to share the directories with more than just yourself, you might need the write permissions for the groups turned back on, just not for the web server's group. See https://www.linuxquestions.org/quest...e-users-37043/
 
1 members found this post helpful.
Old 04-18-2020, 09:24 AM   #6
bigred_247
LQ Newbie
 
Registered: Apr 2020
Location: essex
Distribution: amazon linux 2, ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
If the directories are owned by another account, such as root, then you'll have to use that other account to make the changes using find and chmod there:

Code:
sudo find -L /var/www/html -group apache -perm /g=w -exec chmod g-w {} \;
Strictly speaking, the -print option is not needed there but when present it provides a little more verbosity to watch what is going on.

Later, if you are going to share the directories with more than just yourself, you might need the write permissions for the groups turned back on, just not for the web server's group. See https://www.linuxquestions.org/quest...e-users-37043/

I just tried the below? is this what you meant? I tried giving root the 'w' permission but it did not seem to work.

In the meantime, I'll refer to the post you just sent. Thank you very much for your help so far

[ec2-user@ip-172-31-22-72 www]$ sudo -s
[root@ip-172-31-22-72 www]# find -L /var/www/html -group root -perm /g=w -print -exec chmod g+w {} \;
[root@ip-172-31-22-72 www]# ll
total 4
drwxr-xr-x 2 root root 6 Oct 22 22:59 cgi-bin
drwxr-xr-x 2 root root 6 Apr 17 11:06 html
drwx------ 12 root root 4096 Apr 15 11:44 laravel

Last edited by bigred_247; 04-18-2020 at 09:25 AM.
 
Old 04-23-2020, 10:52 AM   #7
bigred_247
LQ Newbie
 
Registered: Apr 2020
Location: essex
Distribution: amazon linux 2, ubuntu
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you for the link that you posted. This helped

Quote:
Originally Posted by Turbocapitalist View Post
If the directories are owned by another account, such as root, then you'll have to use that other account to make the changes using find and chmod there:

Code:
sudo find -L /var/www/html -group apache -perm /g=w -exec chmod g-w {} \;
Strictly speaking, the -print option is not needed there but when present it provides a little more verbosity to watch what is going on.

Later, if you are going to share the directories with more than just yourself, you might need the write permissions for the groups turned back on, just not for the web server's group. See https://www.linuxquestions.org/quest...e-users-37043/
 
  


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
I was unable to set apache document root for itop tool,,,,could any one help me t luckncool Linux - Newbie 3 01-25-2014 08:38 AM
Apache 2.2 - can not access virtual host document root bobnw Linux - Newbie 8 06-11-2009 06:16 PM
how to make my home directory as Document root for apache antonyphilip Linux - Newbie 3 05-12-2009 04:50 AM
html; character encoding per document tag, not whole document TheLinuxDuck Programming 0 08-14-2008 11:12 AM
how do i create a document, then a directory and save the document in it. jtdodge Linux - Newbie 9 06-15-2005 11:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:49 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