Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

sudo chown yourusername /var/www

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top