WIKI使用導(dǎo)航
站長百科導(dǎo)航
站長專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計算
- 微博營銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺
- WIKI程序與應(yīng)用
- 美國十大主機(jī)
CentOS/用OpenSSH構(gòu)建SSH服務(wù)器
CentOS | CentOS安裝 | CentOS使用手冊 |
SSH服務(wù)和Telnet服務(wù)一樣,通過遠(yuǎn)程登錄登錄到系統(tǒng),在遠(yuǎn)程操控系統(tǒng)。但它與Telnet的不同點就是:Telnet在傳輸?shù)倪^程中是平文傳輸,而SSH是將傳輸內(nèi)容加密,在傳送的過程中保證了傳送內(nèi)容的保密性,從而提高了系統(tǒng)的安全性。
在這里,我們不準(zhǔn)備將SSH服務(wù)作為用戶上傳下載文件的工具。我們只用SSH服務(wù)的開通為遠(yuǎn)程管理系統(tǒng)提供方便。另外在用戶認(rèn)證方式上,為了服務(wù)器和用戶的安全,禁止用戶密碼的認(rèn)證方式,而基于“鑰匙”的方式。
SSH相關(guān)配置文件的修改[ ]
首先修改SSH的配置文件。如下:
[root@sample ~]# vi /etc/ssh/sshd_config ← 用vi打開SSH的配置文件 #Protocol 2,1 ← 找到此行將行頭“#”刪除,再將行末的“,1”刪除,只允許SSH2方式的連接 ↓ Protocol 2 ← 修改后變?yōu)榇藸顟B(tài),僅使用SSH2 #ServerKeyBits 768 ← 找到這一行,將行首的“#”去掉,并將768改為1024 ↓ ServerKeyBits 1024 ← 修改后變?yōu)榇藸顟B(tài),將ServerKey強(qiáng)度改為1024比特 #PermitRootLogin yes ← 找到這一行,將行首的“#”去掉,并將yes改為no ↓ PermitRootLogin no ← 修改后變?yōu)榇藸顟B(tài),不允許用root進(jìn)行登錄 #PasswordAuthentication yes ← 找到這一行,將yes改為no ↓ PasswordAuthentication no ← 修改后變?yōu)榇藸顟B(tài),不允許密碼方式的登錄 #PermitEmptyPasswords no ← 找到此行將行頭的“#”刪除,不允許空密碼登錄 ↓ PermitEmptyPasswords no ← 修改后變?yōu)榇藸顟B(tài),禁止空密碼進(jìn)行登錄
然后保存并退出。(vi保存退出的命令為ZZ)
因為我們只想讓SSH服務(wù)為管理系統(tǒng)提供方便,所以在不通過外網(wǎng)遠(yuǎn)程管理系統(tǒng)的情況下,只允許內(nèi)網(wǎng)客戶端通過SSH登錄到服務(wù)器,以最大限度減少不安全因素。設(shè)置方法如下:
[root@sample ~]# vi /etc/hosts.deny ← 修改屏蔽規(guī)則,在文尾添加相應(yīng)行 # # hosts.deny This file describes the names of the hosts which are # *not* allowed to use the local INET services, as decided # by the '/usr/sbin/tcpd' server. # # The portmap line is redundant, but it is left to remind you that # the new secure portmap uses hosts.deny and hosts.allow. In particular # you should know that NFS uses portmap! sshd: ALL ← 添加這一行,屏蔽來自所有的SSH連接請求 [root@sample ~]# vi /etc/hosts.allow ← 修改允許規(guī)則,在文尾添加相應(yīng)行 # # hosts.allow This file describes the names of the hosts which are # allowed to use the local INET services, as decided # by the '/usr/sbin/tcpd' server. # sshd: 192.168.0. ← 添加這一行,只允許來自內(nèi)網(wǎng)的SSH連接請求
重新啟動SSH服務(wù)[ ]
在修改完SSH的配置文件后,需要重新啟動SSH服務(wù)才能使新的設(shè)置生效。
root@sample ~]# /etc/rc.d/init.d/sshd restart ← 重新啟動SSH服務(wù)器 Stopping sshd: [ OK ] Starting sshd: [ OK ] ← SSH服務(wù)器重新啟動成功
這時,在遠(yuǎn)程終端(自用PC等等)上,用SSH客戶端軟件以正常的密碼的方式是無法登錄服務(wù)器的。為了在客戶能夠登錄到服務(wù)器,我們接下來建立SSH用的公鑰與私鑰,以用于客戶端以“鑰匙”的方式登錄SSH服務(wù)器。
SH2的公鑰與私鑰的建立[ ]
登錄為一個一般用戶,基于這個用戶建立公鑰與私鑰。(這里以centospub用戶為例)
[root@sample ~]# su - centospub ← 登錄為一般用戶centospub [centospub@sample ~]$ ssh-keygen -t rsa ← 建立公鑰與私鑰 Generating public/private rsa key pair. Enter file in which to save the key (/home/kaz/.ssh/id_rsa): ← 鑰匙的文件名,這里保持默認(rèn)直接回車 Created directory '/home/kaz/.ssh' Enter passphrase (empty for no passphrase): ← 輸入口令 Enter same passphrase again: ← 再次輸入口令 Your identification has been saved in /home/kaz/.ssh/id_rsa. Your public key has been saved in /home/kaz/.ssh/id_rsa.pub. The key fingerprint is: tf:rs:e3:7s:28:59:5s:93:fe:33:84:01:cj:65:3b:8e centospub@sample.centospub.com
然后確認(rèn)一下公鑰與密鑰的建立,以及對應(yīng)于客戶端的一些處理。
[centospub@sample ~]$ cd ~/.ssh ← 進(jìn)入用戶SSH配置文件的目錄 [centospub@sample .ssh]$ ls -l ← 列出文件 total 16 -rw------- 1 centospub centospub 951 Sep 4 19:22 id_rsa ← 確認(rèn)私鑰已被建立 -rw-r--r-- 1 centospub centospub 241 Sep 4 19:22 id_rsa.pub ← 確認(rèn)公鑰已被建立 [centospub@sample .ssh]$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys ← 公鑰內(nèi)容輸出到相應(yīng)文件中 [centospub@sample .ssh]$ rm -f ~/.ssh/id_rsa.pub ← 刪除原來的公鑰文件 [centospub@sample .ssh]$ chmod 400 ~/.ssh/authorized_keys ← 將新建立的公鑰文件屬性設(shè)置為400
然后,將私鑰通過安全的方式轉(zhuǎn)移到欲通過SSH連接到服務(wù)器的PC上。這里,以通過3.5寸磁盤為介質(zhì)為例:
centospub@sample .ssh]$ exit ← 退出一般用戶的登錄(返回root的登錄) [root@sample ~]# mount /mnt/floppy/ ← 加載軟盤驅(qū)動器 [root@sample ~]# mv /home/centospub/.ssh/id_rsa /mnt/floppy/ ← 將剛剛建立的私鑰移動到軟盤 [root@sample ~]# umount /mnt/floppy/ ← 卸載軟盤驅(qū)動器
這樣,我們通過對應(yīng)于centospub用戶的私鑰,就可以在遠(yuǎn)程終端上通過SSH客戶端連接到服務(wù)器了。
參考來源[ ]
http://www.centospub.com/make/sshd.html