目录
OpenSSH简介
OpenSSH 是SSH(Secure Shell,安全外壳协议,专为远程登录会话和其他网络服务提供安全性的协议)的免费开源实现。OpenSSH提供了服务端后台程序和客户端工具,用来加密远程控制和文件传输过程中的数据。
基本操作
[root@CentOS ~]# systemctl start sshd
启动
[root@CentOS ~]# systemctl status sshd
查询状态
[root@CentOS ~]# systemctl stop sshd
停止
[root@CentOS ~]# systemctl enable sshd
设置开机自启动
[root@CentOS ~]# systemctl disable sshd
禁止开机自启动
[root@CentOS ~]# rpm -qa | grep ssh 查询rpm格式的ssh安装包在安装后会生成什么文件
libssh-0.9.4-2.el8.x86_64
openssh-askpass-8.0p1-5.el8.x86_64
openssh-clients-8.0p1-5.el8.x86_64
libssh-config-0.9.4-2.el8.noarch
openssh-8.0p1-5.el8.x86_64
qemu-kvm-block-ssh-4.2.0-48.module_el8.4.0+783+f8734d30.x86_64
openssh-server-8.0p1-5.el8.x86_64
[root@CentOS ~]# netstat -tlnp | grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1162/sshd
tcp6 0 0 :::22 :::* LISTEN 1162/sshd
OpenSSH服务主配置文件
位于/etc/ssh/sshd_config路径下,默认配置如下
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj 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:/usr/local/sbin:/usr/sbin
# 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_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# This system is following system-wide crypto policy. The changes to
# crypto properties (Ciphers, MACs, ...) will not have any effect here.
# They will be overridden by command-line options passed to the server
# on command line.
# Please, check manual pages for update-crypto-policies(8) and sshd_config(5).
# 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 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 Fedora and may cause several
# problems.
UsePAM yes
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
# It is recommended to use pam_motd in /etc/pam.d/sshd instead of PrintMotd,
# as it is more configurable and versatile than the built-in version.
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#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
配置参数
参数 | 作用 |
---|---|
AcceptEnv | 指定客户端的哪些环境变量会被传递到会话环境中;指令的值是空格分隔的变量名列表,可并列使用多个AcceptEnv达到相同目的 |
AddressFamily | 指定SSH使用的地址族。取值范围是:"any"(默认)、"inet"(仅IPv4)、"inet6"(仅IPv6) |
AllowGroups | 指定允许登录的用户组,组名列表用空格分隔(可使用*和?通配符)。默认允许所有组登录 |
AllowTcpForwarding | 是否允许TCP转发,默认yes |
AllowUsers | 指定允许登录的用户,用户名列表用空格分隔(可使用*和?通配符)。默认允许所有用户登录 |
AuthorizedKeysFile | 存放该用户可以用来登录的RSA/DSA公钥 |
Banner | 将这个指令指定的文件中的内容在用户进行认证前显示给远程用户。默认不显示内容,"none"表示禁用这个特性。 |
ChallengeResponseAuthentication | 是否启用质疑-应答认证。默认no |
Ciphers | 指定允许使用的加密算法,多个算法间用逗号分隔 |
ClientAliveCountMax | 指定服务端未收到任何客户端回应前最多发送多少个"alive"消息,默认3,超过该上限后将强制关闭会话 |
ClientAliveInterval | 指定服务器等待客户端数据的时间(单位:秒) |
Compression | 是否对通信数据进行加密,或延迟到认证成功后再对通信数据加密。 |
DenyGroups | 指定禁止登录的用户组,组名列表用空格分隔(可使用*和?通配符)。默认允许所有组登录 |
DenyUsers | 指定禁止登录的用户,用户名列表用空格分隔(可使用*和?通配符)。默认允许所有用户登录 相关的 allow/deny 指令按照下列优先级处理: |
ForceCommand | 强制执行这里指定的命令而忽略客户端提供的任何命令 |
GatewayPorts | 是否允许远程主机连接本地的转发端口,默认no,服务端默认将远程端口转发到loopback地址(127.0.0.1),阻止客户端连接到其他转发端口,此项设置为yes后会允许远程端口转发到非loopback地址 |
GSSAPIAuthentication | 是否允许使用基于 GSSAPI 的用户认证,默认no |
GSSAPICleanupCredentials | 是否在用户退出登录后自动销毁用户凭证缓存,默认yes |
HostbasedAuthentication | 开启主机认证,默认no |
HostbasedUserNameFromPacketOnly | 是否在开启主机认证的情况下进行反向域名查询,默认no,为yes时表示不进行反向查询 |
HostKey | 主机私钥的路径,如果权限不对可能会启动服务失败 |
IgnoreRhosts | 是否在纯RSA公钥认证或主机认证过程中忽略.rhosts和.shosts文件,默认yes |
IgnoreUserKnownHosts | 是否在纯RSA公钥认证或主机认证过程中忽略用户的~/.ssh/known_hosts文件,默认no |
KerberosAuthentication | 是否使用Kerberos认证 |
KerberosGetAFSToken | 是否在访问的用户家目录前尝试获取一个AFS token,默认no |
KerberosOrLocalPasswd | 是否开启在kerberos认证失败后使用其他认证方式的功能,默认yes |
KerberosTicketCleanup | 是否在用户退出登录后自动销毁用户的ticket,默认yes |
KeyRegenerationInterval | 指定服务器密钥生命周期,为0表示永不重新生成,默认3600,单位:秒 |
ListenAddress | 指定sshd监听的网络地址,默认监听所有地址 |
格式 ListenAddress 主机名 或 IPv4地址 或 IPv6地址 ListenAddress 主机名 或 IPv4地址:端口号 ListenAddress 主机名 或 IPv6地址:端口号 | |
LoginGraceTime | 指定用户认证时限,0表无限制,默认120,单位:秒 |
LogLevel | QUIET,FATAL,ERROR,VERBOSE, DEBUG, DEBUG1,DEBUG2,DEBUG3,默认INFO |
MACs | 指定进行数据校验的消息摘要算法,默认所有,hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-sha1-96,hmac-md5-96 |
Match | 引入条件块,可用条件:User,Group,Host,Address AllowTcpForwarding,Banner,ForceCommand,GatewayPorts,GSSApiAuthentication, |
MaxAuthTries | 指定每个连接最大的认证次数,默认6,认证失败超过该数后会强制断开连接 |
MaxStartups | 指定最大的未认证连接数,默认10,超过该数后不再接受新连接,需要等待先前未认证的连接断开或认证成功 |
PasswordAuthentication | 是否使用基于密码的认证,默认yes |
PermitEmptyPasswords | 是否允许密码为空的用户登录,默认no |
PermitOpen | 指定TCP端口转发允许的目的地,可以使用空格分隔多个转发目标。 |
格式 PermitOpen 主机:端口 PermitOpen any (无限制) | |
PermitRootLogin | 是否允许root登录 |
yes,允许 without-password,禁止使用密码认证登录 forced-commands-only,只有在指定了command选项的情况下才允许使用公钥认证登录 | |
PermitTunnel | 是否允许tun设备转发 |
PermitUserEnvironment | 指定是否允许 sshd处理 ~/.ssh/environment 以及 ~/.ssh/authorized_keys 中的 environment= 选项,默认no |
PidFile | 指定在哪个文件中存放SSH守护进程的进程号,默认为 /var/run/sshd.pid 文件。 |
Port | 指定 sshd(8) 守护进程监听的端口号,默认为 22 。可以使用多条指令监听多个端口。默认监听本机所有网络端口,可通过ListenAddress指定在某个特定端口监听 |
PrintLastLog | 指定 sshd是否在每一次交互式登录时打印最后一位用户的登录时间,默认yes |
PrintMotd | 指定 sshd是否在每一次交互式登录时打印 /etc/motd 文件的内容,默认yes |
Protocol | 指定sshd支持的SSH协议版本号,'1'和'2'表示仅仅支持SSH-1和SSH-2协议;"2,1"表示同时支持SSH-1和SSH-2协议 |
PubkeyAuthentication | 是否允许公钥认证,默认yes |
RhostsRSAAuthentication | 是否使用强可信主机认证,这是通过在RSA认证成功后再检查 ~/.rhosts 或 /etc/hosts.equiv 进行认证的,默认no |
RSAAuthentication | 是否使用纯RSA公钥认证,默认yes |
ServerKeyBits | 指定临时服务器密钥的长度,默认768,单位:位,最小512 |
StrictModes | 指定是否要求sshd在接受连接请求前对用户主目录和相关的配置文件进行宿主和权限检查,默认yes |
Subsystem | 配置一个外部子系统(例如,一个文件传输守护进程)。仅用于SSH-2协议。 值是一个子系统的名字和对应的命令行(含选项和参数)。比如"sft /bin/sftp-server"。 |
SyslogFacility | 指定 sshd将日志消息通过哪个日志子系统(facility)发送。有效值是: DAEMON, USER, AUTH(默认), LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7 |
TCPKeepAlive | 指定系统是否向客户端发送TCP keepalive,默认yes |
UseDNS | 指定sshd是否应该对远程主机名进行反向解析,默认yes |
UseLogin | 是否在交互式会话的登录过程中使用login,默认no |
UsePrivilegeSeparation | 是否让sshd通过创建非特权子进程处理接入请求的方法来进行权限分离,默认yes,认证成功后将以该认证用户的身份创建另一个子进程 |
X11DisplayOffset | 指定X11转发的第一个可用的显示区数字,默认10 |
X11Forwarding | 是否允许X11转发,默认no |
X11UseLocalhost | sshd是否应当将X11转发服务器绑定到本地loopback地址,默认yes |
XAuthLocation | 指定xauth程序的绝对路径,默认/usr/X11R6/bin/xauth |
默认访问
[root@CentOS ~]# vim /etc/ssh/sshd_config
输入:17回车,删除前面的#
Port 22
#重启
[root@CentOS ~]# systemctl restart sshd
Windows环境下使用Xshell访问
Xshell 7 (Build 0090)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.
Type `help' to learn how to use Xshell prompt.
[C:\~]$
Connecting to 192.168.89.128:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Mar 19 22:30:05 2022 from 192.168.89.1
[root@CentOS ~]#
题一
基于密钥认证
用户 | root |
密钥密码 | 114514 |
密钥文件 | id_rsa |
生成密钥
[root@CentOS ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jjwXbS4q5BrosEybeK/Op3KLAmTQrKX6o279nIw+zTw root@CentOS8
The key's randomart image is:
+---[RSA 3072]----+
| o |
|. + |
|.+ |
|oo . |
|+ S o |
|o. .. o + |
|+ooo+ + + . |
|O=B+BE.+ . |
|O%O@+*o |
+----[SHA256]-----+
[root@CentOS ~]# cat /root/.ssh/id_rsa.pub>>/root/.ssh/authorized_keys
[root@CentOS ~]# chmod 600 /root/.ssh/authorized_keys
[root@CentOS ~]# rm -f /root/.ssh/id_rsa.pub
客户端下载密钥
Xshell 7 (Build 0090)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.
Type `help' to learn how to use Xshell prompt.
[C:\~]$
Connecting to 192.168.89.128:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Mar 19 22:51:18 2022 from 192.168.89.1
[root@CentOS8 ~]# sz /root/.ssh/id_rsa
关闭用户认证
[root@CentOS ~]# vim /etc/ssh/sshd_config
输入:70回车,更改为如下
PasswordAuthentication no
#重启
[root@CentOS ~]# systemctl restart sshd
登录验证
选择密钥文件,输入原先设定的密钥密码
Xshell 7 (Build 0090)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.
Type `help' to learn how to use Xshell prompt.
[C:\~]$
Connecting to 192.168.89.128:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sat Mar 19 23:05:57 2022 from 192.168.89.1
[root@CentOS8 ~]#