Tuesday, April 30, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 19639  / 1 Year ago, fri, february 3, 2023, 10:42:24

I am using Linux 12.04,apache and php is installed on it.I want to access a text file in /root/ folder.I am really confused with the permissions.The php script i m using



<?php
$file = fopen("/root/cr.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached

while(!feof($file))
{
echo fgets($file). "<br>";
}

fclose($file);
?>


This script is able to access the file /var/www folder but not able to access /root/ip.txt file. Please help and explain step to step possible.


More From » permissions

 Answers
0

I will forget about the security implications of doing this and will go down to business:



If you have done ls -l /var/www and ls -l /root you would have noticed that both has different permissions:



$ ls -l /root/cr.txt
total 2
-rw-r----- 1 root root 0 Jul 9 01:28 cr.txt
$ ls -l /var/www
total 2
-rw-r--r-- 1 www-data www-data 0 Jul 9 01:28 somefile


The /root is only readable for root, while /var/www is readable by www-data user. Now, if you check apache process you will notice that it's running using the www-data user.



$ ps aux | grep apache
www-data 5133 0.0 0.2 6512 1208 ? R+ 10:04 0:00 apache



Now, you are trying to make apache running with the www-data user read the file. You can take three courses of action:




  1. Move the file to /var/www and change it's permissions so www-data users can read it.



    mv /root/cr.txt /var/www/
    chown www-data:www-data /var/www/cr.txt


    This is the preferable method.


  2. Create a symlink to the file in the /var/www directory:



    ln -s /root/cr.txt /var/www/


    This won't ensure that your file is being read, in some cases.


  3. This is dangerous and should not be done! Add the www-data user to the root group, or change the file ownership so it could be read by www-data users:



    chown :www-data /root/cr.txt
    ## Or
    sudo adduser www-data root


    This shouldn't be done if you don't understand the risks!



[#30060] Saturday, February 4, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antoccasiona

Total Points: 430
Total Questions: 127
Total Answers: 131

Location: Netherlands
Member since Sat, Jun 26, 2021
3 Years ago
;