您的位置:首页 > 运维架构 > Shell

linux shell 无密码登陆

2014-02-25 22:03 239 查看
假如你Linux Client是客户端, Server为服务器,用户名为user。

现在要配置从Client到Server的无密码SSH登录。

1:在Client上产生一对密钥,执行ssh-keygen命令,

需要输入的地方直接回车,接受缺省值即可,输出如下:

[user@Client .ssh]$ ssh-keygen -d

Generating public/private dsa key pair.

Enter file in which to save the key (/home/user/.ssh/id_dsa): 

Enter passphrase (empty for no passphrase): 

Enter same passphrase again: 

Your identification has been saved in /home/user/.ssh/id_dsa.

Your public key has been saved in /home/user/.ssh/id_dsa.pub.

The key fingerprint is:

22:0c:2e:64:09:2e:a9:f1:37:c5:ee:d9:e5:57:92:b4 user@Client

这时候,在/home/user/.ssh目录下,存有一对密钥id_dsa和id_dsa.pub。

2:当公钥id_dsa.pub以任何方式上传到Server上,如:

[user@Client .ssh]$ scp id_dsa.pub Server:/home/user

3:登录到Server上,执行以下命令

cat id_dsa.pub >> /home/user/.ssh/authorized_keys

这样就完成了配置,此时从Client登录Server,就无需输入密码了。

这种方式是安全的,你完全不用担心别人从其它机器上也可以无密码登录Server。

这个基本原理是这样:

你在client上产生的id_dsa和id_dsa.pub是一对密钥,只有用私钥id_dsa才能解开公钥id_dsa.pub。

现在你把公钥存在服务器上,

你登录服务器的时候,服务器会给提供经过公钥id_dsa.pub(其内容存在authorized_keys里)加密的数据让你解密,

你的机器上用id_dsa这把私钥,去解密,解开之后Server放行。

而别人的机器上没有id_dsa这个私钥,自然无法解密,从而无法无密码登录了。

当然,你要保证你机器上的私钥的安全。

否则,如果别人取得了你的私钥,就好比别人取得了你房间的钥匙,然后就可以拿去开你家房门了

方法二:

使用下例中ssky-keygen和ssh-copy-id,就无需输入密码了。

仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机。 

ssh-keygen 创建公钥和密钥。 

ssh-copy-id 把本地主机的公钥复制到远程主机的authorized_keys文件上。

ssh-copy-id 也会给远程主机的用户主目录(home)和~/.ssh, 和~/.ssh/authorized_keys设置合适的权限 。

步骤1: 用 ssh-key-gen 在本地主机上创建公钥和密钥

ligh@local-host$ ssh-keygen -t  rsa

Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key] 

Enter passphrase (empty for no passphrase): [Press enter key]

Enter same passphrase again: [Pess enter key]

Your identification has been saved in /home/jsmith/.ssh/id_rsa.

Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub. 

The key fingerprint is: 33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 

ligh@local-host

步骤2: 用 ssh-copy-id 把公钥复制到远程主机上

ligh@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub  root@192.168.0.3

ligh@remote-host‘s password:

Now try logging into the machine, with ―ssh ?remote-host‘‖, and check in: 

.ssh/authorized_keys to make sure we haven‘t added extra keys that you weren‘t expecting.

[注: ssh-copy-id 把密钥追加到远程主机的 .ssh/authorized_key 上.]

步骤3: 直接登录远程主机

ligh@local-host$ ssh remote-host 

Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 

[注: SSH 不会询问密码.] 

ligh@remote-host$ 

[注: 你现在已经登录到了远程主机上]

方法三:

方法二、安装sshpass
# sudo apt-get install sshpass
安装完成后使用sshpass允许你用 -p 参数指定明文密码,然后直接登录远程服务器。例如:

# sshpass -p '你的密码' ssh 用户名@服务器ip地址

用 '-p' 指定了密码后,还需要在后面跟上标准的 ssh 连接命令。

sshpass: 用于非交互的ssh 密码验证

 
ssh登陆不能在命令行中指定密码,也不能以shell中随处可见的,sshpass 的出现,解决了这一问题。
它允许你用 -p 参数指定明文密码,然后直接登录远程服务器。 
它支持密码从命令行,文件,环境变量中读取
 
$> sshpass -h
 
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
   -f filename Take password to use from file
   -d number Use number as file descriptor for getting password
   -p password Provide password as argument (security unwise)
   -e Password is passed as env-var "SSHPASS"
   With no parameters - password will be taken from stdin
 
   -h Show help (this screen)
   -V Print version information
At most one of -f, -d, -p or -e should be used
 
sshpass [-f|-d|-p|-e] [-hV] command parameters 中的 command parameters 和使用交互式密码验证的使用方法相同
 
#从命令行方式传递密码
 
    $> sshpass -p user_password ssh user_name@192.168..1.2
    $> sshpass -p user_password scp -P22 192.168.1.2:/home/test/t . 
 
#从文件读取密码
 
    $> echo "user_password" > user.passwd
    $> sshpass -f user.passwd ssh user_name@192.168..1.2
 
#从环境变量获取密码
 
    $> export SSHPASS="user_password"
    $> sshpass -e ssh user_name@192.168..1.2 
 
 
源码位置:http://sourceforge.net/projects/sshpass/
 

--------------end--------------

From: GS

-------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: