配置sshd
要在启动时管理 sshd 服务,请使用 chkconfig 工具:
# /sbin/chkconfig --list sshd sshd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
# /sbin/chkconfig sshd on
# /sbin/chkconfig --list sshd sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
各种配置文件用于定制 sshd 守护进程以进行站点相关操作。
这些描述如下:
ssh 配置文件路径 | 描述 |
---|---|
/etc/ssh/sshd_config | 必需的配置文件。大多数默认值作为注释提供。 |
/etc/ssh/ssh_host_key | 包含用于 SSH 协议版本 1 的主机密钥。仅在使用协议版本 1 时才需要。 |
/etc/ssh/ssh_host_rsa_key/etc/ssh/ssh_host_dsa_key | 包含用于 SSH 协议版本 2 的主机密钥。仅在使用协议版本 2 时才需要。 |
/etc/motd | 成功登录后向 ssh 客户端显示的日期文件消息。 |
~/.hushlogin | 如果文件存在,成功登录后 ssh 不会显示 /etc/motd 文件。 |
/etc/nologin | 如果文件存在,则客户端显示其内容,然后终止会话。忽略 root 登录。 |
~/.ssh/environment | 如果文件存在并且设置了 sshd_config 参数 PermitUserEnvironment,则此文件中的 shell 环境变量将导出到 SSH 会话。 |
~/.ssh/rc/etc/ssh/sshrc | 按照显示的顺序检查文件,并运行找到的第一个文件。如果两者均未找到,则运行 xauth 实用程序。 |
~/.ssh/authorized_keys | 列出用于 RSA 会话身份验证的公钥。 |
~/.ssh/known_hosts/etc/ssh/ssh_known_hosts | 包含所有已知主机的主机公钥。每个用户的文件是自动维护的。可选的全局文件由系统管理员维护。 |
/etc/moduli/etc/ssh/moduli | 与手册页相反,此实现中未使用文件 /etc/moduli。文件 /etc/ssh/moduli 包含用于 "Diffie-Hellman Group Exchange" 的 Diffie-Hellman 组。协议协商。 |
/var/empty/sshd | 在身份验证阶段完成之前的权限分离期间使用的 chroot 目录。该目录由 root 拥有且不可全局写入,通常仅包含一个用于时间和日期显示的 etc/localtime 文件。 |
/etc/hosts.allow/etc/hosts.deny | 由 tcp_wrappers 工具强制执行的访问控制。有关详细信息,请参阅 tcpd。 |
~/.rhosts | 用于 RSA 身份验证或者基于主机的身份验证。用于识别不需要密码交换的登录的用户名/主机名对列表。 |
~/.shosts | 类似于 ~/.rhosts,但被 rlogin 或者 rshd 实用程序忽略。 |
/etc/hosts.equiv | 只要客户端和服务器主机上的用户名匹配,就不需要密码挑战的主机列表。如果一行还包含用户名,则客户端计算机上的该用户可以登录任何服务器帐户,甚至是主机 root 帐户;一般不推荐这样做。 |
/etc/shosts.equiv | 与 /etc/hosts.equiv 文件的处理方式相同,该文件会被 rsh 和 rshd 守护进程忽略。 |
服务控制
要按需管理 sshd 服务,请使用 service 命令或者直接运行 /etc/init.d/sshd 脚本:
# /sbin/service sshd help Usage: /etc/init.d/sshd {start|stop|restart|reload|condrestart|status}
# /etc/init.d/sshd help Usage: /etc/init.d/sshd {start|stop|restart|reload|condrestart|status}
可用的命令有:
命令 | 描述 |
---|---|
start | 启动 sshd 守护进程。 |
stop | 停止 sshd 守护进程。 |
restart | 相当于先停止然后再启动命令序列。 |
reload | 强制 sshd 守护进程在不中断服务的情况下重新读取其配置文件。 |
condrestart | 如果 sshd 守护程序当前正在运行,则这与重新启动命令相同。如果守护程序未运行,则不执行任何操作。通常用于 RPM 包安装以避免启动尚未运行的服务。 |
status | 如果 sshd 守护进程正在运行,请报告其 PID(进程 ID)。如果没有运行,也报告。 |
sshd 守护进程最常由 sshd 服务或者 xinetd(8) 守护进程启动。
直接提供命令行参数是可能的,但通常不会这样做。
相反,可以通过创建 /etc/sysconfig/sshd 文件并定义 ${OPTIONS} 变量来向 sshd(8) 守护进程提供另外的命令行参数:
# Additional command line options for sshd OPTIONS="-q"
此示例将打开安静模式,防止 sshd 记录每个会话的开始、身份验证和结束。
有关可用命令行选项的详细信息,请参阅 sshd 的在线手册页。
sshd服务说明
sshd(Secure Shell Daemon)服务是 Linux 的 OpenSSH 实现的一部分,它提供经过身份验证的端到端加密网络通信。
例如,一台主机上的用户可以在不使用密码的情况下登录到另一个系统,因为 sshd 服务可以配置为使用替代身份验证技术,例如交换共享密钥信息。
OpenSSH 使用客户端/服务器技术。
客户端(例如运行 ssh 程序的用户)发起与目标主机上的 sshd 服务器的连接。
交换 SSH 协议中的消息以向目标主机验证客户端,然后允许或者拒绝远程访问。
如果允许,则构建 TCP/IP 连接以允许客户端与目标系统上的 sshd 守护程序生成的应用程序交换命令和数据。
OpenSSH 工具的好处是所有通信交换都是加密的。
用户名、密码和会话数据等身份验证信息绝不会以未加密的形式传输。
这与专门使用明文传输的 telnet 或者 ftp 等替代方案不同。
openssh-server RPM 包提供了 /usr/sbin/sshd 守护进程及其配置文件。
存在两个版本的 SSH 通信协议,版本 1 和版本 2.
客户端(例如 scp)和 sshd 服务器必须使用相同的协议版本。
sshd 服务器默认支持两个协议版本,但是每个版本都是单独配置的。
默认情况下,客户端和 sshd 守护程序之间的连接是在 TCP/IP 端口 22 上建立的,使用 UDP 或者 TCP 协议。
首先,客户端和服务器必须就加密方法达成一致:sshd 发送支持的密码列表,客户端选择其首选项。
服务器使用 /etc/ssh/ssh_host_key(协议版本 1)或者 /etc/ssh/ssh_host_[rd]sa_key(协议版本 2)之一识别其主机。
这允许客户端检测 IP 欺骗或者中间人攻击。
验证主机 ID 后,客户端会尝试一种或者多种身份验证方法来为用户建立系统访问凭据。
首先尝试公共加密密钥,然后回退到传统的密码挑战。
使用 ssh 客户端,可以通过添加“-v”开关来查看完整的协商,如下所示:
$ ssh -vvv myhost.example.com
sshd 守护程序使用 /etc/pam.d/sshd 文件为其主机确定必要的身份验证规则。
openssh-server RPM 包提供了一个默认文件,对于大多数站点来说已经足够了。
在用户访问通过身份验证后,sshd(8) 守护程序然后启动客户端所需的应用程序,并带有添加到 TCP/IP 会话的标准输入/错误/输出文件(stdin、stderr、stdout)。
sshd(8) 守护程序可用的功能和行为由系统范围的配置 /etc/ssh/sshd_config 文件控制。
默认文件随 RPM 包一起提供,但可以进行编辑以满足本地要求。
例如,为了防止使用 ssh 进行 root 登录,可以更改 sshd_config 设置,如以下代码段所示:
# vi /etc/ssh/sshd_config PermitRootLogin no
配置文件 /etc/ssh/sshd_config
下面是一个配置文件 /etc/ssh/sshd_config的示例。
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy 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 :: 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 # Ciphers and keying #RekeyLimit default none # Logging #SyslogFacility AUTH SyslogFacility AUTHPRIV #LogLevel INFO # Authentication: #LoginGraceTime 2m #PermitRootLogin yes #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 #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 #HostbasedAuthentication no # Change to yes if you don't trust ~/.ssh/known_hosts for # 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 no # 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 #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