在 FreeBSD 上配置 ftp 服务非常简单。但一般较少进行这样的配置,所以经常忘记配置过程。因为银行必须使用 ftp 传送文件,无法选择其他方式。今天我对着旧服务器,依旧还要花大半天才在新服务器上成功让用户只登录 ftp 而不能登录系统。我把这个配置过程写下来,供需要的人参考。
第一步:添加并启动 ftpd 服务。
在 /etc/rc.conf 中添加一行:
ftpd_enable="YES"
然后在命令行输入:
service ftpd start
ftpd 成功启动。这里一般不会有问题的。
第二步:添加一个用户 test。
# adduser
Username: test
Full name: test
Uid (Leave empty for default):
Login group [test]:
Login group is test. Invite test into other groups? []:
Login class [default]:
Shell (sh csh tcsh nologin nologin) [sh]: nologin //使用nologin,不允许用户登录服务器。
Home directory [/home/test]:
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 : test
Password : *****
Full Name : test
Uid : 1003
Class :
Groups : test
Home : /home/test
Home Mode :
Shell : /usr/sbin/nologin
Locked : no
OK? (yes/no): yes
adduser: INFO: Successfully added (test) to the user database.
Add another user? (yes/no): no
Goodbye!
我把这个用户的 Shell 设置为 /usr/sbin/nologin 这样用户就无法登录到我的服务器系统。
第三步:设置用户的登录目录。
指定前面添加的用户 test 登录 ftp 后的根目录,用户通过 ftp 上传的文件保存在这个目录下面,登录 ftp 后无法看到其他目录。在 /etc/ftpchroot 文件中添加这样一句:
test /usr/www/test ./test
第四步:设置允许用户登录 ftp。
经过前面的设置之后,用户还是既不能登录系统,也不能登录 ftp。用户在使用ssh登录时会提示:
This account is currently not available.
Connection to [xxx] closed.
登录FTP时提示:
530 User test access denied.
ftp: Login failed
这是因为 Ftpd 只允许使用 /etc/shells 文件中列出的 shell 项的用户登录。所以要让用户正常登录FTP,需要在在 /etc/shells 文件中添加一项:
/usr/sbin/nologin
号称万能的 FreeBSD 最新的手册上也没有提到这个问题。这样设置之后,用户就只可以登录ftp而无法登录系统。登录 ftp 之后显示登录的是根目录:
ftp> user
(username) test
331 Password required for test.
Password:
230 User test logged in, access restrictions apply.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
Remote directory: /
这样就可以了。