一、什么是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