通过systemd在启动时启用和禁用sshd

我安装了openssh-server但是我有时希望在启动时默认关闭sshd服务,并且只在需要时从终端启动它。

基于许多其他问题的建议,在我的systemd使用16.04发行版启用和禁用启动服务应该很简单:

 $sudo systemctl disable sshd.service 

这似乎有效。 但是,在此之后我无法再在启动时启用该服务:

 $sudo systemctl enable sshd.service Failed to execute operation: No such file or directory 

即使卸载并重新安装openssh-server也无法解决问题,但是purge也是如此。

我通过systemd禁用sshd ,如何在启动时重新启用sshd

请注意,即使在这种混乱的状态下,我仍然可以通过service ssh [start | stop]`手动启动和停止服务。

sshd服务最初写为ssh.service,sshd.service设置为别名。 查看以下输出的最后一行。

 arryph@localhost:~$ systemctl cat sshd.service # /lib/systemd/system/ssh.service [Unit] Description=OpenBSD Secure Shell server After=network.target auditd.service ConditionPathExists=!/etc/ssh/sshd_not_to_be_run [Service] EnvironmentFile=-/etc/default/ssh ExecStart=/usr/sbin/sshd -D $SSHD_OPTS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartPreventExitStatus=255 Type=notify [Install] WantedBy=multi-user.target Alias=sshd.service 

因此,当启用ssh.service时,我们可以将其称为sshd.service 。 但是当您禁用sshd.service并重新启动时, ssh.service不再被加载,因此在此情况下您无法将其作为sshd.service引用。 你必须引用的是ssh.service 。 因此,如果您运行sudo systemctl enable ssh.service ,它将成功启用ssh.service (别名为sshd.service )。