SSH學習筆錄

配置ssh服務,實現:
1、只監聽本地址內網地址
2、允許root用戶ssh直接登陸
3、端口使用2222
4、禁用DNS訪問

一、ssh配置詳解: vim   /etc/ssh/sshd_config

Port 2222   #端口
#AddressFamily any
ListenAddress  xx.xx.xx.xx   #監聽的主機適配器
#ListenAddress ::

# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1
Protocol 2   #SSH協議版本

# 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私鑰

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

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
LogLevel INFO  #登錄記錄的等級

# Authentication:
#LoginGraceTime 2m  #在多久時間沒成功連接上SSH server就強迫斷線
PermitRootLogin yes  #是否允許root登入
#StrictModes yes    #是否讓sshd去檢查用戶家目錄或相關的權限數據
#MaxAuthTries 6
#MaxSessions 10

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs 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   #是否取消使用~/.ssh/.rhosts來做認證


# 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 no
GSSAPIAuthentication yes   #服務器端啓用了GSSAPI。登陸的時候客戶端需要對服務器端的IP地址進行反解析,如果服務器的IP地址沒有配置PTR記錄,
那麼就容易在這裏卡住了。
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no

UsePAM yes  #利用PAM管理使用者的認證

# 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

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes    #登入後是否顯示出一些信息
PrintLastLog yes  #顯示上次登錄的信息
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes  #是否權限較低的程序來提供用戶操作
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
UseDNS no    #是否使用DNS去反查客戶端的主機名
#UseDNS 選項打開狀態下,當客戶端試圖登錄SSH服務器時,服務器端先根據客戶端的IP地址進行DNS PTR反向查詢出客戶端的主機名,然後根據查詢出的>客戶端主機名進行DNS正向A記錄查詢,驗證與其原始IP地址是否一致,這是防止客戶端欺騙的一種措施,但一般我們的是動態IP不會有PTR記錄,打開這個
選項不過是在白白浪費時間而已,不如將其關閉。

#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100  #同時允許幾個尚未登錄的聯機畫面
#PermitTunnel no
#ChrootDirectory none
# no default banner path
#Banner none

二、特別注意:

1、ListenAddress:監聽主機地址,通過ifconfig可以查看本主機的網絡ip,listenaddress綁定其中的一個IP,其中127.0.0.1爲本地測試,不宜使用,否則,除了本地外,將無法訪問。

2、通過/etc/hosts.allow文件可以指定允許訪問的網址或者網段,通過/etc/hosts.deny文件可以指定不許訪問的網址或網段。

vim  /etc/hosts.allow       vim /etc/hosts.deny



當hosts.allow與hosts.deny衝突時,以hosts.allow爲準。


3、常見命令:

netstat命令:

-a (all)顯示所有選項,默認不顯示LISTEN相關
-t (tcp)僅顯示tcp相關選項
-u (udp)僅顯示udp相關選項
-n 拒絕顯示別名,能顯示數字的全部轉化成數字,即禁用域名解析功能。
-l 僅列出有在 Listen (監聽) 的服務狀態

-p 顯示建立相關鏈接的程序名
-r 顯示路由信息,路由表
-e 顯示擴展信息,例如uid等
-s 按各個協議進行統計
-c 每隔一個固定時間,執行該netstat命令。


w命令:可以看到目前登入系統的用戶有哪些人,以及他們正在執行的程序,此命令要常用。









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