OpenSSH技術詳解

一、什么是Openssh?

????OpenSSH 是?SSH?(Secure?SHell) 協(xié)議的免費開源實現(xiàn)。SSH協(xié)議族可以用來進行遠程控制, 或在計算機之間傳送文件。而實現(xiàn)此功能的傳統(tǒng)方式荣挨,如telnet(終端仿真協(xié)議)、 rcp ftp朴摊、 rlogin默垄、rsh都是極為不安全的,并且會使用明文傳送密碼甚纲。OpenSSH提供了服務端后臺程序和客戶端工具口锭,用來加密遠程控件和文件傳輸過程中的數(shù)據(jù),并由此來代替原來的類似服務贩疙。

知識延伸:

? ? ssh協(xié)議有兩個版本:

? ? ? ? v1:基于CRC-32 做MAC讹弯,不安全; (一般用于實現(xiàn)主機認證)

? ? ? ? v2:基于協(xié)議協(xié)商選擇雙方都支持的最安全的MAC機制

? ? ? ? ? ? 基于DH做密鑰交換这溅,基于RSA或DSA實現(xiàn)身份認證组民,從而實現(xiàn)無需輸入賬號面膜

? ? ? ? ? ? 客戶端通過檢查服務器端的主機秘鑰來判斷是否能夠繼續(xù)通信;

? ? 認證方式:

? ? ? ? 1悲靴、基于口令的認證

? ? ? ? 2臭胜、基于密鑰的認證


二、為什么要使用OpenSSH

????由于傳統(tǒng)的telne癞尚、rcp ftp等工具是明文傳輸數(shù)據(jù)的耸三,對數(shù)據(jù)安全性存在很大的安全隱患,而OpenSSH可以對傳輸?shù)臄?shù)據(jù)進行加密從而大大提高了數(shù)據(jù)的安全性浇揩。


三仪壮、OpenSSH程序簡介

? 1、OpenSSH的分為客戶端和服務端兩部分

Clients端的配置文件:/etc/ssh/ssh_config

Server端的配置文件:/etc/ssh/sshd_config

Server端服務腳本:/etc/rc.d/init.d/sshd

OpenSSH在Linux系統(tǒng)中默認是安裝并啟動的

openssh 主要的關鍵包有四個

openssh.x86_64? ? ? ? ? ? ? ? ? ? ? 5.3p1-104.el6? //服務端和客戶端的公共組件? ? ? ?

openssh-askpass.x86_64? ? ? ? ? ? ? 5.3p1-104.el6? //? ?

openssh-clients.x86_64? ? ? ? ? ? ? 5.3p1-104.el6? //客戶端安裝包? ?

openssh-server.x86_64? ? ? ? ? ? ? ? 5.3p1-104.el6? //服務端安裝包


????openssl-clients? 幾個常用文件

[root@1inuxssh]# rpm -ql openssh-clients

/etc/ssh/ssh_config? ? ? ? ? ? ?//客戶端配置文件

/usr/bin/scp? ? ? ? ? ? ? ? ? ? ? //遠程復制文件

/usr/bin/sftp? ? ? ? ? ? ? ? ? ? ?//遠程文件共享

/usr/bin/slogin

/usr/bin/ssh

/usr/bin/ssh-add

/usr/bin/ssh-agent

/usr/bin/ssh-copy-id

/usr/bin/ssh-keyscan


????openssl-server 幾個常用文件

/etc/rc.d/init.d/sshd

/etc/ssh/sshd_config

/etc/sysconfig/sshd


? 2胳徽、服務器端配置文件/etc/ssh/sshd_config 主要參數(shù)詳解

服務端配置文件是讓別人登陸時使用的

注:配置文件中使用“#”注釋掉的一般就是使用默認

? ? #Port 22? ? //默認端口號积锅,為了其安全一般要更改為其他端口

? ? #AddressFamily any? ? //說明要監(jiān)聽任意地址

? ? #ListenAddress 0.0.0.0 //監(jiān)聽本機所有IPV4的ip

? ? #ListenAddress ::? ? ? ? //監(jiān)聽本機所有的IPV6的地址

? ? Protocol 2? 監(jiān)聽的協(xié)議版本

? ? # HostKey for protocol version 1? //說明key的協(xié)議版本

? ? SyslogFacility AUTHPRIV? ? ? ? //使用AUTHPRIV 記錄日志?

? ? #LogLevel INFO? ? //log日志級別



? ? #Authentication:? ? ? ? ? ? //認證相關

? ? #LoginGraceTime 2m? ? //登陸寬限時長? 默認2分鐘不登錄自動關閉

? ? #PermitRootLogin yes? ? //是否支持管理員直接登陸

? ? #StrictModes yes? ? //是否使用嚴格模式 (嚴格檢查用戶的某些相關信息)

? ? #MaxAuthTries 6? ? ? ? //最大嘗試次數(shù)? (6次以后終端斷開)

? ? #MaxSessions 10? ? ? ? //最大并發(fā)允許鏈接數(shù) (超過 將拒絕)

? ? #RSAAuthentication yes? ? //是否支持RSA密鑰認證

? ? #PubkeyAuthentication yes? ? //是否支持公鑰認證

? ? #AuthorizedKeysFile? ? .ssh/authorized_keys? //默認保存口令的文件

? ? #PermitEmptyPasswords no? ? ? ? //是否支持空密碼登陸

? ? PasswordAuthentication yes


? ? UsePAM yes? ? ? ? ? ? ? ? ? ? ? //是否使用PAM 認證( 是一種統(tǒng)一認證框架)

? ? X11Forwarding yes? ? ? ? ? ?//是否轉發(fā)圖形界面請求 (可以打開遠程服務器圖形界面)

? ? Subsystem? ? ? sftp? ? /usr/libexec/openssh/sftp-server?

? ? #UseDNS yes? ? ? ? ? ? ? ? ? ? ?//是否允許DNS反解? 比較浪費時間一般更改為no

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? /etc/ssh/ssh_known_hosts? //保存已經(jīng)認可主機的文件


? 3爽彤、客戶端配置文件/etc/ssh/ssh_config 主要參數(shù)詳解

客戶端配置文件時登陸別人的ssh使用的

? ? #Host *? ? ? ? //表示連接所有主機

? ? #Port 22? ? ? ? //默認連接端口

? ? #Cipher 3des? ? ? ? //加密時使用的加密機制

? ? #StrictHostKeyChecking ask? ? //嚴格的主機秘鑰檢查 即第一次連接時是否詢問


四、客戶端ssh的使用

1缚陷、ssh的基本語法

ssh [OPTIONS] [user]@server [COMMAND]

-l user: 以指定用戶身份連接至服務器适篙;默認使用本地用戶為遠程登錄時的用戶;

ssh user@server

ssh -l user server

[root@1inux ~]#sshcentos@172.16.66.81The authenticity of host '172.16.66.81 (172.16.66.81)'can't be established.RSA key fingerprint is d6:3b:33:71:32:69:7a:dd:47:c2:49:03:ec:03:a1:5e.

Are you sure you want to continue connecting (yes/no)?

[root@1inux ~]#ssh-l centos172.16.66.81The authenticity of host '172.16.66.81 (172.16.66.81)'can't be established.RSA key fingerprint is d6:3b:33:71:32:69:7a:dd:47:c2:49:03:ec:03:a1:5e.

Are you sure you want to continue connecting (yes/no)?


?? ??? ??? ?-p PORT:指明要連接的端口:

[root@1inux ~]#ssh-p22-l centos172.16.66.81The authenticity of host '172.16.66.81 (172.16.66.81)'can't be established.RSA key fingerprint is d6:3b:33:71:32:69:7a:dd:47:c2:49:03:ec:03:a1:5e.

Are you sure you want to continue connecting (yes/no)?

-X :啟用X11Forwarding,即轉發(fā)X界面的請求箫爷;

-x: 禁用嚷节;

-Y:? 啟用信任的X11Forwarding

2、ssh 基于秘鑰的認證

2.1虎锚、ssh-keygen語法:

ssh-keygen [OPTIONS]

-t {rsa|dsa} 密鑰類型 一般使用rsa

-b # 指明密鑰長度

-f /PATH/TO/OUTPUT_KEYFILE? 指明密鑰文件

-P '' :指明加密密鑰的密碼硫痰,表示使用空密碼

[root@1inux ~]#ssh-keygen-t rsa//生成密鑰對Generating public/private rsa key pair.

Enter fileinwhichto save the key (/root/.ssh/id_rsa): /root/.ssh/id_90//定義保存的密鑰文件名Enter passphrase (emptyforno passphrase)://要求對生成的密鑰對加密,也可以不加密? 輸入兩次回車就OK Enter same passphrase again:

Your identification has been saved in/root/.ssh/id_90.//說明已經(jīng)生成密鑰Your public key has been savedin/root/.ssh/id_90.pub.//說明已經(jīng)生成公鑰The key fingerprint is:

7a:17:b3:e7:6f:54:a1:30:23:62:7c:04:37:10:52:08 root@1inux

The key's randomart image is:+--[ RSA2048]----+|? E..+==+? ? ? ||? ? ..+.o.+? . ||? ? ? . o . + . .||? ? ? ? ? ? .? .||? ? ? ? S o? ? . ||? ? ? .? +? .? ||? ? ? . . o ..? ||? ? ? . . o? .? ||? ? ? ? ? ? .o.? |+-----------------+[root@1inux ~]#

[root@1inux ~]#ls.ssh/id_90? id_90.pub? known_hosts

[root@1inux ~]# -------------------------

也可以指明秘鑰路徑 及密碼

[root@1inux .ssh]#ssh-keygen-t rsa -f /root/.ssh/id_rsa -P''//經(jīng)測試 名字必須為id_rsaGenerating public/private rsa key pair.

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:33:c3:f8:f3:2c:ed:88:cc:db:7a:97:5f:d0:de:ce:d9 root@1inux

The key's randomart image is:+--[ RSA2048]----+|? ? ? ? ? ? ? ? ||? ? ? ? ? ? ? ? ||? ? ? ? ? ? ? ? ||? ? ? o? ? .? ||? ? ? . S? . .? ||? ? ? . +? o . ||? ? ? ? o. .? o .||? ? o oo=+? . oo||? ? ? *+o++..? .E|+-----------------+[

root@1inux ~]#ls.ssh/

id_rsa? id_rsa.pub? id_www? id_www.pub? known_hosts


????????2.2翁都、把公鑰部分復制到要登陸遠程主機的特定用戶(可不同于本地用戶)的家目錄下碍论,追加保存至.ssh 目錄中的authorized_keys文件中;

ssh-copy-id-i /PATH/TO/PUBKEY_FILE [user]@server?

//使用此命令會自動將公鑰復制到目標主機指定用戶的家目錄下的.ssh/authorized_keys文件中

[root@1inux ~]#ssh-copy-id-i /root/.ssh/id_rsa.pub henan@172.16.66.81

henan@172.16.66.81's password: Now try logging into the machine, with"ssh 'henan@172.16.66.81'", and checkin:

? .ssh/authorized_keys

to makesure we haven't added extra keys that you weren't expecting.

[root@1inux ~]#


????????2.3柄慰、驗證:

[root@1inux .ssh]#sshlfs@172.16.66.81

Last login: Wed Apr814:31:522015from172.16.66.90

[lfs@1inux ~]$//可以看到已經(jīng)登錄成功[henan@1inux ~]$whoami

henan

[henan@1inux ~]$ifconfig|grep"172.16.66"

? ? ? ? ? inet addr:172.16.66.81Bcast:172.16.255.255Mask:255.255.0.0


基于密鑰的命令總結

1、[root@1inux .ssh]#ssh-keygen-t rsa -f /root/.ssh/id_rsa -P''

2税娜、[root@1inux ~]#ssh-copy-id-i /root/.ssh/id_rsa.pub henan@172.16.66.813坐搔、sshlfs@172.16.66.81


????????2.4、通過ssh直接執(zhí)行命令

[root@1inux ~]#ssh-l henan172.16.66.81date? ? ? ? ? ? ? ? //由于此用于已經(jīng)基于密鑰認證所以沒有要求輸入密碼

Wed Apr815:01:15CST2015

五敬矩、scp遠程復制工具的使用簡介

語法:

scp [OPTIONS] SRC...DEST

常用選項:

-r: 遞歸復制概行,復制目錄及內(nèi)部文件時使用;

-p: 保存源文件元數(shù)據(jù)信息中的屬主弧岳、屬組及權限凳忙;

-q: 靜默模式

-P PORT: 指明遠程服務器使用的端口;

兩種模式:

PUSH: scp [OPTIONS] /PATH/FROM/SOMEFILE ... user@server:/PATH/TO/DEST

//復制本地文件至遠程主機 (但遠程目錄一定要有寫權限)

PULL: SCP [OPTIONS] user@server:/PATH/FROM/SOMEFILE /PATH/TO/DEST

//遠程目標主機文件至本地

eg1:復制當前主機上的/etc/ssh/sshd_config至172.16.66.90主機的/tmp/aa目錄下

[root@1inux ~]#scp-p /etc/ssh/sshd_config henan@172.16.66.90:/tmp/aa? ?

henan@172.16.66.90's password: sshd_config100%38793.8KB/s00:00


eg2:復制遠程主機/etc/fstab至當前主機當前目錄下

[root@1inux ~]#lsanaconda-ks.cfg? Documents? grub.confinstall.log? ? ? ? Music? ? Public? ? Videos

Desktop? ? ? ? ? Downloads? henan@172.16.66.90install.log.syslog? Pictures? Templates

[root@1inux ~]#scproot@172.16.66.90:/etc/fstab ./root@172.16.66.90's password: fstab100%9210.9KB/s00:00? ?

[root@1inux ~]#lsanaconda-ks.cfg? Documents? fstab? ? ? henan@172.16.66.90install.log.syslog? Pictures? Templates

Desktop? ? ? ? ? Downloads? grub.conf? install.log? ? ? ? Music? ? ? ? ? ? ? Public? ? Videos

六禽炬、sftp的使用

要使用sftp需要編輯/etc/ssh/sshd_config 開啟Subsystem 即:

Subsystem?????? sftp??? /usr/libexec/openssh/sftp-server

語法:

sftp [USER]@server

常用命令:put get

eg:

[root@1inux ~]# sftp root@172.16.66.90

Connecting to 172.16.66.90...

root@172.16.66.90's password:? ? ? ? //輸入密碼

sftp> help? ? ? ? ? ? //可以使用help查看其支持的命令

Available commands:

bye? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Quit sftp

cd path? ? ? ? ? ? ? ? ? ? ? ? ? ? Change remote directory to 'path'

chgrp grp path? ? ? ? ? ? ? ? ? ? Change group of file 'path' to 'grp'

chmod mode path? ? ? ? ? ? ? ? ? ? Change permissions of file 'path' to 'mode'

chown own path? ? ? ? ? ? ? ? ? ? Change owner of file 'path' to 'own'

df [-hi] [path]? ? ? ? ? ? ? ? ? ? Display statistics for current directory or

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? filesystem containing 'path'

exit? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Quit sftp

get [-P] remote-path [local-path]? Download file

help? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Display this help text

lcd path? ? ? ? ? ? ? ? ? ? ? ? ? Change local directory to 'path'

lls [ls-options [path]]? ? ? ? ? ? Display local directory listing

lmkdir path? ? ? ? ? ? ? ? ? ? ? ? Create local directory

ln oldpath newpath? ? ? ? ? ? ? ? Symlink remote file

lpwd? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Print local working directory

ls [-1aflnrSt] [path]? ? ? ? ? ? ? Display remote directory listing

lumask umask? ? ? ? ? ? ? ? ? ? ? Set local umask to 'umask'

mkdir path? ? ? ? ? ? ? ? ? ? ? ? Create remote directory

progress? ? ? ? ? ? ? ? ? ? ? ? ? Toggle display of progress meter

put [-P] local-path [remote-path]? Upload file

pwd? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Display remote working directory

quit? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Quit sftp

rename oldpath newpath? ? ? ? ? ? Rename remote file

rm path? ? ? ? ? ? ? ? ? ? ? ? ? ? Delete remote file

rmdir path? ? ? ? ? ? ? ? ? ? ? ? Remove remote directory

symlink oldpath newpath? ? ? ? ? ? Symlink remote file

version? ? ? ? ? ? ? ? ? ? ? ? ? ? Show SFTP version

!command? ? ? ? ? ? ? ? ? ? ? ? ? Execute 'command' in local shell

!? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Escape to local shell

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Synonym for help

====================================================================================


七涧卵、增強服務端sshd配置指南

1、不要使用默認端口腹尖;(修改默認端口為其他端口)

配置文件:/etc/ssh/sshd_config

Port 22

service sshd restart?? //修改后需要重啟服務

2柳恐、不要使用v1版本協(xié)議:

Protocol 2

3、限制可登陸的用戶 {需要添加}

AllowUsers:允許登陸的用戶白名單 (多個用戶使用空格隔開)

AllowGroups:允許登陸的組的白名單

DenyUsers

DenyGroups

/etc/ssh/sshd_config

# service sshd reload

==》 獲取配置文件詳細信息热幔;【 man sshd_conifg 】

4乐设、設定空閑會話超時時長:

5、利用防火墻設置ssh訪問策略:

限定ssh服務僅允許***服務器分配有限的地址段內(nèi)的主機訪問

6绎巨、僅監(jiān)聽特定的IP地址:

7近尚、使用強密碼策略:

[root@1inuxssh]#tr-dc A-Za-z0-9< /dev/urandom |head-c30|xargsQe6zOmB2sBNpEONVcKhWS8T4bVrcb0

8、使用基于密鑰的認證场勤;

9戈锻、禁止使用空密碼

10介汹、禁止root直接登陸

PermitRootLogin no

11、限制ssh的訪問頻度

12舶沛、做好日志嘹承、經(jīng)常做日志分析

/var/log/secure

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市如庭,隨后出現(xiàn)的幾起案子叹卷,更是在濱河造成了極大的恐慌,老刑警劉巖坪它,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件骤竹,死亡現(xiàn)場離奇詭異,居然都是意外死亡往毡,警方通過查閱死者的電腦和手機蒙揣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來开瞭,“玉大人懒震,你說我怎么就攤上這事∴拖辏” “怎么了个扰?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長葱色。 經(jīng)常有香客問我递宅,道長,這世上最難降的妖魔是什么苍狰? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任办龄,我火速辦了婚禮,結果婚禮上淋昭,老公的妹妹穿的比我還像新娘俐填。我一直安慰自己,他們只是感情好响牛,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布玷禽。 她就那樣靜靜地躺著,像睡著了一般呀打。 火紅的嫁衣襯著肌膚如雪矢赁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天贬丛,我揣著相機與錄音撩银,去河邊找鬼。 笑死豺憔,一個胖子當著我的面吹牛额获,可吹牛的內(nèi)容都是我干的够庙。 我是一名探鬼主播,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼抄邀,長吁一口氣:“原來是場噩夢啊……” “哼耘眨!你這毒婦竟也來了?” 一聲冷哼從身側響起境肾,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤剔难,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后奥喻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體偶宫,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年环鲤,在試婚紗的時候發(fā)現(xiàn)自己被綠了纯趋。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡冷离,死狀恐怖吵冒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情酒朵,我是刑警寧澤桦锄,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站蔫耽,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏留夜。R本人自食惡果不足惜匙铡,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望碍粥。 院中可真熱鬧鳖眼,春花似錦、人聲如沸嚼摩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枕面。三九已至愿卒,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間潮秘,已是汗流浹背琼开。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留枕荞,地道東北人柜候。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓搞动,卻偏偏與公主長得像,于是被迫代替她去往敵國和親渣刷。 傳聞我的和親對象是個殘疾皇子鹦肿,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內(nèi)容