tags:

views:

820

answers:

3

How can you copy a folder to /var/www without sudo?

My folder codes has the following permissions at /var/www

4 drwxr-xr-x 8 root root 4096 2009-08-09 03:01 codes

I can only sudo cp -r ~/Dropbox/codes/ /var/www to copy the files. I cannot copy them without sudo.

A: 

What about adding the cron job to the root user. It's not a great idea, but it will get around the problem?

I also find it interesting that your www directory would have only root permissions and be owned by root. Usually they are owned by an apache user or have other permissions such that apache can access them.

Edit You probably don't want to edit the crontab file directy. But something similar to the following command should work:

sudo crontab -e -u root

You may not need the -u root but it's good so sudo doesn't confuse crontab.

Edit 2 You can change permissions with the chmod and chown commands:

sudo chmod 755 /path
sudo chown user:user /path

Be very very careful when using this. You can completely screw up the OS by changing permissions on the wrong files or folders. You'll probably want to add the -R option which will apply the permissions or owner recursively, but again, be very careful.

Darryl Hein
How can you do that? - I run unsuccessfully `sudo -i`; vim /etc/crontab`. I add the following to the file `1 * * * * root sudo cp -r /home/masi/Dropbox/codes/ /var/www/`
Masi
How can you change the owner rights of the folder /var/www?
Masi
A: 
sudo chown yourusername /var/www

And you'll become the new owner of /var/www.

erjiang
Each file inside the /var/www/codes is owned by root. Does the following command allow me to copy `sudo chown /var/www/codes/*`?
Masi
`chown -R` for recursive `chown`.
Mark Rushakoff
This is not a good idea and can cause problems with other users and even apache its self.
MitMaro
+5  A: 

Owning /var/www yourself might conflict with other options on your system. On my Debian system I would do this

sudo addgroup www
sudo adduser nr www   # add myself to the www group
sudo chgrp -R www /var/www  # make files in the group
find /var/www -type f -exec chmod g+w '{}' ';'  # make each file group writable
find /var/www -type d -exec chmod g+ws '{}' ';' # make each directory group writable and sticky

If the directory is group writable and sticky, all files created in /var/www will be writable by anyone in the www group, no matter who or what creates them there. (Caveat: they have to be created by "normal" means; cp -a can circumvent the group sticky bit.)

Because Unix is insanely stupid about group membership, for your membership to be honored you will have to log in again, e.g., ssh localhost or log out and log back in. A nuisance.

Norman Ramsey
Although the other answers will work this one is the correct way. +1
MitMaro
I run `cp -r codes /var/www/ unsuccessfully in getting a long list of the following errors `cp: cannot create regular file /var/www/codes/index.php': Permission denied`. This suggests me that something else need to be changed.
Masi