Skip to content

sshd

sshd 是 SSH daemon(SSH 守护进程)的缩写,是 SSH 协议的服务器端实现,负责为远程用户提供安全的登录和命令执行环境,是现代Linux系统远程管理的基础服务。

主要功能

  • 提供安全的远程登录和其他网络服务
  • 作为守护进程运行,监听SSH客户端连接请求
  • 处理认证、授权、加密和数据传输

配置文件

文件路径

文件路径说明
/etc/ssh/sshd_config主配置文件
/etc/ssh/ssh_config客户端配置文件
/etc/ssh/ssh_host_rsa_key主机密钥文件
/etc/ssh/ssh_host_ecdsa_key主机密钥文件
/etc/ssh/ssh_host_ed25519_key主机密钥文件

配置参数

bash
Include /etc/ssh/sshd_config.d/*.conf # 包含sshd_config.d目录下的所有配置文件 
Port 22  # 指定端口
AddressFamily any  # 指定地址族
ListenAddress 0.0.0.0  # 指定监听地址

# 指定主机密钥文件
HostKey /etc/ssh/ssh_host_rsa_key 
HostKey /etc/ssh/ssh_host_ecdsa_key  
HostKey /etc/ssh/ssh_host_ed25519_key 

RekeyLimit default none  # 指定RekeyLimit
SyslogFacility AUTH # 指定日志级别
LogLevel INFO # 指定日志级别
LoginGraceTime 2m # 指定登录时间

# 指定禁止root用户登录
PermitRootLogin prohibit-password 

StrictModes yes # 指定严格模式
MaxAuthTries 6 # 指定最大认证尝试次数
MaxSessions 10 # 指定最大会话数

# 指定公钥认证
PubkeyAuthentication yes 

# 指定授权密钥文件
AuthorizedKeysFile     .ssh/authorized_keys .ssh/authorized_keys2 # 指定授权密钥文件
AuthorizedPrincipalsFile none # 指定授权主体文件
AuthorizedKeysCommand none # 指定授权密钥命令
AuthorizedKeysCommandUser nobody # 指定授权密钥命令用户
HostbasedAuthentication no # 指定主机密钥文件
IgnoreUserKnownHosts no  # 指定忽略用户主机密钥文件
IgnoreRhosts yes  # 指定忽略用户主机密钥文件
PasswordAuthentication yes # 指定密码认证
PermitEmptyPasswords no # 指定空密码


KbdInteractiveAuthentication no # 启用挑战-响应密码(注意与某些PAM模块和线程的问题)
KerberosAuthentication no # 禁用Kerberos认证
KerberosOrLocalPasswd yes # 指定Kerberos或本地密码
KerberosTicketCleanup yes # 指定Kerberos票据清理
KerberosGetAFSToken no # 指定Kerberos获取AFS令牌
GSSAPIAuthentication no # 禁用GSSAPI认证
GSSAPICleanupCredentials yes # 清理GSSAPI凭证
GSSAPIStrictAcceptorCheck yes # 严格接受者检查
GSSAPIKeyExchange no # 禁用GSSAPI密钥交换


# PAM认证, 默认是 yes
# no: SSH使用内置的认证机制, 即基本的密码和公钥认证
# yes: 使用PAM复杂的认证策略
UsePAM no


AllowAgentForwarding yes # 允许代理转发
AllowTcpForwarding yes # 允许TCP转发
GatewayPorts no  # 禁用网关端口
X11Forwarding yes  # 允许X11转发
X11DisplayOffset 10 # 指定X11显示偏移
X11UseLocalhost yes # 指定X11使用本地主机
PermitTTY yes # 指定PermitTTY
PrintMotd no # 指定PrintMotd
PrintLastLog yes # 指定PrintLastLog
TCPKeepAlive yes # 指定TCPKeepAlive
PermitUserEnvironment no # 指定PermitUserEnvironment
Compression delayed # 指定Compression
ClientAliveInterval 0 # 指定ClientAliveInterval
ClientAliveCountMax 3 # 指定ClientAliveCountMax
UseDNS no # 指定UseDNS
PidFile /run/sshd.pid # 指定PidFile
MaxStartups 10:30:100 # 指定MaxStartups
PermitTunnel no # 指定PermitTunnel
ChrootDirectory none # 指定ChrootDirectory
VersionAddendum none # 指定VersionAddendum
Banner none  # 指定Banner
AcceptEnv LANG LC_*  # 允许客户端传递环境变量

# 指定sftp-server
Subsystem       sftp    /usr/lib/openssh/sftp-server  

X11Forwarding no # 禁用X11转发  
AllowTcpForwarding no # 禁用TCP转发
PermitTTY no # 禁用TTY
ForceCommand cvs server # 指定强制命令

启动服务

bash
systemctl start sshd # 启动服务
systemctl enable sshd # 设置开机自启动
systemctl status sshd # 查看服务状态
systemctl restart sshd # 重启服务

停止服务

bash
systemctl stop sshd # 停止服务
systemctl disable sshd # 禁止开机自启动