『壹』 如何通过linux ssh远程linux不用输入密码登入
可以设置ssh密钥实现自动登录。
1.创建ssh密钥.
2.将生成的公钥传送到你要登录的远程主机,并将其加入文件 ~/.ssh/authorized_keys
具体步骤如下:
1.在你的主机生成密钥 ssh-keygen
测试已经OK。但是我的公钥是设置了密码的,所以这里还是提示输入公钥密码。如果
不设置公钥密码就不需要密码了。
5.注意事项。
如果没有.ssh文件,请ssh 192.168.0.188 (IP可以SSH的) 随便执行一次会自动生成
如果没有authorized_keys 请touch一个。 touch authorized_keys
修改下权限 chmod authorized_keys
『贰』 Linux浣跨敤ssh鍏閽ュ疄鐜板厤瀵嗙爜鐧诲綍瀹炰緥
鍏蜂綋鍐呭瑰備笅锛
ssh 鏃犲瘑鐮佺櫥褰曡佷娇鐢ㄥ叕閽ヤ笌绉侀挜銆俵inux涓嬪彲浠ョ敤鐢╯sh-keygen鐢熸垚鍏閽/绉侀挜瀵癸紝涓嬮潰鎴戜互CentOS涓轰緥銆
鏈夋満鍣ˋ(192.168.1.155)锛孊(192.168.1.181)銆傜幇鎯矨閫氳繃ssh鍏嶅瘑鐮佺櫥褰曞埌B銆
棣栧厛浠root璐︽埛鐧婚檰涓轰緥銆
1.鍦ˋ鏈轰笅鐢熸垚鍏閽/绉侀挜瀵广
[root@A ~]# ssh-keygen -t rsa -P ''
-P琛ㄧず瀵嗙爜锛-P '' 灏辫〃绀虹┖瀵嗙爜锛屼篃鍙浠ヤ笉鐢-P鍙傛暟锛岃繖鏍峰氨瑕佷笁杞﹀洖杞︼紝鐢-P灏变竴娆″洖杞︺
璇ュ懡浠ゅ皢鍦/root/.ssh鐩褰曚笅闈浜х敓涓瀵瑰瘑閽id_rsa鍜宨d_rsa.pub銆
涓鑸閲囩敤鐨剆sh鐨剅sa瀵嗛挜:
id_rsa 绉侀挜
id_rsa.pub 鍏閽
涓嬭堪鍛戒护浜х敓涓嶅悓绫诲瀷鐨勫瘑閽
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1
2.鎶夾鏈轰笅鐨/root/.ssh/id_rsa.pub 澶嶅埗鍒癇鏈虹殑 /root/.ssh/authorized_keys鏂囦欢閲岋紝鍏堣佸湪B鏈轰笂鍒涘缓濂 /root/.ssh 杩欎釜鐩褰曪紝鐢╯cp澶嶅埗銆
[root@A ~]# scp /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys
[email protected]'s password:
id_rsa.pub 100% 223 0.2KB/s 00:00
鐢变簬杩樻病鏈夊厤瀵嗙爜鐧诲綍鐨勶紝鎵浠ヨ佽緭鍏ヤ竴娆B鏈虹殑root瀵嗙爜銆
3.authorized_keys鐨勬潈闄愯佹槸600!!!
[root@B ~]# chmod 600 /root/.ssh/authorized_keys
4.A鏈虹櫥褰旴鏈恒
[root@A ~]# ssh -l root 192.168.1.181
The authenticity of host '192.168.1.181 (192.168.1.181)' can't be established.
RSA key fingerprint is 00:a6:a8:87:eb:c7:40:10:39:cc:a0:eb:50:d9:6a:5b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.181' (RSA) to the list of known hosts.
Last login: Thu Jul 3 09:53:18 2008 from root
[root@B ~]#
绗涓娆$櫥褰曟槸鏃惰佷綘杈撳叆yes銆
鐜板湪A鏈哄彲浠ユ棤瀵嗙爜鐧诲綍B鏈轰簡銆
灏忕粨锛氱櫥褰曠殑鏈哄瓙鍙鏈夌侀挜锛岃鐧诲綍鐨勬満瀛愯佹湁鐧诲綍鏈哄瓙鐨勫叕閽ャ傝繖涓鍏閽/绉侀挜瀵逛竴鑸鍦ㄧ侀挜瀹夸富鏈轰骇鐢熴備笂闈㈡槸鐢╮sa绠楁硶鐨勫叕閽/绉侀挜瀵癸紝褰撶劧涔熷彲浠ョ敤dsa(瀵瑰簲鐨勬枃浠舵槸id_dsa锛宨d_dsa.pub)
鎯宠〢锛孊鏈烘棤瀵嗙爜浜掔櫥褰曪紝閭B鏈轰互涓婇潰鍚屾牱鐨勬柟寮忛厤缃鍗冲彲銆
SSH-KeyGen 鐨勭敤娉
鍋囪 A 涓哄㈡埛鏈哄櫒锛孊涓虹洰鏍囨満;
瑕佽揪鍒扮殑鐩鐨勶細
A鏈哄櫒ssh鐧诲綍B鏈哄櫒鏃犻渶杈撳叆瀵嗙爜;
鍔犲瘑鏂瑰紡閫 rsa|dsa鍧囧彲浠ワ紝榛樿dsa
鍋氭硶锛
1銆佺櫥褰旳鏈哄櫒
2銆乻sh-keygen -t [rsa|dsa]锛屽皢浼氱敓鎴愬瘑閽ユ枃浠跺拰绉侀挜鏂囦欢 id_rsa,id_rsa.pub鎴杋d_dsa,id_dsa.pub
3銆佸皢 .pub 鏂囦欢澶嶅埗鍒癇鏈哄櫒鐨 .ssh 鐩褰曪紝 骞 cat id_dsa.pub ~/.ssh/authorized_keys
4銆佸ぇ鍔熷憡鎴愶紝浠嶢鏈哄櫒鐧诲綍B鏈哄櫒鐨勭洰鏍囪处鎴凤紝涓嶅啀闇瑕佸瘑鐮佷簡;
ssh-keygen鍋氬瘑鐮侀獙璇佸彲浠ヤ娇鍦ㄥ悜瀵规柟鏈哄櫒涓妔sh ,scp涓嶇敤浣跨敤瀵嗙爜.
鍏蜂綋鏂规硶濡備笅:
ssh-keygen -t rsa
鐒跺悗鍏ㄩ儴鍥炶溅,閲囩敤榛樿ゅ.
杩欐牱鐢熸垚浜嗕竴瀵瑰瘑閽ワ紝瀛樻斁鍦ㄧ敤鎴风洰褰曠殑~/.ssh涓嬨
灏嗗叕閽ヨ冨埌瀵规柟鏈哄櫒鐨勭敤鎴风洰褰曚笅锛屽苟鎷峰埌~/.ssh/authorized_keys涓銆
瑕佷繚璇.ssh鍜宎uthorized_keys閮藉彧鏈夌敤鎴疯嚜宸辨湁鍐欐潈闄愩傚惁鍒欓獙璇佹棤鏁堛(浠婂ぉ灏辨槸閬囧埌杩欎釜闂棰橈紝鎵句簡濂戒箙闂棰樻墍鍦)锛屽叾瀹炰粩缁嗘兂鎯筹紝杩欐牱鍋氭槸涓轰簡涓嶄細鍑虹幇绯荤粺婕忔礊銆
『叁』 linux涓璼sh浣跨敤rsa鍏嶅瘑鐮佺櫥褰曟柟娉
A涓烘湰鍦颁富鏈(鍗崇敤浜庢帶鍒跺叾浠栦富鏈虹殑鏈哄櫒) ;
B涓鸿繙绋嬩富鏈(鍗宠鎺у埗鐨勬満鍣⊿erver), 鍋囧俰p涓172.24.253.2 ;
鍦ˋ涓婄殑鍛戒护:
澶嶅埗浠g爜
浠g爜濡備笅:
ssh-keygen -t rsa
(杩炵画涓夋″洖杞,鍗冲湪鏈鍦扮敓鎴愪簡鍏閽ュ拰绉侀挜,涓嶈剧疆瀵嗙爜,鐢熸垚鐨勫叕閽ョ侀挜鍦▇/.ssh鐩褰曚笅锛宨d_rsa鏄绉侀挜锛 id_rsa.pub鏄鍏閽 )
澶嶅埗浠g爜
浠g爜濡備笅:
ssh a href="mailto:[email protected]"[email protected]/a
mkdir .ssh
chmod 700 .ssh
(闇瑕佽緭鍏ュ瘑鐮侊紝 娉:蹇呴』灏.ssh鐨勬潈闄愯句负700)
澶嶅埗浠g爜
浠g爜濡備笅:
scp ~/.ssh/id_rsa.pub a href="mailto:[email protected]:~/.ssh"[email protected]:~/.ssh/a
鐒跺悗鍦ㄦ湇鍔″櫒涓婂缓绔媋uthorized_keys鏂囦欢锛
澶嶅埗浠g爜
浠g爜濡備笅:
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
(娉ㄦ剰锛 蹇呴』灏唦/.ssh/authorized_keys鐨勬潈闄愭敼涓600, 璇ユ枃浠剁敤浜庝繚瀛榮sh瀹㈡埛绔鐢熸垚鐨勫叕閽ワ紝鍙浠ヤ慨鏀规湇鍔″櫒鐨剆sh鏈嶅姟绔閰嶇疆鏂囦欢/etc/ssh/sshd_config鏉ユ崲鏂囦欢鍚嶏紝榛樿ゆ槸鍙玜uthorized_keys锛
澶嶅埗浠g爜
浠g爜濡備笅:
cat id_rsa.pub authorized_keys
# (灏唅d_rsa.pub鐨勫唴瀹硅拷鍔犲埌 authorized_keys 涓, 娉ㄦ剰涓嶈佺敤 锛屽惁鍒欎細娓呯┖鍘熸湁鐨勫唴瀹癸紝浣垮叾浠栦汉鏃犳硶浣跨敤鍘熸湁鐨勫瘑閽ョ櫥褰)
鍥炲埌A鏈哄櫒:
澶嶅埗浠g爜
浠g爜濡備笅:
# ssh a href="mailto:[email protected]"[email protected]/a (涓嶉渶瑕佸瘑鐮, 鐧诲綍鎴愬姛)
浠庤〃闈涓婄畝鍗曠殑鐞嗚В涓涓嬬櫥褰曠殑杩囩▼,
棣栧厛 ssh-keygen -t rsa 鍛戒护鐢熸垚浜嗕竴涓瀵嗛挜鍜屼竴涓鍏閽, 鑰屼笖瀵嗛挜鍙浠ヨ剧疆鑷宸辩殑瀵嗙爜
鍙浠ユ妸瀵嗛挜鐞嗚В鎴愪竴鎶婇挜鍖, 鍏閽ョ悊瑙f垚杩欐妸閽ュ寵瀵瑰簲鐨勯攣澶,
鎶婇攣澶(鍏閽)鏀惧埌鎯宠佹帶鍒剁殑server涓, 閿佷綇server, 鍙鏈夋嫢鏈夐挜鍖(瀵嗛挜)鐨勪汉, 鎵嶈兘鎵撳紑閿佸ご, 杩涘叆server骞舵帶鍒
鑰屽逛簬鎷ユ湁杩欐妸閽ュ寵鐨勪汉, 蹇呴渶寰楃煡閬撻挜鍖欐湰韬鐨勫瘑鐮,鎵嶈兘浣跨敤杩欐妸閽ュ寵 (闄ら潪杩欐妸閽ュ寵娌¤剧疆瀵嗙爜), 杩欐牱灏卞彲浠ラ槻姝㈤挜鍖欒浜嗛厤浜(绉侀挜琚浜哄嶅埗)
鏇寸畝娲佺殑鏂瑰紡锛
姝ラ1銆佸傛灉鏈嶅姟鍣ㄤ笂娌℃湁authorized_keys鏂囦欢锛屽垯锛氾紙濡傛灉鏈夊垯璺宠繃杩欎竴姝ワ級
澶嶅埗浠g爜
浠g爜濡備笅:
ssh a href="mailto:[email protected]"[email protected]/a
mkdir .ssh
chmod 700 .ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
姝ラ2銆佸湪鏈鏈猴細
澶嶅埗浠g爜
浠g爜濡備笅:
ssh-keygen -t rsa
ssh--id -i ~/.ssh/id_rsa.pub a href="mailto:[email protected]"[email protected]/a
ssh-keygen -t rsa #涔嬪悗涓変笅鍥炶溅锛岃〃绀烘棤瀵嗙爜銆傚嵆鍙銆
娉: ssh--id 鑷鍔ㄦ妸瀵嗛挜杩藉姞鍒拌繙绋嬩富鏈虹殑 .ssh/authorized_key 涓
『肆』 濡備綍閫氳繃linux ssh杩滅▼linux涓嶇敤杈撳叆瀵嗙爜鐧诲叆
浣跨敤鍏绉侀挜閰嶇疆浜掍俊鍙浠ヨ揪鍒版晥鏋溿
1銆侀栧厛瑕侀厤缃畇sh浣垮叾鍏佽稿叕閽ヨよ瘉锛屽厛鏌ョ湅涓嬫槸涓嶆槸銆傞粯璁ゆ槸娉ㄩ噴鐨勶紝涔熸槸鍏佽稿叕閽ヨよ瘉
cat/etc/ssh/sshd_config|grep-ipub
#PubkeyAuthenticationyes
2銆佸亣璁炬垜浠瑕佷粠A鏈虹殑鐢ㄦ埛userA鏃犲瘑鐮佺櫥褰旴鏈虹殑鐢ㄦ埛userB锛岄栧厛鍦ˋ鏈轰笂鐢╱serA鎵ц岋細
$ssh-keygen-trsa(鍑虹幇鎻愮ず鍚庯紝涓鐩村洖杞﹀嵆鍙锛
姝ゅ懡浠や細鍦╱serA鐨刪ome鐩褰曚笅鐢熸垚 .ssh鐩褰曪紝骞跺湪鐩褰曚笅鐢熸垚鏂囦欢id_rsa鍜宨d_rsa.pub
3銆佺敤鐢ㄦ埛userB鐧诲綍B鏈烘墽琛岋細
$ssh-keygen锛堝嚭鐜版彁绀哄悗锛屼竴鐩村洖杞﹀嵆鍙锛屾ゅ勬槸涓轰簡鐢熸垚.ssh鐩褰曪紝鑻ユ湁姝ょ洰褰曪紝璇ユラゆ棤闇鎵ц屼簡锛
$cd.ssh
$scpuserA@<A鏈篒P>:~/.ssh/id_rsa.pub.鎷疯礉A鏈轰笂鐨刾ub鏂囦欢鍒癇鏈虹敤鎴穟serB鐨.ssh鐩褰
$catid_rsa.pub>>authorized_keys
4銆佸皾璇曠敤A鏈虹敤鎴穟serA鏃犲瘑鐮佺櫥褰旴鏈虹敤鎴穟serB