如何在 FreeBSD 上启用 SSH

OpenSSH 服务器 在 FreeBSD 上默认安装。 但是在新的 FreeBSD 系统上,不允许远程 SSH 登录。 您必须自己启用 SSH 访问才能通过 SSH 从网络上的其他系统访问您的 FreeBSD 服务器。 这个简短的指南将引导您完成在 FreeBSD 上配置和启用 SSH 的步骤。 下面提供的所有步骤都在新安装的 FreeBSD 12.2 系统上进行了测试。

在 FreeBSD 上启用 SSH

如果您尚未在系统上创建任何普通用户,请先创建一个。

为了 example,我要创建一个用户名为 ostechnix 使用命令:

# adduser

系统将提示您回答几个问题,例如用户名、全名、登录组名、登录类、默认 shell 等。大多数情况下,默认值就可以了。 按 ENTER 键接受默认值并输入密码两次。

Username: ostechnix
Full name: Ostechnix
Uid (Leave empty for default): 
Login group [ostechnix]: 
Login group is ostechnix. Invite ostechnix into other groups? []: 
Login class [default]: 
Shell (sh csh tcsh nologin) [sh]: 
Home directory [/home/ostechnix]: 
Home directory permissions (Leave empty for default): 
Use password-based authentication? [yes]: 
Use an empty password? (yes/no) [no]: 
Use a random password? (yes/no) [no]: 
Enter password: 
Enter password again: 
Lock out the account after creation? [no]: 
Username : ostechnix
Password : *****
Full Name : Ostechnix
Uid : 1001
Class : 
Groups : ostechnix 
Home : /home/ostechnix
Home Mode : 
Shell : /bin/sh
Locked : no
OK? (yes/no): yes
adduser: INFO: Successfully added (ostechnix) to the user database.
Add another user? (yes/no): no
Goodbye!

在 Freebsd 中创建普通用户

接下来,编辑 /etc/rc.conf 文件:

# vi /etc/rc.conf

在末尾添加以下行:

sshd_enable="YES"

在 Freebsd 12 上启用 SSH 访问

ESC键 并输入 :wq 保存并退出文件。

最后,启动 SSH 服务:

# /etc/rc.d/sshd start

在 freebsd 上启动 ssh 服务

在 freebsd 上启动 ssh 服务

完毕! 现在您可以通过 SSH 从网络上的任何系统访问您的 FreeBSD 服务器,如下所示。

$ ssh [email protected]

代替 [email protected] 在上面的命令中使用您的用户名和 IP 地址。

您可能已经知道,当您第一次通过 SSH 连接到 FreeBSD 服务器时,系统会提示您验证服务器的指纹。 只需键入 是的 继续并输入用户的密码。

样本输出:

FreeBSD 12.2-RELEASE r366954 GENERIC

Welcome to FreeBSD!

Release Notes, Errata: https://www.FreeBSD.org/releases/
Security Advisories: https://www.FreeBSD.org/security/
FreeBSD Handbook: https://www.FreeBSD.org/handbook/
FreeBSD FAQ: https://www.FreeBSD.org/faq/
Questions List: https://lists.FreeBSD.org/mailman/listinfo/freebsd-questions/
FreeBSD Forums: https://forums.FreeBSD.org/

Documents installed with the system are in the /usr/local/share/doc/freebsd/
directory, or can be installed later with: pkg install en-freebsd-doc
For other languages, replace "en" with a language code like de or fr.

Show the version of FreeBSD installed: freebsd-version ; uname -a
Please include that output and any error messages when posting questions.
Introduction to manual pages: man man
FreeBSD directory layout: man hier

Edit /etc/motd to change this login announcement.
Forget how to spell a word or a variation of a word? Use

look portion_of_word_you_know
-- Dru <[email protected]>
[email protected]:~ $

以普通用户身份通过​​ SSH 连接到 FreeBSD 服务器

以普通用户身份通过​​ SSH 连接到 FreeBSD 服务器

在 FreeBSD 中启用 SSH 根访问

默认情况下,出于安全目的,在 FreeBSD 上禁用 SSH root 登录。 因此,您不能以 root 用户身份通过​​ SSH 访问您的 FreeBSD 服务器。 这是 强烈建议禁用 SSH root 登录 并使用非特权用户并允许 SSH 访问该用户,如上所述。

如果您出于任何原因仍希望允许对 root 用户进行 SSH 访问,请执行以下操作。

编辑 /etc/ssh/sshd_config 文件:

# vi /etc/ssh/sshd_config

找到以下行:

#PermitRootLogin no

通过删除它取消注释 哈希 (#) 符号并将其值更改为 是的 如下所示:

PermitRootLogin yes

在 FreeBSD 中启用 SSH 根访问

在 FreeBSD 中启用 SSH 根访问

Save 并退出文件。 使用命令重启 SSH 服务:

# /etc/rc.d/sshd restart

现在,您可以使用命令从网络上的任何系统以 root 用户身份通过​​ SSH 登录。

# ssh [email protected]

样本输出:

以 root 用户 SSH 进入 FreeBSD 服务器

以 root 用户 SSH 进入 FreeBSD 服务器

同样,不建议为 root 帐户启用 SSH 访问。 我强烈建议您使用非特权用户并禁用 SSH root 登录。

相关阅读:

  • 如何从 FreeBSD 12 升级到 FreeBSD 13