使用debian做ftp服务器

copyright(c) 李雪白 2007年

本文遵从自由软件基金会发布的GNU自由文档许可证(GNU Free Document License), 任何组织和个人都有在此条款下的复制,转载,修改的权限。


许多实验室都有淘汰下来的旧电脑,有的闲置着也没什么用,其实经过简单的配置就可以拿来做ftp服务器了,可以自己实验室同学用着,也可以公开用户给大家用,放点电影电视剧之类的,放些音乐的,都蛮好的。

我下面使用的电脑就是一个旧电脑,cpu大概是赛扬几百兆的,内存似乎也只有256M……只要硬盘稍微大点就可以了。显示器也可以没有,安装系统的时候拿别的先用用,安装系统结束之后就拿走了,以后可以通过ssh远程管理。当然必要的条件是有网络阿,能够接入校园网。

选择linux是因为很方便简单,同时不安装桌面可以使得系统很小,不到600M;因为科大著名的debian.ustc.edu.cn,肯定要选择debian了.

实现的功能是可以匿名访问,可以在特定的目录匿名上传,匿名用户没有删除权力。可以许多的本地用户访问,对其独有文件夹具有完全的权限,这许多的用户分成几个组,设有组内共享文件夹,组间不能访问。

1,刻录安装光盘

在其他的有刻录机的电脑中下载

ftp://debian.ustc.edu.cn/debian-cd/images/current/debian-31r0a-i386-binary-1.iso

刻录光盘(把iso文件挂载或者虚拟光驱,然后镜像刻录就可以了)。

这个stable版本是sarge

2,下载源列表

ftp://debian.ustc.edu.cn/sources.list/

下载stable源列表,把内容抄录出来。

# /etc/apt/sources.list for debian stable(sarge)
# see sources.list(5) for more information.
# you can replace ftp with htttp.
# this file should be used in stable(sarge) only.

deb ftp://debian.ustc.edu.cn/debian stable main non-free contrib
deb-src ftp://debian.ustc.edu.cn/debian stable main non-free contrib

## See: http://www.debian.org/releases/sarge/i386/release-notes/ch-whats-new.en.html#s-non-us
#deb ftp://debian.ustc.edu.cn/debian-non-US stable/non-US main non-free contrib
#deb-src ftp://debian.ustc.edu.cn/debian-non-US stable/non-US main non-free contrib

deb ftp://debian.ustc.edu.cn/debian proposed-updates main contrib non-free
deb-src ftp://debian.ustc.edu.cn/debian proposed-updates main contrib non-free

deb ftp://debian.ustc.edu.cn/debian-security stable/updates main

只需要用笔在纸上记录未被注释的5行即可。

3,光盘引导安装。

按照提示一步一步来...

硬盘分区,给根目录/ 2G应该足够了,剩下基本上是/home的, 也可以给swap,大小和内存差不多就可以了。 (其实根目录连600M都没有用到,当然没有安装x,以后也就是 黑屏字符界面的)

手动修改安装源列表, 删掉原有的sources.list

输入之前抄录的源

选择手动安装软件,不安装桌面文件。

ok,系统安装完了。

4,安装一些GNU工具

在aptitude中安装lftp,vim,emacs,wget等符号化的工具, (其实后面主要用的是emacs或者vim这些编辑器)。

检查ssh是否安装过了。

5,修改ip地址

编辑 /etc/network/interfaces 文件.

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# This is a list of hotpluggable network interfaces.
# They will be activated automatically by the hotplug subsystem.
mapping hotplug
        script grep
        map eth0

# The primary network interface
auto eth0
iface eth0 inet static
        address 202.38.??.??
        netmask 255.255.255.0
        network 202.38.??.??
        broadcast 202.38.??.??
        gateway 202.38.??.??
        dns-server 202.38.64.1

编辑 /etc/hosts 这个文件并且把新IP地址加进去.

127.0.0.1       localhost.localdomain   localhost       userftp
202.38.??.??    ???.ustc.edu.cn          userftp

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

/etc/init.d/networking restart 重新启动网络。

6,配置vsftp

现在可以通过其他电脑

ssh ??@202.38.??.??

来远程管理服务器了。

安装vsftp

apt-get install vsftpd

配置/etc/vsftpd.conf

# 服务器以standalong模式运行,这样可以进行下面的控制
listen=YES
# 接受匿名用户
anonymous_enable=YES
# 匿名用户login时不询问口令
no_anon_password=YES
# 接受本地用户
local_enable=YES
# 可以上传(全局控制).若想要匿名用户也可上传则需要设置anon_upload_enable=YES, 
# 若想要匿名用户可以建立目录则需要设置anon_mkdir_write_enable=YES.这里禁止匿
# 名用户上传,所以不设置这两项.
write_enable=YES
# 本地用户上传文件的umask
local_umask=022
# 如果设为YES,匿名登入者会被允许上传目录的权限,当然,匿名使用者必须要有对上
# 层目录的写入权。
anon_upload_enable=YES
# 定义匿名登入的使用者名称。默认值为ftp
ftp_username=ftp
# 如果设为YES,匿名登入者会被允许新增目录,当然,匿名使用者必须要有对上层目录
# 的写入权。
anon_mkdir_write_enable=YES
# 为YES则进入目录时显示此目录下由message_file选项指定的文本文件
# (,默认为.message)的内容
dirmessage_enable=YES
# 本地用户login后所在目录,若没有设置此项,则本地用户login后将在他的home目录
# (/etc/passwd的第六个字段)中.匿名用户的对应选项是anon_root
# local_root=/home
anon_root=/home/ftp/
# 使用上传/下载日志,日志文件默认为/var/log/vsftpd.log,可以通过xferlog_file
# 选项修改
xferlog_enable=YES
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
# 日志使用标准xferlog格式
xferlog_std_format=YES
# You may change the default value for timing out a data connection.
data_connection_timeout=120
# 关闭本地用户chroot()
chroot_local_user=NO
# 设置为yes则下面的控制有效。
# 开启要设置chroot()用户项.
chroot_list_enable=YES
# (default follows)
# 指定要设置chroot()的特定用户文件
chroot_list_file=/etc/vsftpd.chroot_list
# 若设置为YES则记录在userlist_file选项指定文件(默认是/etc/vsftpd.user_list)
# 中的用户无法login,并且将检察下面的userlist_deny选项
userlist_enable=YES
# 若为NO,则仅接受记录在userlist_file选项指定文件(默认是/etc/vsftpd.user_list)
# 中的用户的login请求.若为YES则不接受这些用户的请求.
userlist_deny=NO
# 注意!!!vsftpd还要检察/etc/vsftpd.ftpusers文件,记录在这个文件中的用户将
# 无法login!!
# 匿名用户的传输比率(b/s)
anon_max_rate=512000
# 本地用户的传输比率(b/s)
local_max_rate=1024000
# 可接受的最大client数目
max_clients=100
# 每个ip的最大client数目
max_per_ip=5
# This option should be the name of a directory which is empty.  Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem access.
secure_chroot_dir=/var/run/vsftpd
# This string is the name of the PAM service vsftpd will use.
pam_service_name=vsftpd

tcp_wrappers=YES

# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/certs/vsftpd.pem
# 每一个联机,都能以独立的process 来呈现.
setproctitle_enable=YES
# 若是启动,所有匿名上传数据的拥有者将被更换为chown_username当中所设定的使用
# 者。这样的选项对于安全及管理,是很有用的。
chown_uploads=YES
# 这里可以定义当匿名登入者上传档案时,该档案的拥有者将被置换的使用者名称。预
# 设值为root。
chown_username=root

7,继续配置其他相关

增加用户名一堆(都属于ftpuser组的)

groupadd ftpuser
mkdir /home/john/
useradd -g ftpuser john
passwd john

补充/etc/passwd 中缺漏的/bin/bash

比如

john:x:1001:1001::/home/john:

增加为

john:x:1001:1001::/home/john:/bin/bash

否则很可能shell无法解析命令。

更改文件所有者以及权限

chown -v -R john:ftpuser /home/john/
chmod -v -R 700 /home/john/

给ftpuser组所有用户共享一个文件夹,做一个连接

ln -v -s /home/ftpuser/ /home/john/ftpuser

在每个用户的文件夹中设置.message做欢迎界面

编辑/etc/vsftpd.user_list 填写所有可以ftp的用户,包括匿名anonymous

编辑/etc/vsftpd.chroot_list 填写不准进入上层目录的用户名

给/home/ftp/中允许匿名写的目录修改权限。

chmod -v 777 /home/ftp/temp/

做两个定时工作 crontab -e 在里面写

0,10,20,30,40,50 * * * * chmod 555 /home/ftp/incoming/*
# 每10分钟修改一次/home/ftp/incoming/ 中的权限
0 4 * * 1 rm -rf /home/ftp/incoming/temp/*
# 每星期一凌晨4点清空/home/ftp/incoming/temp/文件夹
0 5 10 * * /sbin/reboot
# 每个月10日凌晨5点重新启动电脑

重启vsftp

/etc/init.d/vsftpd restart

好了,ftp服务器做好了。