【Linux操作系统】Linux配置OpenSSH服务器步骤记录
1.安装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_keyHostKey /etc/ssh/ssh_host_ecdsa_key#In FIPS mode Ed25519 keys are not supported, please comment out the next lineHostKey /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 AUTHSyslogFacility AUTHPRIV#LogLevel INFO# Authentication:#LoginGraceTime 2mPermitRootLogin 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_keysAuthorizedKeysFile .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 noPasswordAuthentication yes# Change to no to disable s/key passwords#ChallengeResponseAuthentication yesChallengeResponseAuthentication no# Kerberos options#KerberosAuthentication no#KerberosOrLocalPasswd yes#KerberosTicketCleanup yes#KerberosGetAFSToken no#KerberosUseKuserok yes# GSSAPI optionsGSSAPIAuthentication yesGSSAPICleanupCredentials 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 RHEL and may cause several# problems.UsePAM yes#AllowAgentForwarding yes#AllowTcpForwarding yes#GatewayPorts noX11Forwarding 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 variablesAcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGESAcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENTAcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGEAcceptEnv XMODIFIERS# override default of no subsystemsSubsystem 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 serverPort 22ListenAddress 192.168.0.2PermitEmptyPasswords noDenyUsers lisi
出现问题:E325: ATTENTION Found a swap file by the name "/etc/ssh/.sshd_config.swp"
查询后发现是上次修改内容没有保存,意外退出造成的,生成了一个.swp文件。解决方法:
rm -f /etc/ssh/.sshd_config.swp
删掉这个备份文件,然后重新编辑
命令行输入
sudo gedit /etc/ssh/sshd_config
3.启动sshd服务
Job for sshd.service failed because the control process exited with error code. See "systemctl status sshd.service" and "journalctl -xe" for details.
Subject: sshd.service 单元已开始启动
-- Defined-By: systemd
-- Support: https://access.redhat.com/support
--
-- sshd.service 单元已开始启动。
10月 20 17:56:13 rhel sshd[32022]: error: Bind to port 22 on 192.168.0.2 failed>
10月 20 17:56:13 rhel sshd[32022]: fatal: Cannot bind any address.
10月 20 17:56:13 rhel systemd[1]: sshd.service: Main process exited, code=exite>
10月 20 17:56:13 rhel systemd[1]: sshd.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: https://access.redhat.com/support
--
-- The unit sshd.service has entered the 'failed' state with result 'exit-code'.
10月 20 17:56:13 rhel systemd[1]: Failed to start OpenSSH server daemon.
-- Subject: sshd.service 单元已失败
-- Defined-By: systemd
-- Support: https://access.redhat.com/support
--
-- sshd.service 单元已失败。
--
-- 结果为“failed”。
systemctl status sshd.service
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset>
Active: activating (auto-restart) (Result: exit-code) since Sun 2024-10-20 1>
Docs: man:sshd(8)
man:sshd_config(5)
Process: 33661 ExecStart=/usr/sbin/sshd -D $OPTIONS $CRYPTO_POLICY (code=exit>
Main PID: 33661 (code=exited, status=255)
10月 20 17:59:02 rhel systemd[1]: sshd.service: Main process exited, code=exite>
10月 20 17:59:02 rhel systemd[1]: sshd.service: Failed with result 'exit-code'.
10月 20 17:59:02 rhel systemd[1]: Failed to start OpenSSH server daemon.
重装能解决100%的问题。
5.PuTTY显示中文字符
设置为UTF-8