天天看點

多台伺服器免密登陸設定

在開始之前需要對伺服器進行基本的配置

一. Linux配置

  1. 關閉防火牆和selinux
    #關閉防火牆
    service iptables stop
    chkconfig iptables off
    
    #關閉selinux
    vim /etc/selinux/config
    #設定為disabled
    SELINUX=disabled    
               
  2. 百度雲配置設定的名字太麻煩了,為友善操作修改主機名,但是遇到了一些問題,詳細請點選 Linux修改hostname的幾種方式:
    #百度雲   修改後,重新開機(reboot -h)永久生效
    vi /etc/sysconfig/network
    #第2台  将hostname改為node01
    HOSTNAME=node01
    #第2台  将hostname改為node01
    HOSTNAME=node03
     
    #騰訊雲   可能我裝的不是原版的,出來的居然是這個鬼東西
    # Created by cloud-init on instance boot automatically, do not edit.
    #在下面添加一行
    NETWORKING=yes
    HOSTNAME=node02
    然後使用: vi /etc/hostname
    node02
    #reboot -h 重新開機後生效了
    
               

    2.域名映射并測試

    /etc/hosts 檔案用于在通過主機名進行通路時做 ip 位址解析之用。

    是以,你想通路一個什麼樣的主機名,就需要把這個主機名和它對應的 ip 位址. 配置在/etc/hosts 檔案中.

    #三台伺服器進行域名映射操作
    vim /etc/hosts
    127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    #172.16.0.4 instance-mv524v7d instance-mv524v7d.novalocal
    
    #添加以下配置,前面為你伺服器的ip
    192.168.221.100 node01
    192.168.221.110 node02
    192.168.221.120 node03
    
    # 測試騰訊雲與百度雲 互ping:
    [email protected] etc# ping node01
    PING node01 (106.13.你的ip.你的ip) 56(84) bytes of data.
    64 bytes from node01 (106.13.你的ip.你的ip): icmp_seq=1 ttl=48 time=60.1 ms
    64 bytes from node01 (106.13.你的ip.你的ip): icmp_seq=2 ttl=48 time=59.6 ms
    64 bytes from node01 (106.13.你的ip.你的ip): icmp_seq=3 ttl=48 time=59.7 ms
    
    [[email protected] ~]# ping node02
    PING node02 (132.232.你的ip.你的ip) 56(84) bytes of data.
    64 bytes from node02 (132.232.你的ip.你的ip): icmp_seq=1 ttl=49 time=60.1 ms
    64 bytes from node02 (132.232.你的ip.你的ip): icmp_seq=2 ttl=49 time=59.6 ms
    64 bytes from node02 (132.232.你的ip.你的ip): icmp_seq=3 ttl=49 time=59.6 ms
    
    [[email protected] ~]# ping node01
    PING node01 (106.13.你的ip.你的ip) 56(84) bytes of data.
    64 bytes from node01 (106.13.你的ip.你的ip): icmp_seq=1 ttl=59 time=1.29 ms
    64 bytes from node01 (106.13.你的ip.你的ip): icmp_seq=2 ttl=59 time=0.526 ms
    64 bytes from node01 (106.13.你的ip.你的ip): icmp_seq=3 ttl=59 time=0.570 ms
               
    重新開機伺服器

二、3台伺服器免密碼登入

1. 三台機器生成公鑰與私鑰:

[[email protected] ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):  #回車
Enter passphrase (empty for no passphrase):  #回車
Enter same passphrase again:  #回車
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
35:8f:46:e1:19:d1:91:20:44:01:cb:6c:58:bd:4f:85 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|      o==.==.o   |
|     = ..oE++    |
|    . =  .*.     |
|     .  .o.+     |
|        Soo .    |
|         ..      |
|                 |
|                 |
|                 |
+-----------------+
           

2. 拷貝公鑰到同一台機器

[[email protected] ~]# ssh-copy-id node01
The authenticity of host 'node01 (106.13.55.197)' can't be established.
RSA key fingerprint is 7c:70:21:11:f4:64:f4:2a:db:db:f9:42:65:63:9b:c6.
Are you sure you want to continue connecting (yes/no)? yes  #輸入yes
Warning: Permanently added 'node01,106.13.55.197' (RSA) to the list of known hosts.
[email protected]'s password:   #輸入第一台伺服器的密碼
Now try logging into the machine, with "ssh 'node01'", and check in:

 .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

           

3. 複制第一台機器的認證到其他機器

#根據提示,輸入yes,和伺服器的登陸密碼
scp /root/.ssh/authorized_keys node02:/root/.ssh/
scp /root/.ssh/authorized_keys node03:/root/.ssh/
           

4. 通過網絡時鐘同步;

#設定定時任務
[[email protected] ~]#  crontab -e
#添加阿裡雲時鐘同步定時任務
*/1 * * * * /usr/sbin/ntpdate ntp4.aliyun.com;
           

繼續閱讀