SSH无密码登陆remote-host方法总结

itisyang 2011-08-27

项目使用的Git服务器是基于SSH的,又倡导小步提交,每天频繁的gitpull代码和gitpush代码,每次都需要输入remote-host的密码很是麻烦,参考了网上的做法实验了一下无密码配置,将具体做法总结如下:

1,在local-host使用ssh-keygen命令生成ssh的公钥和私钥(不要设置密码,一路直接Pressenterkey)

local-user@local-host$ [Note: You are on local-host here]
local-user@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/local-user/.ssh/id_rsa):[Press enter key]
Enter passphrase (empty for no passphrase):[Press enter key]
Enter same passphrase again:[Press enter key]
Your identification has been saved in /home/local-user/.ssh/id_rsa.
Your public key has been saved in /home/local-user/.ssh/id_rsa.pub.
The key fingerprint is:
d3:2a:75:b4:04:44:4c:27:44:54:89:4b:f4:68:13:32 local-user@local-host

2,把生成的公钥id_rsa.pub拷贝到remote-host(任何你要登陆的服务器),可以使用ssh-copy-id,我用的是windows7操作系统,使用的Cygwin,且没有ssh-copy-id这个命令,所以直接把id_rsa.pub文件的内容拷贝到remote-host的/home/remote-user/.ssh/authorized_keys文件末尾:

cat /home/local-user/.ssh/id_rsa.pub | ssh remote-user@remote-host 'cat >>  /home/remote-user/.ssh/authorized_keys'

ssh-copy-id -i /home/local-user/.ssh/id_rsa.pub remote-user@remote-host

如果你的ssh默认端口不是22,需要加-premote-port

cat /home/local-user/.ssh/id_rsa.pub | ssh remote-user@remote-server -p remote-port 'cat >>  /home/remote-user/.ssh/authorized_keys'

3,笔者所用公司电脑装的防火墙把22端口封了,防火墙配置没有权限修改,所以在git服务器的sshd上增加了443端口(这个端口一般不会封,而git服务器上80端口已经提供http服务了)

sudo vi /etc/ssh/sshd_config
Port 443  #增加443端口
sudo reload ssh

使用如下命令查看端口是否已开通

sudo netstat -nlptu | grep :443

开通443端口后修改gitrepositoryconfigfile

git config --local -e
remote.origin.url=ssh://git@remote-host:remote-port/opt/git/project.git

git报错git-upload-pack/git-receive-pack:commandnotfound解决办法:

登陆git服务器

cd /usr/bin
ln -s /wls/devopr/git-ies/bin/git* .

参考:

http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/#more-268

http://hi.baidu.com/thinkinginlamp/blog/item/e74ab051102c5f12367abef6.html

相关推荐