The problem is that you're running git clone
under sudo
, which means you're running it under the root
account, not under your own account.
Since you're not running under your own user account, git
doesn't have access to your account's ssh configuration. The git clone
command is using ssh, it's just using it with the root
account's settings, not with yours.
You said in a comment that you're cloning into a subdirectory of the Apache root folder. That folder is owned by the root
account, so of course your account doesn't have permission to write to it.
In general, you shouldn't use the root
account for anything that you can do from a non-root
account, just as a matter of safety. If you can run the git clone
command from your own user account, you have less risk of doing something that breaks your system, and ssh
will use your personal configuration. (Modifying root
's ssh configuration will do the job, but I don't think it's a good idea.)
If you want to avoid running git
commands as root
, you need to do the git clone
into a directory that you have permission to write to.
One solution is, after you're created your directory under the Apache root directory, to use sudo chown
to change the ownership of that directory to your account. You'll then be able to write to that directory, including running git clone
without sudo
.
Or you can use sudo chown
to change just the group ownership of the directory to some group you're a member of -- probably a group created for this purpose. Then use chmod g+w
to grant group write permission. Now you'll be able to write to the directory even though it's still owned by root
.
Or you can just clone into a subdirectory of your home directory, and then use sudo ln -s
to create a symbolic link to your directory under the Apache root directory. (A drawback of that is that it's not obvious, by looking under your home directory, that Apache depends on this subdirectory.)