一、無密碼登入辦法
環境:本機器:ubuntu 9.04,伺服器centos5.x系列。
最近為了工作需要,開機啟動將伺服器的檔案掛載到自己的工作區,
一開始採用smbmount進行掛載,建立項目進行開發。掛載裡面的檔案,每次儲存時都會提示你檔案已經修改需要你重新整理整個項目,太麻煩了。都不敢隨便儲存。不即時儲存容易出現的問題大家可想而知。
Samba是為大家在windows和linux進行檔案分享權限設定量身定製的。但是對於linux而言就不盡人意了。
smbmount 掛載可以直接在參數上面輸入使用者名稱和密碼。當然開機掛載很容易了,命令如下:
- sudo /etc/rc.local;
smbmount //ip:/xxx /mnt/ -o user%pwd;
以上方式不推薦使用。
那麼linux之間掛載採用什麼方式呢 —— 有以下兩種 nfs,sshfs。本人再用的是sshfs。
具體掛載命令如下。
- 安裝 :sudo apt-get install sshfs;
sudo sshfs user@ip-address:/remote/directory /www/workspaces;
- 開機自動掛載
sudo /etc/rc.local;
- 在exit 0 之前加上:
su martin -c 'sshfs user@ip-address:/remote/directory /www/workspaces;’#martin擁有私密金鑰的使用者
- 但是這個中間需要輸入密碼。下面講以下ssh無密碼登入.
- 覺得輸入密碼煩躁的時候。
- 不得不從其他伺服器scp檔案的時候。
下面M表示本機,S表示伺服器(或者目的ip)
- 首先在M運行ssh-keygen -t rsa;#dsa表示加密類型,exp:dsa,rsa
然後斷行符號知道結束。
- cd ~/.ssh/;ls -l;會看見有下面幾個檔案
其中id_rsa 是私密金鑰。id_rsa.pub是公開金鑰。
將M中ssh產生的公開金鑰複製下來。
- 接下來進如ssh 連結上S機器。
將公開金鑰內容複寫到S機器。
vi ~/.ssh/authorized_keys;
- 在S機器將檔案authorized_keys賦予600許可權。
chmod 600 ~/.ssh/authorized_keys;
多個使用者可以將自己機器內產生的公開金鑰在authorized_keys下面複製進去。
這樣就完成了。
vi /var/log/secure;可以看到登入記錄。
卸載,方法使用umount /xxx就行。
出現錯誤
Agent admitted failure to sign using the key.
則需要在M機器上將私密金鑰添加進來
ssh-add .ssh/id_rsa;
二、linux使用認證ssh登陸
ssh-keygen -t rsa
指定密鑰路徑和輸入口令之後,即在/home/msa/.ssh/中產生公開金鑰和私密金鑰:id_rsa id_rsa.pub(root帳號下在/root/.ssh/中)
輸入口令之後在CRT中登陸的時候需驗證口令
如以下例子
[root@kangta-01 .ssh]# 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:
06:25:cd:e3:b6:e7:13:76:3e:59:81:4f:77:43:bd:0a root@kangta-01
[root@kangta-01 .ssh]# ll
總計 12
-rw------- 1 root root 1743 11-28 23:47 id_rsa
-rw-r--r-- 1 root root 396 11-28 23:47 id_rsa.pub
-rw-r--r-- 1 root root 395 11-17 00:38 known_hosts
cat id_rsa.pub >> authorized_keys
至於為什麼要產生這個檔案,sshd_config裡面寫的就是這個.
然後chmod 400 authorized_keys,稍微保護一下.
在CRT上的終端可用sz id_rsa拉回本地,然後把伺服器上的id_rsa和id_rsa.pub幹掉
6)配置/etc/ssh/sshd_config
Protocol 2
ServerKeyBits 1024
PermitRootLogin no #禁止root登入而已,與本文無關,加上安全些
#以下三行沒什麼要改的,把預設的#注釋去掉就行了
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
PermitEmptyPasswords no
7)重啟sshd
/sbin/service sshd restart
8)轉換認證格式
運行CRT,在tools下convert private key to openssh format 轉換id_rsa為crt的ppk認證檔案
9)配置CRT登入
在connection--SSH--publickey中,點擊Browse,選擇剛剛轉換好的認證.
三、SSH遠程登入失敗,提示“Password authentication failed”
用SecureCRT登陸,提示“Password authentication failed”
使用linux下ssh登陸,提示“Permission denied, please try again.”
網上收集的相關資料:
方法一:
1. vi伺服器端的/etc/ssh/sshd_config
2. 把PasswordAuthentication設成yes
3. 重啟ssh服務
方法二:
1.vi伺服器端的/etc/ssh/sshd_config
2.找到#PermitRootLogin no將其修改為PermitRootLogin yes
方法三:
1.查看sshd是否正確在運行
netstat -anp | grep sshd
2.如果沒有運行使用 service sshd restart 重啟服務
方法四:
1.用戶端使用 ssh -v root@192.168.0.120 查看登陸資訊
2.提示“Permission denied, please try again.”
3.查看防火牆
#/etc/init.d/iptables status
如果22連接埠被限制則無法登陸。
放開連接埠22
#/sbin/iptables -I INPUT -p tcp –dport 22 -j ACCEPT
#/etc/rc.d/init.d/iptables save