免输入密码登录服务器主要应用在自动脚本上,不需要手工输入密码,自动就通过身份验证。
有两个方式可以实现,其一是用密钥验证,其二是自动输入密码。
第一个比较安全,简单几步就可以了:
1 生成自己到公钥和私钥
2 把公钥加入服务器端用户到authorized_keys
我们先在服务器上开通一个新用户:
在本机生成自己到密钥文件:
密钥文件是一对一的,如果密钥文件改变了,验证会失败的
可以简单的上传到服务器上,但可能会提示文件夹不存在:
如果有多个用户需要自动登录,那还是追加到方式比较合适,比如先上传到用户根目录为id.pub然后追加
自动脚本命令追加的方式:
自动输入密码的方式(未尝试,供参考):
方式一 安装sshpass:
# sudo apt-get install sshpass
安装完成后使用sshpass允许你用 -p 参数指定明文密码,然后直接登录远程服务器。例如:
# sshpass -p '你的密码' ssh 用户名@服务器ip地址
用 '-p' 指定了密码后,还需要在后面跟上标准的 ssh 连接命令。
方式二:
参考阅读:
1 ssh自动输入密码登录服务器/ssh免输入密码登录/非交互ssh 密码验证
2 Linux实现SSH无密码登录
3 http://bbs.chinaunix.net/viewthread.php?tid=1288757
原创内容如转载请注明:来自 阿权的书房
有两个方式可以实现,其一是用密钥验证,其二是自动输入密码。
第一个比较安全,简单几步就可以了:
1 生成自己到公钥和私钥
2 把公钥加入服务器端用户到authorized_keys
我们先在服务器上开通一个新用户:
root@test-server:~# useradd aslibra
root@test-server:~# passwd aslibra
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@test-server:~# cat /etc/passwd | grep aslibra
aslibra:x:1003:1003::/home/aslibra:/bin/sh
root@test-server:~# passwd aslibra
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@test-server:~# cat /etc/passwd | grep aslibra
aslibra:x:1003:1003::/home/aslibra:/bin/sh
在本机生成自己到密钥文件:
密钥文件是一对一的,如果密钥文件改变了,验证会失败的
lhq@lhq-laptop:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/lhq/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/lhq/.ssh/id_rsa.
Your public key has been saved in /home/lhq/.ssh/id_rsa.pub.
The key fingerprint is:
b9:f3:00:67:a8:cb:e3:cf:7b:21:2e:9f:00:f3:f6:8c lhq@lhq-laptop
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . . |
| o S |
| ..+.. |
| .. .+. |
| .ooo.o* |
| .+BO*F.+ |
+-----------------+
Generating public/private rsa key pair.
Enter file in which to save the key (/home/lhq/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/lhq/.ssh/id_rsa.
Your public key has been saved in /home/lhq/.ssh/id_rsa.pub.
The key fingerprint is:
b9:f3:00:67:a8:cb:e3:cf:7b:21:2e:9f:00:f3:f6:8c lhq@lhq-laptop
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| . . |
| o S |
| ..+.. |
| .. .+. |
| .ooo.o* |
| .+BO*F.+ |
+-----------------+
可以简单的上传到服务器上,但可能会提示文件夹不存在:
scp .ssh/id_rsa.pub aslibra@test-server.aslibra.com:~/.ssh/authorized_keys
如果有多个用户需要自动登录,那还是追加到方式比较合适,比如先上传到用户根目录为id.pub然后追加
lhq@lhq-laptop:~$ ssh aslibra@test-server.aslibra.com
aslibra@test-server.aslibra.com's password:
Linux test-server.aslibra.com 2.6.18-194.17.4.el5xen #1 SMP Mon Oct 25 16:36:31 EDT 2010 x86_64
.....
aslibra@test-server:~$ ls -a
. .. .ssh id.pub
aslibra@test-server:~$ cat id.pub >> .ssh/authorized_keys
aslibra@test-server:~$ exit
logout
Connection to test-server.aslibra.com closed.
lhq@lhq-laptop:~$ ssh aslibra@test-server.aslibra.com
Linux test-server.aslibra.com 2.6.18-194.17.4.el5xen #1 SMP Mon Oct 25 16:36:31 EDT 2010 x86_64
.....
aslibra@test-server:~$
#上面再次登录已经不提示密码了
aslibra@test-server.aslibra.com's password:
Linux test-server.aslibra.com 2.6.18-194.17.4.el5xen #1 SMP Mon Oct 25 16:36:31 EDT 2010 x86_64
.....
aslibra@test-server:~$ ls -a
. .. .ssh id.pub
aslibra@test-server:~$ cat id.pub >> .ssh/authorized_keys
aslibra@test-server:~$ exit
logout
Connection to test-server.aslibra.com closed.
lhq@lhq-laptop:~$ ssh aslibra@test-server.aslibra.com
Linux test-server.aslibra.com 2.6.18-194.17.4.el5xen #1 SMP Mon Oct 25 16:36:31 EDT 2010 x86_64
.....
aslibra@test-server:~$
#上面再次登录已经不提示密码了
自动脚本命令追加的方式:
cat ~/.ssh/id_rsa.pub | ssh aslibra@test-server.aslibra.com "cat - >> ~/.ssh/authorized_keys"
自动输入密码的方式(未尝试,供参考):
方式一 安装sshpass:
引用
# sudo apt-get install sshpass
安装完成后使用sshpass允许你用 -p 参数指定明文密码,然后直接登录远程服务器。例如:
# sshpass -p '你的密码' ssh 用户名@服务器ip地址
用 '-p' 指定了密码后,还需要在后面跟上标准的 ssh 连接命令。
方式二:
#!/usr/bin/expect
spawn /usr/bin/ssh root@10.99.0.245
expect "*password:"
send "123456\r"
expect "*]#"
send "cd /root"
expect "*]#"
send "exit\r"
expect eof
spawn /usr/bin/ssh root@10.99.0.245
expect "*password:"
send "123456\r"
expect "*]#"
send "cd /root"
expect "*]#"
send "exit\r"
expect eof
参考阅读:
1 ssh自动输入密码登录服务器/ssh免输入密码登录/非交互ssh 密码验证
2 Linux实现SSH无密码登录
3 http://bbs.chinaunix.net/viewthread.php?tid=1288757
原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
宁波LED


2010/12/22 20:19
难道这就是传说中的破解密码?
分页: 1/1
1
1
Eclipse乱码解决办法
ubuntu下的飞信
