mac OS版本為Mojave (10.14.1)
一、有私鑰登陸
1.生成公欠雌、私鑰
1.1 密鑰類型為“RSA”蹄梢,密鑰長(zhǎng)度“2048”(自定義,根據(jù)遠(yuǎn)程服務(wù)器需求生成)
1.2 passphrase可設(shè)置為空富俄,這樣則自動(dòng)登陸禁炒,免密碼
# ssh-keygen -b 2048 -t rsa -C catayi@test #-C為審計(jì)做準(zhǔn)備,在公鑰的末尾加上 “空格”+“備注”
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ~/.ssh/id_rsa.
Your public key has been saved in ~/.ssh/id_rsa.pub.
The key fingerprint is:
10:ce:a1:5e:cc:c6:da:49:c8:e1:dc:30:63:41:60:18 zhangshumin@ops
The key's randomart image is:
+--[ RSA 2048]----+
|EoooO.o |
|.. = # o |
| * % |
| . * o |
| o o S |
| |
| |
| |
| |
+-----------------+
2.公鑰pub給遠(yuǎn)程ssh服務(wù)器配置霍比,私鑰找個(gè)地方保存起來(lái)待每次登陸驗(yàn)證
3.寫登陸腳本
~/.ssh/config文件下寫入以下配置幕袱,沒(méi)有則創(chuàng)建一個(gè)
Host catayitest #別名
HostName 192.168.0.1 #遠(yuǎn)程host或ip
Port 22 #遠(yuǎn)程端口
User catayi #遠(yuǎn)程登陸用戶
IdentityFile ~/.ssh/id_rsa #私鑰位置
4.終端輸入#ssh catayitest
(剛才寫的別名)然后回車,顯示要輸入passphrase(創(chuàng)建公悠瞬、私鑰的時(shí)候有則輸入们豌,沒(méi)有則繼續(xù)回車)涯捻,登陸成功!
二望迎、無(wú)私鑰登陸
1.首先mac要安裝expect障癌、spawn-fcgi(homebrew一鍵安裝)
2.找方便自己的路徑下寫一下腳本,保存為.sh后綴的文件辩尊,如catayitest2.sh
#!/usr/bin/expect -f
set timeout 3
spawn ssh username@hostname #username:遠(yuǎn)程服務(wù)器登陸用戶名涛浙;hostname:遠(yuǎn)程服務(wù)器地址或ip
expect "*yes/no*" {send "yes\n"}
expect "*passphrase*" {send "\r"} #這里若是共存有私鑰登陸則可以跳過(guò)驗(yàn)證
expect "*password:*" {send "xxx\r";interact} #xxx:登陸密碼
- ~/.ssh/config文件下追加以下配置,沒(méi)有則創(chuàng)建一個(gè)
Host catayitest2 #別名
HostName 192.168.0.1 #遠(yuǎn)程host或ip
Port 22 #遠(yuǎn)程端口
User catayi #遠(yuǎn)程登陸用戶
4.終端輸入#expect catayitest2.sh
然后一路回車摄欲,
自動(dòng)填密碼登陸成功轿亮!