How to sudo git clone and safely change permission to another user

16,481
sudo chown -R user:user path

This recursively changes the owner and group of everything under path to user.

Share:
16,481

Related videos on Youtube

Piotr Kula
Author by

Piotr Kula

Updated on September 18, 2022

Comments

  • Piotr Kula
    Piotr Kula over 1 year

    On Raspberry Pi, I log in as the default user Pi.

    I do a sudo git clone on a repository, because it needs to create directories.

    This is specific for DNU/DNX, because we cannot run sudo dnu restore yet, or ever? I don't know. It gets permission denied on the packages file causing it to error and I cant run the sample.

    pi@raspberrypi ~/Home/samples/1.0.0-beta4/HelloMvc $ dnu restore
    Restoring packages for /home/pi/Home/samples/1.0.0-beta4/HelloMvc/project.json
    Writing lock file /home/pi/Home/samples/1.0.0-beta4/HelloMvc/project.lock.json
    ----------
    System.UnauthorizedAccessException: Access to the path "/home/pi/Home/samples/1.0.0-beta4/HelloMvc/project.lock.json" is denied.
    

    I had to do a chmod -R 0777 /Samples but that obviously is not the correct way to fix this.

    How can I safely or easily use sudo git clone but then make everything as if Pi user did the clone, or allow the Pi user to work properly.

    I did try chmod Pi:Pi on the specific packages file but that didn't help. I didn't try it recursively but I don't really know how to do this properly.

    So I did a fresh clone as requested in comments, let see whats going on.

    pi@raspberrypi /home/test $ sudo git clone https://github.com/aspnet/Home.git
    
    pi@raspberrypi /home/test $ stat /home/test/Home
      File: `/home/test/Home'
      Size: 4096            Blocks: 8          IO Block: 4096   directory
    Device: b302h/45826d    Inode: 153225      Links: 4
    Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2015-06-23 20:42:42.819728005 +0000
    Modify: 2015-06-23 20:42:48.649688508 +0000
    Change: 2015-06-23 20:42:48.649688508 +0000
     Birth: -
    
    pi@raspberrypi /home/test $ stat /home/test
      File: `/home/test'
      Size: 4096            Blocks: 8          IO Block: 4096   directory
    Device: b302h/45826d    Inode: 153224      Links: 3
    Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2015-06-23 20:42:31.099806322 +0000
    Modify: 2015-06-23 20:42:42.819728005 +0000
    Change: 2015-06-23 20:42:42.819728005 +0000
     Birth: -
    
    pi@raspberrypi /home/test $ stat /home/pi
      File: `/home/pi'
      Size: 4096            Blocks: 8          IO Block: 4096   directory
    Device: b302h/45826d    Inode: 29          Links: 7
    Access: (0755/drwxr-xr-x)  Uid: ( 1000/      pi)   Gid: ( 1000/      pi)
    Access: 2015-02-16 15:09:21.453774622 +0000
    Modify: 2015-06-23 19:33:46.613016792 +0000
    Change: 2015-06-23 19:33:46.613016792 +0000
     Birth: -
    
    • muru
      muru almost 9 years
      Why not give Pi write permissions to the directory you want to clone in?
    • Piotr Kula
      Piotr Kula almost 9 years
      I don't know..? I don't know how to do any of this to be honest. I am happy I figured out what the problem was and found a work around.. but how do I do this properly? I am only going to be using Pi user. I have very little understanding of how chmod and chown work together but everywhere I read is not to screw it up because of security..
    • muru
      muru almost 9 years
      OK, for a start, post the current permissions and ownership of the directory you do git clone in. Say it is /foo/bar, then do stat /foo/bar. And add the groups of Pi: groups Pi. Edit your post to add more information.
    • muru
      muru almost 9 years
      You'll probably want to use a solution like in this question: askubuntu.com/questions/46331/…, and the information requested will tell you how to modify the steps there.
    • muru
      muru almost 9 years
      Ah, no, I meant the ownership of /home/test in this case - the directory containing the git repo, not the git repo itself.
    • Piotr Kula
      Piotr Kula almost 9 years
      I have to use sudo mkdir to create directories too. so its still root. Actually I have to do almost everything with sudo
    • muru
      muru almost 9 years
      Is test an actual user? Or is that just a representative path?
    • Piotr Kula
      Piotr Kula almost 9 years
      that was just me creating a direcoty quickly, was meant to be home/pi i did a stat of the original user /home/pi dir
    • muru
      muru almost 9 years
      If you're working in /home/pi, you don't need sudo at all. Where are you working in?
    • Piotr Kula
      Piotr Kula almost 9 years
      I am actually cloning the original files in /var/usr/source then running the sample there so I can proxy to it using nginx. I am not actually cloning anything in to the pi directory. Will it be easier if I actually clone into the /home/pi directory instead? Will the permissions then cascade across everything there?
    • muru
      muru almost 9 years
      That depends on what you're doing. Are you following the steps here: github.com/aspnet/Home/blob/dev/GettingStartedDeb.md?
    • Piotr Kula
      Piotr Kula almost 9 years
      Yea those step are way before and all done. I am actually cloning the /Home repo. I just did a sudo clone into /var/pi, it created a Home directory, I went in there to the sample I want and did a dnu restore, I am getting the same problem. accees to lock.json file denied. Even though the sample in under /home/pi - This problem never used to exist (like last month) but you know, its beta, then changing things, fixing things, making more secure etc. Besides those help files are way out of date. It tooke me ages to work out how to do that properly from other sources
    • Piotr Kula
      Piotr Kula almost 9 years
      Thanks for trying to help muru - Linux permissions is black magic to me. Just trying to at least do it as correct as possible and try to learn what all this means. Thanks again.
  • rkeatin3
    rkeatin3 almost 9 years
    Specify the username you want to change ownership to before specifying the directory: sudo chown -R pi /home/pi/Home
  • Piotr Kula
    Piotr Kula almost 9 years
    Oh yes, offcourse, I missed that. OK I did that... and.............. it worked. :)
  • Piotr Kula
    Piotr Kula almost 9 years
    I did a stat on the direcotry and now its UID: pi and GID root but the home direcoty is UID: pi and GID: pi what are the implications of this?
  • rkeatin3
    rkeatin3 almost 9 years
    Not sure. I'm a bit new to Linux myself.
  • Piotr Kula
    Piotr Kula almost 9 years
    Well, I suppose this is much better than doing a chmod 0777 on the entire directory. Thanks!
  • Olathe
    Olathe almost 9 years
    You'll want to do sudo chown -R user:user path to change the group as well.