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命令:可以看到目前登入系统的用户有哪些人,以及他们正在执行的程序,此命令要常用。









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