14

I created a directory /var/www to store my web apps clone in. When I initially cloned the app from GitHub it required me to use sudo and every time I do a git pull it's requiring sudo. I'm running into some problems because of this. For example, my ssh keys are not matching up. So when I do my git pull, I'm having to use HTTPS instead of ssh and manually enter my username and password every time I want to pull and update my app. How do I configure this so I don't have to use sudo every time I use git?

3 Answers 3

16

A couple of things are going on here:

  • When you sudo git checkout ..., all those files are owned by the root user and root group. With the standard permissions, that's why subsequent alterations to those files require you be root.

  • /var/www/ by default is owned by www-data group. Something your user is not by default.

The easiest way to write in /var/www/ is to just add your user to the www-data group. You can of course, change the directory to be owned by your user but this can have some nasty knock-on effects if you're not pre-empting them.

You'll need to re-log in after adding your user to the www-data group.

In your case specifically, you're going to need to fix your current mess of root-owned data. You can either delete it as root (and re-checkout) but if you have unsaved work, it'll just be cleaner to pull everything back to your user. The following example is extremely lazy and assumes what we're talking about is the only thing in /var/www/:

sudo chown -R www-data: /var/www/
5
  • Ok, would it be better to just sudo git checkout into another directory, and then deploy my app in the /var/www directory? I just read somewhere that it's not generally not a good idea to checkout projects into the var directory to begin with. One person recommended checking out projects into the home directory
    – Scott
    Sep 9, 2013 at 16:16
  • 3
    It would be best if you stopped running git as root. Aim for that. Any plan that leaves you running it as root has already failed. What you're saying about checking out into a webdirectory is correct - giving people access to your .git directory can give them access to things you don't want to let people access, but you can prevent access to the .git/ dir which mitigates the whole issue.
    – Oli
    Sep 9, 2013 at 16:17
  • There are also other acceptable directory structures where you have a holding directory for your website where non-hosted stuff goes (including your .git) and inside that there's a public/ directory which you tell Apache to host. The ethos of keeping one website in /var/www/ is very archaic.
    – Oli
    Sep 9, 2013 at 16:24
  • Do you have a recommended directory structure? Basically, I have my checkout, and then I have to bundle that into a .tgz. And then I have to untar that into a node application. It's a meteor application by the way. I'm kind of confused where to keep these three separate folders/files
    – Scott
    Sep 9, 2013 at 16:49
  • @Scott I ran into this same issue. Subsequent subcommands in the repo (in my case) will call git and try to clone other repos. The solution, to re-align SSH keys as you stated, was to start the process without using sudo. A lot of this Q&A and thread discussion is a work-around that will probably have you hitting walls down the road. Aug 31, 2019 at 15:03
0

Hm, change folder owner? I moved my www folder in /home/username, you can change it's location in /etc/apache2/sites-enabled/000-default

2
  • So it has to do with the directory that I'm storing my clone in?
    – Scott
    Sep 9, 2013 at 16:10
  • Yes, exactly. By default /var/www can't be written. Sep 9, 2013 at 16:12
0

I'm a bit late with this answer, but I found that to avoid entering password each time I had to change the repo from https to ssh.

From Github.com help section:

The git remote set-url command changes an existing remote repository URL.

Open Terminal.

Change the current working directory to your local project.

List your existing remotes in order to get the name of the remote you want to change.

git remote -v
origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY.git (push)

Change your remote's URL from HTTPS to SSH with the git remote set-url command.

git remote set-url origin [email protected]:USERNAME/REPOSITORY.git

Verify that the remote URL has changed.

git remote -v

# Verify new remote URL

origin  [email protected]:USERNAME/REPOSITORY.git (fetch)
origin  [email protected]:USERNAME/REPOSITORY.git (push)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .