How to use git as root with your SSH keys and Git name and email.

Submitted by luxian on Sun, 01/14/2018 - 01:56

This is definitely not best practice, but on some servers I have some server configuration in Git and I deploy and update them manually. Git is used only as versioning system for configuration and not automatic provisioning.

In theory things should be pretty straight forward, but there are two main issue with this approach, both caused by the fact that you have to use git  after you switch to root account (sudo su). 

  1. you loose you ssh key chain and you can not pull/push changes
  2. if you commit changes, your Git name and email are empty and using git config it's not OK since multiple users might do the same thing

It's possible to fix this using sudo by using something like this:

sudo -Es GIT_AUTHOR_NAME=Your\ Name GIT_COMMITTER_NAME=Your\ Name GIT_COMMITTER_EMAIL=your.email@example.com GIT_AUTHOR_EMAIL=your.email@example.com su

I added an alias for this and whenever you use the alias, you will forward your ssh key chain and set Git username and password by setting temporary environment variables. It's multi user friendly, so multiple admins can use this to do changes as root and commits author name and ssh keys will be set for each individual.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.