Centos 7 的防火牆和ssh連接

Centos 7 的防火牆和ssh連接

Centos 7 firewall :

1、firewalld的基本使用

啓動: systemctl start firewalld
關閉: systemctl stop firewalld
查看狀態: systemctl status firewalld 
開機禁用  : systemctl disable firewalld
開機啓用  : systemctl enable firewalld
 

1、iptables的基本使用

啓動: service iptables start
關閉: service iptables stop
查看狀態: service iptables status
開機禁用  : chkconfig iptables off
開機啓用  : chkconfig iptables on
 

SSH的英文全稱是Secure SHell。通過使用SSH,你可以把所有傳輸的數據進行加密,這樣“中間人”這種攻擊方式就不可能實現了,而且也能夠防止DNS和IP欺騙。還有一個額外的好處就是傳輸的數據是經過壓縮的,所以可以加快傳輸的速度。SSH有很多功能,它既可以代替telnet,又可以爲ftp、pop、甚至ppp提供一個安全的“通道”。

SSH在Linux中的服務是sshd,安裝openssh後纔可開啓。CentOS 7 安裝後默認情況下是不啓動sshd服務,即無法通過ssh服務遠程連接。
首先查看系統是否安裝openssh,一般情況想都是默認安裝了,

[root@localhost ~]# rpm -qa | grep ssh
libssh2-1.4.3-10.el7.x86_64
openssh-server-6.6.1p1-22.el7.x86_64
openssh-clients-6.6.1p1-22.el7.x86_64
openssh-6.6.1p1-22.el7.x86_64

如果沒有安裝可以通過yum在線安裝。

[root@localhost ~]# yum install openssh

手動設置啓動ssh服務

1、先檢查確認有沒有安裝ssh-server服務器,輸入命令:ps –e|grep ssh
2、在CentOS命令區輸入:yum install openssh-server
3、啓動、關閉、重啓命令:
開啓ssh服務:service sshd start

關閉ssh服務:service sshd stop

重啓ssh服務:service sshd restart
4、開機自啓動相關設置:
SSH服務開機自動啓動:chkconfigsshd on

取消開機自啓動:chkconfig sshd off

開啓服務後,檢查服務狀態:service sshd status

#       $OpenBSD: sshd_config,v 1.73 2005/12/06 22:38:28 reyk Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

 

#############1. 關於 SSH Server 的整體設定##############
#Port 22    
##port用來設置sshd監聽的端口,爲了安全起見,建議更改默認的22端口爲5位以上陌生端口
#Protocol 2,1
Protocol 2
##設置協議版本爲SSH1或SSH2,SSH1存在漏洞與缺陷,選擇SSH2
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress用來設置sshd服務器綁定的IP地址
##監聽的主機適配卡,舉個例子來說,如果您有兩個 IP, 分別是 192.168.0.11192.168.2.20 ,那麼只想要
###開放 192.168.0.11 時,就可以設置爲:ListenAddress 192.168.0.11
####表示只監聽來自 192.168.0.11 這個 IP 的SSH聯機。如果不使用設定的話,則預設所有接口均接受 SSH

#############2. 說明主機的 Private Key 放置的檔案##########                 
#ListenAddress ::
##HostKey用來設置服務器祕鑰文件的路徑
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
##設置SSH version 1 使用的私鑰

# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
##設置SSH version 2 使用的 RSA 私鑰

#HostKey /etc/ssh/ssh_host_dsa_key
##設置SSH version 2 使用的 DSA 私鑰


#Compression yes      
##設置是否可以使用壓縮指令

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
##KeyRegenerationInterval用來設置多長時間後系統自動重新生成服務器的祕鑰,
###(如果使用密鑰)。重新生成祕鑰是爲了防止利用盜用的密鑰解密被截獲的信息。

#ServerKeyBits 768
##ServerKeyBits用來定義服務器密鑰的長度
###指定臨時服務器密鑰的長度。僅用於SSH-1。默認值是 768(位)。最小值是 512 。


# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
##SyslogFacility用來設定在記錄來自sshd的消息的時候,是否給出“facility code”

#LogLevel INFO
##LogLevel用來設定sshd日誌消息的級別


#################3.安全認證方面的設定################
#############3.1、有關安全登錄的設定###############
# Authentication:
##限制用戶必須在指定的時限內認證成功,0 表示無限制。默認值是 120 秒。

#LoginGraceTime 2m
##LoginGraceTime用來設定如果用戶登錄失敗,在切斷連接前服務器需要等待的時間,單位爲妙

#PermitRootLogin yes
##PermitRootLogin用來設置能不能直接以超級用戶ssh登錄,root遠程登錄Linux很危險,建議註銷或設置爲no

#StrictModes yes
##StrictModes用來設置ssh在接收登錄請求之前是否檢查用戶根目錄和rhosts文件的權限和所有權,建議開啓
###建議使用默認值"yes"來預防可能出現的低級錯誤。

#RSAAuthentication yes
##RSAAuthentication用來設置是否開啓RSA密鑰驗證,只針對SSH1

#PubkeyAuthentication yes
##PubkeyAuthentication用來設置是否開啓公鑰驗證,如果使用公鑰驗證的方式登錄時,則設置爲yes

#AuthorizedKeysFile     .ssh/authorized_keys
##AuthorizedKeysFile用來設置公鑰驗證文件的路徑,與PubkeyAuthentication配合使用,默認值是".ssh/authorized_keys"。
###該指令中可以使用下列根據連接時的實際情況進行展開的符號: %% 表示'%'、%h 表示用戶的主目錄、%u 表示該用戶的用戶名
####經過擴展之後的值必須要麼是絕對路徑,要麼是相對於用戶主目錄的相對路徑。

 

#############3.2、安全驗證的設定###############
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
##是否使用強可信主機認證(通過檢查遠程主機名和關聯的用戶名進行認證)。僅用於SSH-1。
###這是通過在RSA認證成功後再檢查 ~/.rhosts 或 /etc/hosts.equiv 進行認證的。出於安全考慮,建議使用默認值"no"。

# similar for protocol version 2
#HostbasedAuthentication no
##這個指令與 RhostsRSAAuthentication 類似,但是僅可以用於SSH-2。

# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication

#IgnoreUserKnownHosts no
##IgnoreUserKnownHosts用來設置ssh在進行RhostsRSAAuthentication安全驗證時是否忽略用戶的“/$HOME/.ssh/known_hosts”文件
# Don't read the user's ~/.rhosts and ~/.shosts files

#IgnoreRhosts yes
##IgnoreRhosts用來設置驗證的時候是否使用“~/.rhosts”和“~/.shosts”文件

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
##PasswordAuthentication用來設置是否開啓密碼驗證機制,如果用密碼登錄系統,則設置yes

#PermitEmptyPasswords no
#PermitEmptyPasswords用來設置是否允許用口令爲空的賬號登錄系統,設置no

#PasswordAuthentication yes
##是否允許使用基於密碼的認證。默認爲"yes"。
PasswordAuthentication yes

# Change to no to disable s/key passwords
##設置禁用s/key密碼
#ChallengeResponseAuthentication yes
##ChallengeResponseAuthentication 是否允許質疑-應答(challenge-response)認證
ChallengeResponseAuthentication no

 

########3.3、與 Kerberos 有關的參數設定,指定是否允許基於Kerberos的用戶認證########
#Kerberos options
#KerberosAuthentication no
##是否要求用戶爲PasswdAuthentication提供的密碼必須通過Kerberos KDC認證,要使用Kerberos認證,
###服務器必須提供一個可以校驗KDC identity的Kerberos servtab。默認值爲no

#KerberosOrLocalPasswd yes
##如果Kerberos密碼認證失敗,那麼該密碼還將要通過其他的的認證機制,如/etc/passwd
###在啓用此項後,如果無法通過Kerberos驗證,則密碼的正確性將由本地的機制來決定,如/etc/passwd,默認爲yes

#KerberosTicketCleanup yes
##設置是否在用戶退出登錄是自動銷燬用戶的ticket

#KerberosGetAFSToken no
##如果使用AFS並且該用戶有一個Kerberos 5 TGT,那麼開啓該指令後,
###將會在訪問用戶的家目錄前嘗試獲取一個AFS token,並嘗試傳送 AFS token 給 Server 端,默認爲no

 

####3.4、與 GSSAPI 有關的參數設定,指定是否允許基於GSSAPI的用戶認證,僅適用於SSH2####
##GSSAPI 是一套類似 Kerberos 5 的通用網絡安全系統接口。
###如果你擁有一套 GSSAPI庫,就可以通過 tcp 連接直接建立 cvs 連接,由 GSSAPI 進行安全鑑別。

# GSSAPI options
#GSSAPIAuthentication no
##GSSAPIAuthentication 指定是否允許基於GSSAPI的用戶認證,默認爲no

GSSAPIAuthentication yes
#GSSAPICleanupCredentials yes
##GSSAPICleanupCredentials 設置是否在用戶退出登錄是自動銷燬用戶的憑證緩存
GSSAPICleanupCredentials yes

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# ChallengeResponseAuthentication=no
#UsePAM no
##設置是否通過PAM驗證
UsePAM yes

# Accept locale-related environment variables
##AcceptEnv 指定客戶端發送的哪些環境變量將會被傳遞到會話環境中。
###[注意]只有SSH-2協議支持環境變量的傳遞。指令的值是空格分隔的變量名列表(其中可以使用'*''?'作爲通配符)。
####也可以使用多個 AcceptEnv 達到同樣的目的。需要注意的是,有些環境變量可能會被用於繞過禁止用戶使用的環境變量。
#####由於這個原因,該指令應當小心使用。默認是不傳遞任何環境變量。

AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL
AllowTcpForwarding yes

##AllowTcpForwarding設置是否允許允許tcp端口轉發,保護其他的tcp連接

#GatewayPorts no
##GatewayPorts 設置是否允許遠程客戶端使用本地主機的端口轉發功能,出於安全考慮,建議禁止

 

#############3.5、X-Window下使用的相關設定###############

#X11Forwarding no
##X11Forwarding 用來設置是否允許X11轉發
X11Forwarding yes

#X11DisplayOffset 10
##指定X11 轉發的第一個可用的顯示區(display)數字。默認值是 10 。
###可以用於防止 sshd 佔用了真實的 X11 服務器顯示區,從而發生混淆。
X11DisplayOffset 10

#X11UseLocalhost yes

 

#################3.6、登入後的相關設定#################

#PrintMotd yes
##PrintMotd用來設置sshd是否在用戶登錄時顯示“/etc/motd”中的信息,可以選在在“/etc/motd”中加入警告的信息

#PrintLastLog yes
#PrintLastLog 是否顯示上次登錄信息

#TCPKeepAlive yes
##TCPKeepAlive 是否持續連接,設置yes可以防止死連接
###一般而言,如果設定這項目的話,那麼 SSH Server 會傳送 KeepAlive 的訊息給 Client 端,以確保兩者的聯機正常!
####這種消息可以檢測到死連接、連接不當關閉、客戶端崩潰等異常。在這個情況下,任何一端死掉後, SSH 可以立刻知道,而不會有殭屍程序的發生!

#UseLogin no
##UseLogin 設置是否在交互式會話的登錄過程中使用。默認值是"no"。
###如果開啓此指令,那麼X11Forwarding 將會被禁止,因爲login不知道如何處理 xauth cookies 。
####需要注意的是,在SSH底下本來就不接受 login 這個程序的登入,如果指UsePrivilegeSeparation ,那麼它將在認證完成後被禁用。
UserLogin no       

#UsePrivilegeSeparation yes
##UsePrivilegeSeparation 設置使用者的權限
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no

#UseDNS yes
##UseDNS是否使用dns反向解析

#PidFile /var/run/sshd.pid

#MaxStartups 10
##MaxStartups 設置同時允許幾個尚未登入的聯機,當用戶連上ssh但並未輸入密碼即爲所謂的聯機,
###在這個聯機中,爲了保護主機,所以需要設置最大值,預設爲10個,而已經建立聯機的不計算入內,
####所以一般5個即可,這個設置可以防止惡意對服務器進行連接

#MaxAuthTries 6
##MaxAuthTries 用來設置最大失敗嘗試登陸次數爲6,合理設置辭職,可以防止攻擊者窮舉登錄服務器
#PermitTunnel no

 

############3.7、開放禁止用戶設定############

#AllowUsers<用戶名1> <用戶名2> <用戶名3> ...
##指定允許通過遠程訪問的用戶,多個用戶以空格隔開

#AllowGroups<組名1> <組名2> <組名3> ...
##指定允許通過遠程訪問的組,多個組以空格隔開。當多個用戶需要通過ssh登錄系統時,可將所有用戶加入一個組中。

#DenyUsers<用戶名1> <用戶名2> <用戶名3> ...
##指定禁止通過遠程訪問的用戶,多個用戶以空格隔開

#DenyGroups<組名1> <組名2> <組名3> ...
##指定禁止通過遠程訪問的組,多個組以空格隔開。

# no default banner path
#Banner /some/path

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server
ClientAliveInterval 3600
ClientAliveCountMax 0
View Code

 

[root@localhost ~]# vi /etc/ssh/sshd_config

# $OpenBSD: sshd_config,v 1.93 2014/01/10 05:59:19 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

# The default requires explicit activation of protocol 1
#Protocol 2

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024

# Ciphers and keying
#RekeyLimit default none

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile      .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes

# GSSAPI options
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no
#GSSAPIEnablek5users no

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
# WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
# problems.
UsePAM yes

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation sandbox          # Default for new installations.
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS

# override default of no subsystems
Subsystem       sftp    /usr/libexec/openssh/sftp-server

# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       PermitTTY no
#       ForceCommand cvs server
View Code

 

修改端口的時候需要添加到防火牆的控制中,否則無法使用ssh連接。

[root@localhost ~]# semanage port -l | grep ssh #查看當前ssh服務監聽的端口
ssh_port_t tcp 22
[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 8090 #增加監聽端口8090

[root@localhost ~]# semanage port -l | grep ssh
ssh_port_t tcp 8090,22

semanage只是端口工具,修改防火牆只能使用firewall-cmd

[root@localhost ssh]# yum provides firewall-cmd #查找防火牆工具所在的包
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

  • base: mirror.bit.edu.cn
  • extras: mirrors.btte.net
  • updates: mirrors.btte.net
    firewalld-0.3.9-14.el7.noarch : A firewall daemon with D-BUS interface providing a dynamic firewall
    Repo : base
    Matched from:
    Filename : /usr/bin/firewall-cmd

[root@localhost ssh]# yum -y install firewalld #安裝防火牆工具

[root@localhost ssh]# systemctl start firewalld #啓動防火牆服務

[root@localhost ssh]# systemctl status firewalld #查看防火牆狀態
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2016-09-17 04:22:15 CST; 15s ago
Main PID: 16979 (firewalld)
CGroup: /system.slice/firewalld.service
└─16979 /usr/bin/python -Es /usr/sbin/firewalld –nofork –nopid
Sep 17 04:22:14 localhost systemd[1]: Starting firewalld - dynamic firewall daemon…
Sep 17 04:22:15 localhost systemd[1]: Started firewalld - dynamic firewall daemon.
[root@localhost ssh]# firewall-cmd –zone=public –add-port=8090/tcp –permanent #防火牆中允許8090端口通過
success

[root@localhost ssh]# semanage port -m -t ssh_port_t -p tcp 8090 #將ssh服務修改爲8090端口
[root@localhost ssh]# firewall-cmd –zone=public –remove-port=22/tcp –permanent #刪除22端口
success
[root@localhost ssh]# firewall-cmd –reload #重新加載防火牆服務配置
success

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章