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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章