(1) SSH 安裝並配置linux
CentOS 默認已安裝了 SSH client、SSH server,打開終端執行以下命令進行檢驗shell
rpm -qa | grep ssh
若是返回的結果以下圖所示,包含了 SSH client 跟 SSH server,則不須要再安裝vim
若須要安裝,則能夠經過 yum 進行安裝:安全
yum -y install openssh-clients openssh-server openssh
注:sshd 在 openssh-server,ssh 在 openssh-clients,ssh-keygen 在 openssh 中。服務器
接着執行以下命令測試一下 SSH 是否可用:oracle
ssh localhost
此時會有以下提示(SSH 首次登錄提示),輸入 yes 。而後按提示輸入密碼,這樣就登錄到本機了。ssh
[root@master home]# ssh localhost The authenticity of host 'localhost (::1)' can't be established. ECDSA key fingerprint is bd:66:a8:94:98:d5:0b:f3:bc:e9:5c:41:4c:ca:b3:d4. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts. root@localhost's password: Last login: Fri Apr 20 07:07:31 2018 from 192.168.2.1
但這樣登錄是須要每次輸入密碼的,咱們須要配置成 SSH 無密碼登錄比較方便。oop
(2) SSH 免密登錄測試
首先在 client 端用 ssh-keygen 生成公私祕鑰對,並將公鑰追加到 server 端的 ~/.ssh/authorized_keys 文件中:spa
rm ./id_rsa* # 刪除以前生成的公匙(若是有) ssh-keygen -t rsa # 會有提示,都按回車就能夠 ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa scp ~/.ssh/id_rsa.pub hadoop@192.168.2.110:/home/hadoop/.ssh # 將生成的公鑰拷貝到遠程服務器上 cat id_rsa.pub >> authorized_keys # 加入受權 chmod 600 ./authorized_keys # 修改文件權限,不然沒法登錄,親測 ssh-copy-id hadoop@192.168.2.110 # 或直接遠程受權,不須要拷貝
注意: 必定要 chmod 來一下,否則沒法起做用。此時 client 再次 ssh server 就無需輸入密碼就能夠直接登錄了。
1. sshd 在 openssh-server
2. ssh 在 openssh-clients
3. ssh-keygen 在 openssh
(3) SSH 配製
scp 是 secure copy 的簡寫,用於在 Linux 下進行遠程拷貝文件,相似於 cp 命令,不過 cp 只能在本機中拷貝。執行 scp 時會要求輸入 slave1 上 hadoop 用戶的密碼(hadoop)。
sudo vim /etc/ssh/sshd_config RSAAuthentication yes # 啓用 RSA 認證 PubkeyAuthentication yes # 啓用公鑰私鑰配對認證方式
scp ~/.ssh/id_rsa.pub hadoop@slave1:/home/hadoop/ # 遠程拷貝 scp /home/hadoop/hadoop-2.7.6.tar.gz hadoop@slave1:`pwd` # 遠程拷貝 ssh root@sdw2 'chown -R hadoop:hadoop /usr/local/hadoop' # 遠程執行命令
/var/log/secure 日誌看到是權限問題:Dec 21 14:54:55 SHLT-TB-ORACLEBAK sshd[12347]: Authentication refused: bad ownership or modes for directory /home/oracle (tailf /var/log/secure)
ssh -vvv 192.168.2.101 # 查看詳細日誌,用於排錯
sshd爲了安全,對屬主的目錄和文件權限有所要求。若是權限不對,則 ssh 的免密碼登錄不生效。
用戶目錄權限爲 755 rsa_id.pub 及authorized_keys權限通常爲644 rsa_id權限必須爲600。
用腳本實現 SSH 互信,依賴 expect 服務
yum install -y tcl tclx tcl-devel expect
vi ssh.sh #!/usr/bin/expect set timeout 30 spawn ssh test@192.168.140.111 # 執行 shell 命令 expect "password:" send "pwd\r" interact # 執行完成後保持交互狀態,而不是退出遠程終端
(1) [#!/usr/bin/expect] 注意:這一行須要在腳本的第一行。
(2) [set timeout 30] 單位:s。
(3) [spawn ssh test@192.168.140.111] spawn 用來傳遞交互指令。
(4) [expect "password:"] 判斷上次輸出結果裏是否包含「password:」的字符串。
(5) [send "pwd\r"] 執行交互動做。
(6) [interact] 執行完成後保持交互狀態,而不是退出遠程終端。
useradd test echo pwd | passwd --stdin test
執行 expect ssh.sh 後會登錄到 192.168.140.111 上。
#!/bin/expect # 循環 1000 臺機器的IP地址,生成密鑰文件 authorized_keys for ip in {cat ip.list} do ssh user@$ip ssh-keygen -t rsa &>/dev/null expect{ "yes/no" { send "yes\r";exp_continue} "password:"{send "$passwd\r";exp_continue} } cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys &> /dev/null exit if [ !-f ~/.ssh/authorized_keys ]; then touch ~/.ssh/authorized_keys fi ssh user@$ip cat ~/.ssh/authorized_keys >> ~/.ssh/authorized_keys &> /dev/null expect { "yes/no" { send "yes\r";exp_continue} "password:"{send "$passwd\r";exp_continue} } done # scp authorized_keys 文件到各臺機器上面。 for ip in {cat ip.list} do scp ~/.ssh/authorized_keys user@$ip:~/.ssh/ expect { "yes/no" { send "yes\r";exp_continue} "password:"{send "$passwd\r";exp_continue} } done
天天用心記錄一點點。內容也許不重要,但習慣很重要!