衡阳师范学院好吗:ssh配置及实现ssh免登陆Linux

来源:百度文库 编辑:九乡新闻网 时间:2024/04/23 08:49:02
去年4、5月份时,实现了ssh免登陆的,不过前几天又因工作再需要,重新做了一遍,因为以前做过的都记不得了,免不了google解决,现在总结一下。
1. ssh是什么?
Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices. Used primarily on GNU/Linux and Unix based systems to access shell accounts, SSH was designed as a replacement for Telnet and other insecure remote shells, which send information, notably passwords, in plaintext, rendering them susceptible to packet analyzation.The encryption used by SSH provides confidentiality and integrity of data over an insecure network, such as the Internet.   -- from wikipedia
SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用SSH 协议可以有效防止远程管理过程中的信息泄露问题。
2. 准备工作
   假设A主机10.20.0.1想通过ssh登录到B主机10.20.0.2上。
   那么客户端(A主机)需要安装ssh客户端软件,服务器端(B主机开机sshd进程)需要安装ssh服务器软件。
   ssh客户端Linux发行版一般都自带的,对于ssh服务器端,Ubuntu用户可以sudo apt-get install openssh-server来安装,其他Linux用户也安装openssh-server即可。
   在各自的$HOME目录下都有一个.ssh隐藏目录。
3. 生成密钥
   在A主机,cd $HOME/.ssh
         运行命令ssh-keygen -t dsa 生成密钥。(dsa换成rsa,即是使用rsa加密算法生成密钥了)
         一路Enter键确认下来,当然遇到"yes/no?"一般选yes就好了。
4. 分发密钥(公钥)
   在A主机,scp ~/.ssh/id_dsa.pub  userB@10.20.0.2:~/.ssh/id_dsa.pub
   此过程中需要输入B主机的userB的密码。
   然后,在B主机:cat ~/.ssh/id_dsa.pub  >> ~/.ssh/authorized_keys (将id_dsa.pub的内容追加到 authorized_keys中)
5. 享受免登陆
   在A主机:ssh userB@10.20.0.2  不需输入密码即可登录B主机;
   当然 scp test userB@10.20.0.2 远程拷贝也不需输入密码了(scp基于ssh协议嘛);
   还可以ssh远程执行B主机上的命令或执行脚本,比如:
   ssh test userB@10.20.0.2 'apache2 -k start' #启动B主机上的Apache服务;
   远程执行多条命令可用分号隔开,如:ssh test userB@10.20.0.2 'ls;pwd'