How can I add an already generated SSH key to git bash? - bash

I have an SSH key saved in D:/keys folder. I want to add it to my git bash. All the tutorials I found is how to generate SSH key using gitbash and load it to github/gitlab. I generated my SSH key using puttygen. Now I want to add it to my git bash so that I can clone a repository from remote. How can I do that?

On windows you might need to start the ssh agent like this
# start the ssh-agent in the background
$ eval $(ssh-agent -s)
> Agent pid 59566
Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
$ ssh-add <path/to/key>
Got this information from here under "Adding your SSH key to the ssh-agent":
https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#adding-your-ssh-key-to-the-ssh-agent

I don't think there is any specific config in gitbash itself. You have to put the key in the default location ~\.ssh/id_rsa and it will be used. If you need to have it somewhere else you could do so with a config file same as on Linux ~/.ssh/config
host example.com
HostName example.com
IdentityFile ~/.ssh/id_rsa
User git
Don't forget to set the permissions chmod 400 ~/.ssh/id_rsa

Assume the private key file you want to import to git bash is D:/keys folder/myprivatekey and your Git was installed in D:/Git (in which folder you would see the binary file git-bash.exe), open the file D:/Git/etc/ssh/ssh_config.
Here are some texts in this file:
...
# StrictHostKeyChecking ask
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22
...
Simply add a new line and save it:
...
# StrictHostKeyChecking ask
IdentityFile "D:/keys folder/myprivatekey"
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22
...
And the key is already added.

I was able to get it so the passphrase is only prompted for on the first window that's opened after booting using the script at Auto-launching ssh-agent on Git for Windows. I did find, however, it didn't work when I added it add it to either ~/.profile or ~/.bashrc. I needed to add it to ~/.bash_profile for it to get picked up and used by Git Bash on Windows.

Related

How to use the GitBash to clone data with ssh (windows 10 environment)

What can I solve this problem?
when I wanted to connect my personal GitLab account, I got an error message like the following picture
### shell script
git clone git#gitlab.com:<username>/test1.git
GitLab server-side
Clone ssh address
PS: I have checked and found some reference from StackOverflow but unfortunately it isn't okay for me
1.git clone through ssh
2.SSH and Git Clone
3.git clone with ssh issue
I have found a great reference but sorry for Mandarin website, however, I can use my way to share how to deal with this issue.
Step 1:
ls -al ~/.ssh
Step 2:
ssh-keygen
(using enter key for default value)
Step 3: To setup config file
vim /c/Users/Willie/.ssh/config
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa
Step 4:
git clone git#gitlab.com:<username>/test2.git
Step 5:
When you finished Step 4
1.the test2.git file will be download done
2.you will get the new file(known_hosts) in the ~/.ssh
PS: I create the id_rsa and id_rsa.ub by myself and I deliver it to the Gitlab server. using both keys to any client-sides(windows and Linux).
Check first if you do have a ~/.ssh/id_rsa private key/~/.ssh/id_rsa.pub public key.
If so, check your private key: if it has 70 chars per line, try and regenerate with the old PEM format:
ssh-keygen -m PEM -t rsa -P "" -f ~/.ssh/id_rsa
(That will override your current key, but if said current key is not working anyway, that should be OK).
Update the public key on GitLab side, and try again, with a simple:
ssh -T git#gitlab.com
Using a config file means not using the user and using a shorter name:
Host gitlab
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa
means: ssh -T gitlab or git clone gitlab:<username>/test2.git will work.

ssh specify default directory to check

Instead of ssh "~/.ssh/somekey.pem" ubuntu#somehost, is there a way to make ssh auto check the ~/.ssh directory for keys so that I can simply do ssh "somekey.pem" ubuntu#somehost (i.e. omitting ~/.ssh)?
If you're using the same host then try updating your ~/.ssh/config file with the host info
Host dev
IdentityFile ~/.ssh/github.key
HostName dev.example.com
Port 22000
User fooey
then just type ssh dev to ssh in!

Hosts declared in ssh configuration on Windows are not visible

I am using Git on Windows with Git Bash.
In C:\Users\myuser\.ssh I created a config file, where I declared a host
Host my-host along with some configuration.
The config file has the following content:
Host my-host
User my-username
Hostname my-repo.com
Port 7999
IdentityFile id_rsa
In theory, I should be able to connect to this host from the command line:
ssh my-host.
However, that doesn't happen, if try to run this command from Git Bash, I get:
ssh: Could not resolve hostname my-host: Name or service not known
So how can I make this host visible in Git Bash or in Windows Command Line?
Check port number, 7999 is not usual for ssh
set full path for id_rsa, like ~/.ssh/id_rsa
maybe you should use alias name without "-"
check connection with your parameters without alias, like "ssh -i~/.ssh/id_rsa -p7999 user#host"
check you id_rsa, is it private key?
check rights on folder and file: ~/.ssh - 700, ~/.ssh/config - 600

puphpet how to configura to automatically clone a repository?

I'm starting to use puphpet, I want to use it to automatically clone a repository on provision but I'm not able to find a way, is this possible?
One posibility that I found is create a .sh script on puphpet/files/exec-once-unprivileged/
The content of the script could be something like:
cd /vagrant
# Add github.com into known hosts to avoid interactive question
ssh -T git#github.com -o StrictHostKeyChecking=no
git clone ssh://git#github.com/repository
If you want to clone a private repository you can use ssh-agent to use the ssh keys of the host machine in the VM. To do this do on the host machine:
ssh-add ~/.ssh/id_rsa

Install gitolite v3 on the Centos 6.4. The second user is asking a password

Please help me to understand what i am doing wrong. I have server on CentOS 6.4 and I need to install gitolite v3. What am i doing? by the first i am install one using next command:
yum install git* perl-Time-HiRes
useradd -m --system --shell /bin/bash --user-group gitolite
su - gitolite
mkdir bin
echo "PATH=$HOME/bin:$PATH" > .bash_profile
source .bash_profile
ssh-keygen -t rsa
cp .ssh/id_rsa.pub ./gitolite.pub
git clone git://github.com/sitaramc/gitolite
gitolite/install -ln
gitolite setup -pk gitolite.pub
Install was successful. Than I clone gitolite-admin repositories:
git clone gitadmin:gitolite-admin
cd gitolite-admin/keydir
And generate public key for the new user(lodar.pub)
ssh-keygen -t rsa -f lodar
mv ./lodar ../../.ssh/lodar
Than i edit conf/gitolite.conf:
repo gitolite-admin
RW+ = gitolite
repo testing
RW+ = #all
repo empty
RW+ = lodar
Commit all changes:
git add keydir/lodar.pub
git commit -m 'add user lodar and new repo empty'
git push
And that is all. Push was successful too. After all manipulation i created .ssh/config
Host gitadmin
User gitolite
Hostname 192.168.0.1
Port 22
IdentityFile ~/.ssh/gitolite
Host gitlodar
User gitolite
Hostname 192.168.0.1
Port 22
IdentityFile ~/.ssh/lodar
But if i try to connect using
ssh gitlodar info
lodar#192.168.0.1's password:
i must to enter the password.
ssh gitadmin info
hello, gitolite, this is gitolite#lodar-14452 running gitolite3 v3.5.1-4-g2f48a3e on git 1.7.1
R W gitolite-amdin
R W testing
What am i doing wrong?
You did:
mv ./lodar ../../.ssh/lodar
I supposed you meant
cp ./lodar.pub ../../.ssh/
mv ./lodar ../../.ssh/
Because if you don't copy the public key in ~/.ssh, you wouldn't be able to use it in an ssh session.
But the other mistake is in your ~/.ssh/config file:
Host gitlodar
User gitolite # not loadar!
Hostname 192.168.0.1
Port 22
IdentityFile ~/.ssh/lodar
See "How do programs like gitolite work?":
The idea is to always use the same user to connect to the gitolite server (in your case, connect as 'gitolite'), and open an *non-interactive shell.
The force-command ssh line will call the gitolite script with a parameter representing the name of the user (here 'loadar'), because that named is registered with the public key in the ~gitolite/.ssh/authorized_keys.
$pwd
/home/lodar/gitolite-admin/keydir
$ssh-keygen -t rsa -f lodar
Last command generated two keys: public (lodar.pub) and private (lodar). I moved private key in the .ssh on my client computer. The public one stayed in keydir.
$mv ./lodar ../../.ssh/lodar
That is why after 'git push' user lodar added to giotlite repo. I checked it on the server by the command
$gitolite sshkeys-lint
sshkeys-lint: === checking authkeys file:
sshkeys-lint: === checking pubkeys:
sshkeys-lint: === gitolite.pub maps to user gitolite
sshkeys-lint: === lodar.pub maps to user lodar
YOU ARE RIGHT. MY MISTAKE was IN MY .ssh/config. Thanks for the HELP. it is working great now.

Resources