Linux服务器之间配置免密码SSH访问

环境:Oracle VM Virtualbox, Ubuntu Server 12.04,SecureCRT

1、首先在虚拟机中安装好Linux操作系统,这里我选择的是Ubuntu Server,为了方便后续操作,网络建议选择桥接(bridge),因为电脑比较老,虚拟机和Linux都是32位的

技术分享

安装Ubuntu的时候,注意选择安装ssh服务,如果没有安装,可以通过下面这个命令安装:

$sudo apt-get install openssh-server

通过下面命令可以查看ssh服务有没有启动:

$ps -e | grep ssh

 6606 ?        00:00:00 sshd
10284 ?        00:00:00 sshd
10453 ?        00:00:00 sshd

如果没有,可以通过一下命令启动:

$sudo /etc/init.d/ssh start

#配置文件:/etc/ssh/sshd_config
#若修改了配置文件,则需要重启ssh服务 

$sudo /etc/init.d/ssh restart

2、安装好Ubuntu以后需要设置静态IP(所有Linux服务器均需设置)

$vi /etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.100  #其它Linux服务器的IP需设置在同一网段
netmask 255.255.255.0
gateway 192.168.1.1

可以通过ifconfig命令查询IP是否设置正确

3、ssh免密码配置(192.168.1.101是我另一台Ubuntu的IP)

$ssh 192.168.1.101

#这时候提示需要password,输入正确的口令,会连接到相应的主机
#退出目标主机,开始配置

$ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/sweet/.ssh/id_rsa):  #不输入任何东西,直接回车
Enter passphrase (empty for no passphrase):  #不输入任何东西,直接回车
Enter same passphrase again:  #不输入任何东西,直接回车
......

$ls -a

#可以看到刚才的命令创建了一个.ssh的隐藏文件夹
$cd .ssh

$ls -l

total 12
-rw------- 1 sweet sweet 1679 Dec 21 11:56 id_rsa
-rw-r--r-- 1 sweet sweet  394 Dec 21 11:56 id_rsa.pub
-rw-r--r-- 1 sweet sweet 1110 Dec 21 21:19 known_hosts

#可以用cat命令看下,id_rsa和id_rsa.pub文件中的内容,一堆看不懂的字符

#把公钥文件拷贝到另外一台服务器上 需要在另外一台服务器上执行ssh-keygen -t rsa创建.ssh文件夹
$scp ./id_rsa.pub [email protected]:/home/sweet/.ssh/authorized_keys

#注意一下目标机的authorized_keys的权限是-rw-r--r--,如果不是需要执行chmod 644 authorized_keys修改文件的权限

$ssh 192.168.1.101
#这时候再执行这个命令就可以直接登录了,不需要输入password了

在另外一台服务器上进行相同的步骤操作后,即可以相互免密码登录了。

???注:如果配置hadoop的话,需要本机对本机能够进行免密码访问,直接将公钥文件id_rsa.pub的文件追加到authorized_keys中即可???

$cat id_rsa.pub >> authorized_keys


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。