ubuntu安装ftp和tftp

为了局域网方便访问ubuntu系统,在ubuntu14.04上安装了tfp和tftp服务。在windows端用FileZilla、FlashFXP、xFTP、CuteFTP等等访问都很方便。

一、安装ftp服务器

  1. 安装vsftpd:
apt-get install vsftpd 

检查端口

netstat -tnl

ftp默认打开的是21端口,看下对应21端口的服务有没有打开。
或者直接在浏览器里输入“ftp://服务器IP”,会有相关提示的。

开启、停止、重启vsftpd服务的命令:

service vsftpd start | stop | restart  
  1. 修改配置文件
vim /etc/vsftpd.conf 

vsftpd.conf主要配置:

# Example config file /etc/vsftpd.conf
listen=YES
anonymous_enable=NO #禁止匿名登录
local_enable=YES #本地用户可以登录
write_enable=YES #允许写操作,包括上传,修改等
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES #显示目录信息
use_localtime=YES #使用本地时间
xferlog_enable=YES #开启日志
xferlog_file=/var/log/vsftpd.log #日志存储位置
xferlog_std_format=YES #日志标准格式
chroot_local_user=YES #将用户限制在他们的home目录 可以选填NO
chroot_list_enable=YES #启用可以chroot的用户的列表
chroot_list_file=/etc/vsftpd.chroot_list #指定可以chroot的用户列表的位置
ls_recurse_enable=YES #允许递归操作
secure_chroot_dir=/var/run/vsftpd/empty #必须为空  

用户访问目录的权限设置:
在默认配置下,本地用户登入FTP后可以使用cd命令切换到其他目录,这样会对系统带来安全隐患。可以通过以下三条配置文件来控制用户切换目录。

    chroot_local_user=YES      # 用于指定用户列表文件中的用户是否允许切换到上级目录。默认值为NO。  
    chroot_list_enable=YES  # 设置是否启用chroot_list_file配置项指定的用户列表文件。默认值为NO。  
    chroot_list_file=/etc/vsftpd.chroot_list      
    #禁用的列表名单,格式为一行一个用户,用于指定用户列表文件,该文件用于控制哪些用户可以切换到用户家目录的上级目录。  

通过搭配能实现以下几种效果:

    (1).当chroot_list_enable=YES,chroot_local_user=YES时,在/etc/vsftpd.chroot_list文件中列出的用户,可以切换到其他目录;未在文件中列出的用户,不能切换到其他目录。  
    (2).当chroot_list_enable=YES,chroot_local_user=NO时,在/etc/vsftpd.chroot_list文件中列出的用户,不能切换到其他目录;未在文件中列出的用户,可以切换到其他目录。  
    (3).当chroot_list_enable=NO,chroot_local_user=YES时,所有的用户均不能切换到其他目录。  
    (4).当chroot_list_enable=NO,chroot_local_user=NO时,所有的用户均可以切换到其他目录。  

如果你被绕晕了,可以忽略这些,直接看英文注释。

# You may restrict local users to their home directories.  See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
#
# You may specify an explicit list of local users to chroot() to their home
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)

也就是说,如果chroot_local_user选项为NO,则本地账户可以访问除home目录以外的其他目录,此时chroot_list_file是不可以chroot()的用户;当chroot_local_user为YES时,本地账户只能访问自家目录,此时chroot_list_file变成可以chroot()的用户列表。

我设置了chroot_list_enable=NO,chroot_local_user=YES,并不设置 /etc/vsftpd.chroot_list限制名单,可以让它访问home以外的目录。

3.用户设置
这个用户主要用于访问ftp服务器

 useradd  -d /home/uftp -m uftp

设置用户密码

 passwd uftp

完成上面这些之后,编辑/etc/shells文件,如果没有/sbin/nologin,要添加这一行。这是禁止该用户登录shell。

/sbin/nologin

这里要把uftp用户的shell设置为/sbin/nologin,否则访问不了。

usermod -s /sbin/nologin uftp

修改特定目录的访问权限

chmod -R 755 /opt/tomcat
chown -R uftp:ftp /opt/tomcat
  1. 开机启动设置
systemctl enable vsftpd 

启动ftp服务

systemctl start vsftpd.service

重启vsftpd服务

/etc/init.d/vsftpd restart  
 或 service vsftpd restart  

修改配置文件后一定要重启服务才能生效

用命令查看当前电脑局域网IP地址

ifconfig

然后在ftp访问终端输入对应的IP地址,端口号(21),账户密码就能访问了。

  1. 卸载
    删除用户
sudo userdel uftp 

卸载软件

sudo apt-get remove --purge vsftpd

(–purge 选项表示彻底删除改软件和相关文件)

二、安装tftp服务器

TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂、开销不大的文件传输服务。端口号为UDP:69。 路由器,交换机等网络设备升级硬件系统可用,PXE安装系统需要配置tftp服务。

1、安装

sudo apt-get install tftp-hpa tftpd-hpa

2、配置

sudo gedit /etc/default/tftpd-hpa

打开tftpd-hpa修改里面的配置

TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/home/uftp/tftpboot" //修改成自己要使用的tftpboot目录
TFTP_ADDRESS="[::]:69"
TFTP_OPTIONS="-l -c -s"

3、创建tftp目录

mkdir /home/uftp/tftpboot
chmod 777 /home/uftp/tftpboot

4、测试
在当前命令目录下新建test.txt

$ tftp localhost
tftp>put test.txt
tftp>get test.txt
tftp>q

需要文件真实存在,否则报Error code 1: File not found

5、卸载

sudo apt-get remove --purge tftp-hpa tftpd-hpa

清理残留数据

dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P 

6、tftp服务器重启

sudo service tftpd-hpa restart
或sudo /etc/init.d/tftpd-hpa restart
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章