To copy files each minute at /var/www without sudo

雨燕双飞 提交于 2019-12-08 10:17:45

问题


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.


回答1:


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.




回答2:


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.




回答3:


sudo chown yourusername /var/www

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



来源:https://stackoverflow.com/questions/1250231/to-copy-files-each-minute-at-var-www-without-sudo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!