FTP協議的優劣勢與應用場景:技術揭秘

FTP

FTP檔案傳輸協議

FTP(File Transfer Protocol)檔案傳輸協議,基於C/S架構,支援檔案的上傳和下載功能
FTP使用2個埠,都是基於TCP進行傳輸連線,分別是20和21
20埠為資料傳輸埠,上傳和下載檔案使用此埠
21埠為命令傳輸埠,客戶端和服務端之間的命令互動使用此埠

工作模式

主動模式:Client向Server的20埠傳送資料連線請求,伺服器主動連線,然後傳輸檔案資料

被動模式:Client向Server傳送請求,Server隨機開放一個埠和Client連線傳輸檔案

[root@dns named]# netstat -tunpla|grep -w 21

tcp6 0 0 :::21 :::* LISTEN 4946/vsftpd

tcp6 0 0 192.168.5.101:21 192.168.5.8:62972 ESTABLISHED 7208/vsftpd #命令傳輸埠

tcp6 0 0 192.168.5.101:18502 192.168.5.102:52025 TIME_WAIT - #資料傳輸埠
FTP支援三種認證模式:

➢ 匿名使用者:任何人無需驗證即可訪問和使用ftp

➢ 本地使用者:使用Linux系統本地使用者認證來使用ftp

➢ 虛擬使用者:透過建立虛擬使用者來訪問ftp,虛擬使用者只用於ftp而不會登入Linux系統

wget工具下載檔案

wget ftp:

//us

er1:

123

@192.

168.5

.

101

/

1

.txt

本地模式

1.安裝好ftp後啟動服務

[root@web01 ~]

# systemctl restart vsftpd.service

2.建立使用者

useradd user1

3.設定使用者不能登入

4.給使用者設定密碼
5.客戶端登入

[root@localhost ~]

# lftp 192.168.5.101

lftp 192.168.5.101:~> user user1

Password:

ls

pwd

ftp://[email protected]/%2Fhome/user1

6.上傳,拉取檔案

lftp 192.168.5.101:~> user user1

Password:

lftp [email protected]:~> put /etc/passwd

2546 bytes transferred

exit

lftp [email protected]:~> get passwd

2546 bytes transferred

匿名模式登入ftp

服務端修改配置檔案引數

[root@dns ~]# cat /etc/vsftpd/vsftpd.conf |grep -v ^#

anonymous_enable=YES
客戶端登入

[root@dns2 ~]# ftp 192.168.5.101

Connected to 192.168.5.101 (192.168.5.101).

220 (vsFTPd 3.0.3)

Name (192.168.5.101:root): anonymous

331 Please specify the password.

Password:

230 Login successful.

Remote system type is UNIX.

Using binary mode to transfer files.

#

客戶端匿名登入之後的目錄為/var/ftp/pub

[root@web01 user1]# cat /etc/passwd|grep ftp

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

[root@web01 user1]# ls /var/ftp/

pub

匿名上傳ftp

匿名上傳拒絕

[root@localhost ~]# lftp 192.168.5.101

lftp 192.168.5.101:~> ls

drwxr-xr-x 2 0 0 6 Nov 16 2020 pub

lftp 192.168.5.101:/> put passwd

put: Access failed: 550 Permission denied. (passwd)
1.修改資料夾許可權

不能將根目錄許可權改為777

[root@web01 ~]# chmod 777 /var/ftp/pub
2.修改配置檔案

[root@web01 pub]# cat /etc/vsftpd/vsftpd.conf |grep -v ^#

anonymous_enable=YES

anon_upload_enable=YES

2.進入pub目錄上傳檔案

lftp 192.168.5.101:/> cd pub

lftp 192.168.5.101:/pub> put passwd

2546 bytes transferred

lftp 192.168.5.101:/pub>

匿名刪除檔案

lftp 192.168.5.101:/pub>

rm

passwd

rm

: Access failed: 550 Permission denied. (passwd)

lftp 192.168.5.101:/pub>

1.配置檔案中新增行

anon_mkdir_write_enable=YES ---可以建立資料夾

anon_other_write_enable=YES ---可以刪除檔案
2.測試

lftp 192.168.5.101:/pub>

mkdirtest
mkdir

ok, `

test

' created

lftp 192.168.5.101:/pub> ls

-rw------- 1 14 50 2546 Nov 21 10:54 passwd

drwx------ 2 14 50 6 Nov 21 13:58 test

lftp 192.168.5.101:/pub> rm passwd

rm ok, `passwd'

removed

lftp 192.168.5.101:/pub>

rm

-fr

test

/

rm

ok, `

test

/

' removed

lftp 192.168.5.101:/pub> ls

修改本地使用者和匿名登入的位置

配置檔案加入這段

cat /etc/vsftpd/vsftpd.conf |grep -v ^#

anon_root=/mnt

local_root=/mnt
修改目錄的許可權

[root@web01 ~]# chmod 777 /mnt/data/

下載ftp裡面的檔案

修改

umask

值可以下載自己上傳的檔案

anon_umask=022
[root@localhost ~]

# lftp 192.168.5.101

lftp 192.168.5.101:~>

cd

data/

lftp 192.168.5.101:/data> get passwd

2546 bytes transferred

lftp 192.168.5.101:/data>
本地使用者參考匿名使用者

僅允許user1登入

配置檔案中加上

userlist_enable=YES

userlist_deny=NO
在檔案中加入使用者名稱

[root@dns vsftpd]# cat /etc/vsftpd/user_list

#

vsftpd userlist

#

If userlist_deny=NO, only allow

usersin

this file

#

If userlist_deny=YES (default), never allow

usersin

this file, and

# do

not even prompt

for

a password.

#

Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers

# forusers

that are denied.

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

news

uucp

operator

games

nobody

user1

不在ftpusers檔案中 在user_list檔案中才能登入

允許使用者跳出和不能跳出根目錄

新增配置檔案

chroot_local_user=YES ###所有人都不能跳出根目錄 FTP根目錄

chroot_list_enable=YES ###如果需要指定使用者可以跳出FTP根目錄,則需要開啟以下內容

chroot_list_file=/etc/vsftpd/chroot_list
[root@dns vsftpd]# cat chroot_list 僅允許user1可以跳出目錄

user1

vsftp配置虛擬使用者

[root@web01 ~]# cd /usr/share/doc/vsftpd/EXAMPLE/VIRTUAL_USERS

[root@node4 ~]# vim /etc/vsftpd/vsftpd.conf

guest_enable=YES

guest_username=vuser666

allow_writeable_chroot=YES #允許使用者在其 chroot 目錄中進行寫操作

anon_upload_enable=YES #允許匿名使用者上傳檔案

[root@node4 vsftpd]# vim /etc/vsftpd/logins.txt

zhangsan

redhat

lisi

redhat

[root@node4 vsftpd]# useradd -d /data/upload/ vuser666

[root@node4 ~]# chmod 755 /data/upload/

[root@node4 vsftpd]# db_load -T -t hash -f /etc/vsftpd/logins.txt /etc/vsftpd/user.db

[root@node4 ~]# vim /etc/pam.d/vsftpd

####該檔案僅保留兩行,其他均刪除
#

%PAM-1.0

auth required pam_userdb.so db=/etc/vsftpd/user

account required pam_userdb.so db=/etc/vsftpd/user

[root@node4 ~]# lftp 192.168.6.4

lftp 192.168.6.4:~> user zhangsan

Password:

lftp [email protected]:~> cd redhat/

lftp [email protected]:/redhat> ls

-rw------- 1 1002 1002 1025 Jul 25 19:04 group.1

-rw-r--r-- 1 0 0 2689 Jul 25 18:31 passwd

lftp [email protected]:/redhat> put /etc/profile

2123 bytes transferred

lftp [email protected]:/redhat> exit
上傳的檔案在vuser666的家目錄中

連結:https://www.cnblogs.com/cloudwangsa/p/18563731
                                                              (版權歸原作者所有,侵刪)

文末福利

最後給大家送上一份新年禮物華為《Linux核心筆記》,共計1456頁,確實乾貨,運維必須收藏備用!同時也非常適合專業的運維人員,幫你提高效率,早早下班的好輔助!(文末免費一鍵領取)

這份筆記非常全面且詳細,從Linux基礎到shell指令碼,再到防火牆、資料庫、日誌服務管理、Nginx、高可用叢集、Redis、虛擬化、Docker等等,與其說Linux學習筆記,不如說是涵蓋了運維各個核心知識。


並且圖文並茂,程式碼清晰,每一章下面都有更具體詳細的內容,十分適合Linux運維學習參考!

領取方式

掃碼加上小助理(秒發)
回覆暗號“1456頁Linux筆記“,
即可100%免費領取成功
(注意:後臺回覆無效,是掃碼領取哦)

相關文章