zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    greenplum日常维护手册
    Android UI界面基本属性 大全
    Listview 选项按下去黑了所有按钮的解决方法 ——android:cacheColorHint=“#00000000”
    【转】Android应用程序模块详解
    android退出有多个activity的应用
    启动模式"singleTask"和FLAG_ACTIVITY_NEW_TASK具有不同的行为!
    Android 按两次back键退出 效率最高版
    【转】跑马灯效果
    Sundy笔记__Git版本控制
    如果你想用对话框代替一个activity的话,可以设置activity的主题属性
  • 原文地址:https://www.cnblogs.com/cainiao-chuanqi/p/12863057.html
Copyright © 2011-2022 走看看