2025-02-24
There are some code that I need version tracking, but are placed in /root or some user-inaccessible directory, so it would be natural to use root when creating, and updating the git repo.
However, to upload your repo to GitHub, you can only use your ssh keys. For the sake of security, I don't want to use root for ssh-ing, so, It would make sense to su to some other users while ssh-ing, here's how I did it.
env HOME=/home/<user> SSH_TTY=$TTY SSH_AUTH_SOCK=/run/user/<user_uid>/ssh-agent.socket git push
<user> is the username that you have ssh access to, and <user_uid> is the UID of that user, usually 1000 for a personal computer.
Breakdown:
HOME: Set the HOME variable to the user's, so that ssh looks for ~/.ssh in that directory.SSH_TTY: Set the TTY to the current one, this is useful when running sudo, because sudo strips that.SSH_AUTH_SOCK: Set this to the user's, so that it uses the ssh-agent for that user. When the user has ssh-agent enabled, you don't need to use password for authentication.