Use a git repository on /var/www/html/ - linux

I am making a web tool and hosting this project on Github. I want to create a repository on my machine (running linux) being able to easily test it on local.
I can test it without problems on /var/www/html (already have apache and php set up), but I am having trouble creating a repository there. However, if I try to create the repo in ~/Documents/Github/PROJECT_NAME it works perfectly; but I can't test my project from there.
How can I create a repo inside /var/www/html where I can put my project files and run them locally without problems?
I tried to run sudo git init then sudo git clone git#github.com:xxx/xxx.git (that is how I clone my repo on ~/Documents/Github/PROJECT_NAME, so I have already exchanged SSH keys with Github) but it didn't work:
Cloning into 'PrerequisiteVisualizer'...
Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
EDIT: I am able to run without problems
mkdir ~/Desktop/TESTING
cd ~/Desktop/TESTING
git init
git clone git#github....
My question is similar to Attempting to use symbolic link for var/www/html but it still is different.
EDIT2: I think I need to clarify why the thread I cited isnt what I am looking for. I saw the solution proposed there but note that, as long as I understood the other thread, they created a directory in his home (~) and made it accessible locally using the per-user web directories. But this is not what I want to do. I just want to "create a repo inside /var/www/html", not in anywhere else.

The problem isn't strictly with /var/www/html, it's with sudo. If you use sudo to do git, you are running it as a different user, which doesn't have access to your private ssh credentials (nor should it have).
In the other thread you pointed to there's an explanation of per user www directories, which should be one way of solving your problem. If it doesn't, you could amend the question with reasoning why it doesn't.
Update: based on the discussion, you want all content within /var/www/html owned by the user operating git repository. That you should be able to do in the way proposed by #rogerovo in a comment to this answer:
sudo chown -R _currentuser_:www-data /var/www/html && chmod -R g+sw /var/www/html

Permissions for /var/www/html folder needs to be changed.
Kindly run this command sudo chmod o+w /var/www/html to give write access to everyone.
Once run, you should be able to transfer files in /var/www/html folder.

Related

fatal: Not a git repository (or any of the parent directories) for user other than root

when I login with root user and go to the working directory (/home/gitproject), I have no issue running commands but when I login to the server with other users I get the following:
fatal: not a git repository (or any of the parent directories): .git
How to resolve this issue?
the repo is not owned by root it is owner by another user and even when I use that user to login I get the same error!
It's hard to know exactly what's wrong here, but here are some advice on how to debug this. git does not find a the structure for a repository where you are, which means that it is unable to find either /home/gitproject/.git or /home/.git or /.git.
This may be because the files are missing or because only root can read them. Check with the users which do not work using the following command:
ls -l /home/gitproject/.git
(and so on).
On a side note, as larks mentioned in a comment, sharing a git repository is going to be a huge pain. Try gitolite or other projects of this kind.
so I solved the issue ...
with root user I ran this command
rm -r .git
and logged in with another user and ran this:
git init
git remote set-url origin URL
It solved the issue. But still not clear what really happened there.

Need help cloning a GitHub repository because of public key issues

I am trying to clone a private GitHub repository in a server that I am trying to get up and running on a virtual machine.
No matter what I do, I keep getting the following error:
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I have tried a bunch of things. For starters, I made sure that I made my rsa keys correctly by following GitHub's tutorial. I am also sure that my public rsa key found at ~/.ssh/id_rsa.pub was correctly copied over to my GitHub account's ssh keys.
My config file at ~/.ssh/config looks like the following:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
I have tried cloning a public repository and a private repository, and both don't work.
When I go to clone the repository I am executing the following command:
sudo git clone git#github.mit.edu:<GitHub Name>/<Repo Name>.git
If I execute the command without sudo, I get the following error:
fatal: could not create work tree dir '<Repo Name>'.: Permission denied
I am inside of a Python virtual environment when I do all of this. Does that make any difference?
I have been reading Stack Overflow posts related to this matter, but none of them are helping me address my issue.
Any help would be greatly appreciated! Thank you!
You are using sudo so Git is using ssh keys of user root.
To make this work you'd have to call git without sudo.
So when I don't use sudo I get the error fatal: could not create work
tree dir 'cardlearning'.: Permission denied
Make sure current user has write permission in directory you are executing the clone.
You can't clone into existing directory so as a workaround you can:
$ sudo mkdir tmp
$ sudo chown $USER: tmp
$ git clone git#github.mit.edu:<GitHub Name>/<Repo Name>.git tmp/cardlearning
$ mv tmp/cardlearning ./cardlearning
$ rmdir tmp
Check that your user has permissions to write to the directory you are in.
or execute
git clone git#github.mit.edu:<GitHub Name>/<Repo Name>.git ~/myRepo
to place it in the myRepo subdirectory of your home directory.
Do not use sudo to edit your ssh config when using a ~ referenced path, as you may end up editing the root user's ssh preferences and not your users. Likewise do not clone repositories using sudo as it will attempt to connect using the root user's ssh key.

Should we use sudo for git operations?

What is the difference between the following two command lines?
root#superhero:~/Workspace/# sudo git push origin master
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
root#superhero:~/Workspace/# git push origin master
Everything up-to-date
Using sudo causes the command to run as the root user. The Git command uses credentials and configuration stored in the current user's home directory; when you run as sudo, this code is going to be looking at the root home directory, not your home directory and thus miss this context.
In most cases, it should not be necessary to use sudo. There are very few cases where it is required (such as installing software globally such as with apt-get) and when it comes to programming, use of sudo is often but not always an anti-pattern (in order to ensure that source code is hermetic and reproducable, most software should be installed in the repository, not globally).
When you are using sudo in your command. This is the root user that execute the command. The key used to access to your git server are store in a directory (.ssh/). When you run the command in root. The directory is the '.ssh/' of root so not the good one.
Another point, with sudo, this is a user from sudo group that execute the command. So the sudo group does not have access to your.ssh

"Unable to create home directory" error when changing JENKINS_HOME

Jenkins was running all fine on a RedHat Linux machine (a clean EC2 machine on AWS), until I decided to change the JENKINS_HOME. I simply moved the Jenkins directory from /var/lib/jenkins to /home/ec2-user/jenkins and then created a symlink. (I followed the first answer to this question: Change JENKINS_HOME on Red Hat Linux?).
However when I restart Jenkins I get the error:
Unable to create the home directory ‘/var/lib/jenkins’. This is most
likely a permission problem. To change the home directory, use
JENKINS_HOME environment variable or set the JENKINS_HOME system
property.
I tried changing JENKINS_HOME in /etc/sysconfig/jenkins, setting it to the new folder (which I suppose defeats the point of a symlink?) and I still get the same error
Unable to create the home directory ‘/home/ec2-user/jenkins’.
It is for backup purposes, so that I have all Jenkins data in a mounted external data storage (AWS Elastic File System).
I've figured it out. This error was persisting because the /jenkins/ folder needs to be accessible to user 'jenkins' to run processes, but it couldn't access this folder because it is belongs to the particular logged in user. I changed the mounting to /var/ where jenkins can access as global process, and it solved the problem.
I ran into the same problem, so sharing my solution here:
The user jenkins does not have access to the folder home/ec2-user/jenkins. You can modify the access rights of the folder home/ec2-user/home by changing or adding the user jenkins to owner
sudo chown jenkins /home/ec2-user/jenkins
sudo chmod u+w /home/ec2-user/jenkins
To verify the new ownership, you can do:
ls -ld /home/ec2-user/jenkins
The error seems pretty obvious: "This is most likely a permission problem."
I assume /home/jenkins does not exists, and the user jenkins does not have write permissions in /home. If you moved the Jenkins home, then you probably did it as root and just forgot to update owner permissions.
You would need to create the home, something like this:
sudo service jenkins stop
# make the changes in /etc/sysconfig/jenkins
sudo mkdir --parents /home/jenkins # or mv, in your case
sudo chown --recursive jenkins /home/jenkins
sudo service jenkins start

make git clone with sudo

when I make git clone with ssh from a user prompt it works properly.
git clone ssh://URL.com/soft.git soft_git
the ssh key id_rsa and id_rsa.pub are under /home/user/.ssh
my purpose is the execute git with sudo but I got the following error
Cloning into '/home/user/git/soft'...
Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I create a folder /root/.ssh and I copy the ssh keys into it but I got the same error
how to execute git with sudo properly.
When you run git using sudo, git will run as root. Because git is running as root, ssh is running as root. Because ssh is running as root, it is trying to log on to the remote server as root. The remote server is not okay with this (as it should be!)
You will need to do two things:
Put the username in your URL: ssh://myusername#URL.com/soft.git.
Make your SSH key available to the root user, because it will look under /root/.ssh instead of /home/user/.ssh. (You could also probably point SSH at the correct key, but I don't know how to do this, and SSH is picky about permissions.)
On my computer (Ubunutu 18.04), adding SSH_AUTH_SOCK=$SSH_AUTH_SOCK after sudo and before git fixed the problem:
sudo SSH_AUTH_SOCK=$SSH_AUTH_SOCK git clone git#github.com:my-github-account/my-repo.git
Normally, sudo's SSH_AUTH_SOCK environment variable won't be set properly. Executing the git clone with SSH_AUTH_SOCK=$SSH_AUTH_SOCK sets sudo's SSH_AUTH_SOCK environment variable to whatever it is for you.
This way, you don't need to add an extra .ssh dir for sudo with copies of your keys, which is what I think one of the other answers suggests.
The solution is more fully explained in this rather old github gist:
https://gist.github.com/scottjacobsen/4281310
P.S. I'm adding a new answer several years later; I googled a solution to this problem, and this SO Q/A is one of the first things that comes up.
Normally the default remote ssh user is the same as your user name. If you're using sudo this will be root which probably isnt' going to work. You need to supply the remote username.
sudo git clone ssh://username#URL.com/soft.git soft_git
You can generally resolve git ssh issues easier by trying to login to the remote with plain ssh. You'll get better diagnostics and can see what's going wrong.
sudo ssh ssh://URL.com/

Resources