There may be better tools if one is working directly in Linux. Replace id_rsa
with id_ed25519
or your key name.
Create SSH Private/Public Keys for GIT
Update APT and install OpenSSH Client and GIT
sudo apt update && sudo apt install -y openssh-client git
mkdir -p ~/.ssh/<your_git_repo_host>
Where <your_git_repo_host>
can be github.
ssh-keygen -t ed25519 -C 'your@email.com' -f ~/.ssh/<your_git_repo_host>/id_ed25519
or using RSA
if your github server doesn't support EdDSA
:
ssh-keygen -t rsa -b 4096 -C 'your@email.com' -f ~/.ssh/<your_git_repo_host>/id_rsa
Not having a password for the private key is convenient for not having to enter it for prompts, but not as secure.
Get the public key so it can be provided to your git host.
cat ~/.ssh/<your_git_repo_host>/id_ed25519.pub
Open your github URL through a web browser.
Click under your profile at the top right corner.
Click Settings.
Navigate to SSH and GPG keys on the left pane.
Under SSH keys section click the New SSH Key button.
On the SSH keys/Add New page, add a descriptive Title and paste the value of your
id_ed25519.pub
orid_rsa.pub
to the Key textbox and click the Add SSH key button.
Start SSH Agent
Start ssh-agent
since it is the key manager for your private key allowing authentication with the public key you provided to github.
eval $(ssh-agent)
Note, you can kill the agent if you need to with eval "$(ssh-agent -k)"
.
ssh-add ~/.ssh/<your_git_repo_host>/id_ed25519
Github Host Configuration
If keys to the host were not preserved by the line above a config can be created manually:
touch ~/.ssh/config
vim ~/.ssh/config
# Configure to use key based off host.
Host <your_git_domain_name>
IdentityFile ~/.ssh/<your_git_repo_host>/id_ed25519
or
echo "# Configure to use key based off host." > ~/.ssh/config
echo "Host github.com" >> ~/.ssh/config
echo -e '\tIdentityFile ~/.ssh/your_git_repo_host/id_ed25519' >> ~/.ssh/config
If you encounter an issue with WARNING: UNPROTECTED PRIVATE KEY FILE!
and your private key was password protected. It's likely a permission issue.
sudo chmod 600 ~/.ssh/<your_git_repo_host>/id_ed25519
or everything under ~/.ssh
sudo chmod 755 ~/.ssh
You can test your git connection issues with verbose logging to better determine the cause for authentication failures:
ssh -vT git@<your_git_domain_name>.com
SSH Key Password
It's a bit annoying to type in your SSH key password every time you run a git command so you can use keychain
to store it. It will prompt for the password once and cache it. Keychain will automatically start the ssh-agent
.
sudo apt-get install keychain
Update your .bashrc
:
# SSH
#eval `ssh-agent
#eval ``keychain --eval --agents ssh <your_git_repo_host>/id_ed25519
eval $(keychain --eval <your_git_repo_host>/id_ed25519)
Show Git branch in Terminal
Windows Terminal doesn't show the Git branch by default.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "
Pipe command output to clipboard
Works best with pure Linux below due to this issue Install
sudo apt install xclip
Example
cat /etc/os-release xclip -selection c
Set alias in ~/.profile or ~/.bashrc or ~/.bashrc_profile
echo 'alias xclip="xclip -selection c"' >> ~/.profile
WSL Option:
cat /etc/os-release | clip.exe
VS Code and WSL
VS Code for an editor with remoting directly to WSL.
TODO: MOAR!!!
Personal Access Token Alternative
You can also use personal access tokens (PAT) to authenticate with github. If you have only a few repositories you use, this may be an easier option to authenticate to github.
Click under your profile at the top right corner.
Click Settings.
In the left pane select Developer settings.
On the new left pane select Personal access tokens.
Click Generate new token.
Select the appropriate expiration date for the token from the drop-down options.
Select the appropriate level of access.
For just pull/push, I use would select the
repo
checkbox which should automatically select everything under it.
Click Generate token.
- The token will be shown on the redirect page. Make note of it since it won't be shown again.
Note if you're on Windows, there you may just clone normally and it will cache your username and password. This may not be possible with single-sign-on (SSO). If this is the case you can use the token directly when cloning:
git https://your_personal_access_token@your_repo/some_path.git
Notes
WSL2.0 uses ~/.profile
When cloning either select the
https
(PAT) orssh
option from your github repository page depending on the authentication your set up.