关于sshd无法正常启动的解决方案

本文讲述了作者在遇到SSH服务故障后,如何重新安装openssh-server,处理dpkg错误,迁移配置文件,解决用户权限问题以及设置ssh配置的过程,最终成功启动并配置SSH服务。
摘要由CSDN通过智能技术生成

前段时间ssh突然坏了 按照网上的方法用了好几个命令都没解决

想着重新install一下 openssh-server 结果失效了 报了 dpkg 处理软件包出现错误 (--configure) 之类的 按照网上的方法 把info替换成了info_bak 然后重新创建了info文件夹 apt update 和 强制安装了一下 再把新安装的内容复制到旧安装的 info_bak 里 删除info之后迁移 info_bak 到 info 具体命令如下

sudo su                 

mv /var/lib/dpkg/info     /var/lib/dpkg/info_bak

mkdir /var/lib/dpkg/info

apt-get update && apt-get -f install 

mv /var/lib/dpkg/info/*        /var/lib/dpkg/info_bak/

rm -rf /var/lib/dpkg/info

mv /var/lib/dpkg/info_bak /var/lib/dpkg/info

来源: 安全验证 - 知乎

更改完之后 apt正常安装openssh-server了 然后因为改了 sshd_config 文件 从网上找了个默认的配置文件放到下面  使用命令 vi /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
#RSAAuthentication yes
PubkeyAuthentication 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

替换完成之后 报了个新错误

$ sudo sshd -T
Privilege separation user sshd does not exit

意思是没有找到sshd这个用户

使用命令 `id sshd` 也是提示查无此用户

sudo useradd sshd // 添加用户

然后使用了以上的命令添加了一下用户

报了个新的错误

$ sudo sshd -T
Missing privilege separtion directory: /run/sshd

这个就简单了 说没有文件夹 那么就建一个即可 

sudo mkdir /run/sshd

然后再试试 `sudo sshd -T`

后面直接启动 ssh 即可

service ssh start // 启动ssh
service ssh status

看到状态是 active

启动成功

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常是由于SSH服务器没有开启或没有安装导致的。在Ubuntu 16 (和其他一些Linux系统) 中,SSH服务的名字是sshd.service而不是ssh.service。因此,当你尝试重新启动sshd.service时,会出现"unit ssh.service not found"的错误信息。为了解决这个问题,你可以采取以下步骤: 1. 首先,确认你已经安装了OpenSSH服务器。你可以使用以下命令来检查: ``` sudo apt-get install openssh-server ``` 2. 确认OpenSSH服务器正在运行。你可以使用以下命令来检查: ``` sudo systemctl status sshd.service ``` 如果服务运行正常,你应该会看到"active (running)"的状态。 3. 如果OpenSSH服务器没有安装,可以使用以下命令来安装: ``` sudo apt-get install openssh-server ``` 4. 如果OpenSSH服务器已经安装但没有运行,你可以使用以下命令来启动它: ``` sudo systemctl start sshd.service ``` 5. 最后,你可以使用以下命令来重新启动OpenSSH服务器: ``` sudo systemctl restart sshd.service ``` 确保不要使用"ssh.service"作为服务的名称,而是使用正确的"sshd.service"。 希望这个解决方案可以帮助你解决问题。如果还有其他疑问,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Xshell连接Ubuntu虚拟机失败](https://blog.csdn.net/qq_43759079/article/details/106351783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [kali linux系统打开ssh服务](https://blog.csdn.net/weixin_47152322/article/details/123783014)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值