Jul 05 2010

OpenSSH公钥授权方式

Category: Uncategorizedwuxicn @ 7:49 PM

用公钥授权(Public key Authentication)的方式可以实现ssh服务器与本机之间的配对,实现不用输入密码联入SSH服务器。

具体做法:

1) 在本机(ssh客户机)生成密钥文件:

client$ cd ~/.ssh
client$ ssh-keygen -q -f id_rsa -t rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

在提示输入密码阶段时(Enter passphrase…),直接Enter,这样以后在本机就不用输入密码了。
这样,在.ssh/目录下就会生产2个文件:id_rsaid_rsa.pub .
注意:保密好这两个文件!

2) 分发公钥文件:

将你的公钥文件 (id_rsa.pub) 分发到你的SSH服务器上:

# Step 1: 将你的id_rsa.pub文件上传到服务器上
client$ scp ~/.ssh/id_rsa.pub user@your.ssh.server:~/.ssh/

# Step 2: 在服务器上安装公钥文件
server$ chmod 700 ~/.ssh
server$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
server$ chmod 600 ~/.ssh/authorized_keys
server$ rm ~/.ssh/id_rsa.pub

注意:这个地方要用>>追加到authorized_keys文件末尾,以免冲到以前的一些公钥授权。

好了,现在大功告成了,enjoy!
PS: 以上方法只保证在OpenSSH服务器上有效,其他SSH服务器不知道。

Tags: ,


Apr 21 2010

使用SSH隧道翻墙!

Category: Uncategorizedwuxicn @ 4:38 PM
使用SSH隧道翻墙你必须有一个国外可以通过SSH登录的空间。

做法很简单(感谢Sprayfly):
1. 将下面内容存成文件tunnel.sh:

#!/bin/bash
# --------------------------------------
#
#     Title: SSH HTTP Proxy Script
#    Author: Jonathan Lumb
#     Email: jonolumb (at) gmail (dot) com
#  Homepage: http://sprayfly.com
#      File: tunnel.sh
#   Created: July 05, 2009
#
#   Purpose: Establishes/closes a secure HTTP proxy
#
# --------------------------------------
########### Setup SSH Proxy #############
# You will need to have your server setup to run with public and private keys
export SSH_HOST=username@host
############ End of Setup #############

if [ ! -f /tmp/.tunnel ]
then
    echo "创建 SSH 隧道"
    ssh -f -D 9999 $SSH_HOST "if [ -f ~/.tunnel ]; then rm ~/.tunnel; fi; while [ ! -f ~/.tunnel ]; do echo > /dev/null; done" &
    touch /tmp/.tunnel
else
    echo "关闭 SSH 隧道"
    ssh $SSH_HOST "touch ~/.tunnel"
    rm /tmp/.tunnel
fi

exit

然后给这个文件增加执行权限:chmod +x tunnel.sh

2. 建立SSH的RSA授权文件:ssh-keygen -t rsa注意:密码留空。
这样在本地的 ~/.ssh/ 下就会产生两个文件 id_rsaid_rsa.pub,将这两个文件的访问权限设置为0700:chmod 0700 id_rsa id_rsa.pub
然后将密钥授权文件id_rsa.pub文件传到远程的~/.ssh/ 下,命名为:authorized_keys,操作:
scp id_rsa.pub username@host.com:~/.ssh/authorized_keys

3. 做完上面两步后就可以开启你的SSH tunnel了:
开启/关闭SSH隧道:./tunnel.sh
然后在浏览器中设置socks代理(http代理必须留空):127.0.0.1端口:9999

补充说明一点:要关闭ssh隧道还可以在远程的 $HOME 目录下建立一个 .tunnel 文件,这样本地ssh隧道就会自动关闭了,建立方法:touch .tunnel

Tags: , , ,