Using Custom SSH Keys with Git Push: How to Deploy Securely with -i
🔐 How to Use -i
(Identity File) with git push
When deploying to a remote server using git push
, you might want to use a custom SSH key instead of the default one. While you can’t pass -i
directly to git push
, you can configure it through SSH in one of the following ways:
🔹 Option 1: Use GIT_SSH_COMMAND
(Simple and Temporary)
Run the following command:
GIT_SSH_COMMAND='ssh -i /path/to/private_key' git push production master
✅ This sets a temporary SSH command that uses your custom key, just for this git push
.
🔹 Option 2: Add a Permanent Entry in ~/.ssh/config
Edit your SSH configuration:
nano ~/.ssh/config
Add the following:
Host your-server-alias
HostName your-server
User sshuser
IdentityFile /path/to/private_key
IdentitiesOnly yes
Then update your Git remote to use the alias:
git remote set-url production ssh://your-server-alias/home/sshuser/repos/myapp.git
✅ Now, every time you run:
git push production master
Git will automatically use the custom identity file defined in the SSH config.
🔐 Make Sure Your Private Key Has Proper Permissions
Run:
chmod 600 /path/to/private_key
⚠️ SSH will refuse to use a private key if it’s too open (e.g., 644
or 777
).
By configuring either of these options, you can securely and flexibly push code using a custom SSH key.
Comments
Post a Comment