linux 安裝 ftp 以及 將ftp文件下載到指定目錄

1. ftp 的安裝

rpm -qa |grep vsftpd

2. 安裝命令

yum -y install vsftpd

3.新增FTP用戶

useradd -d /home/ftp ftpuser

4.密碼

admin123

[root@basenode plugins]# passwd ftpuser

5. 設(shè)置ftp文件夾權(quán)限

[root@basenode vsftpd]# chown -R ftpuser:root /home/ftp  
[root@basenode vsftpd]# 

6. 配置文件說(shuō)明

[root@basenode vsftpd]# pwd
/etc/vsftpd
[root@basenode vsftpd]# ll
總用量 20
-rw------- 1 root root  125 6月  10 2021 ftpusers
-rw------- 1 root root  371 7月  31 00:33 user_list
-rw------- 1 root root 5116 6月  10 2021 vsftpd.conf
-rwxr--r-- 1 root root  338 6月  10 2021 vsftpd_conf_migrate.sh
[root@basenode vsftpd]#

6.1 添加用戶

```bash
vim user_list 
ftptest  

6.2 修改配置文件

vim /etc/vsftpd/vsftpd.conf

userlist_enable=YES     #啟動(dòng)用戶列表
userlist_deny=NO        #決定是否對(duì)用戶列表的用戶拒絕訪問(wèn)ftp 

6.3我的完整配置文件


# Example config file /etc/vsftpd/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=YES
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
write_enable=YES
#
# Default umask for local users is 077. You may wish to change this to 022,
# if your users expect that (022 is used by most other ftpd's)
local_umask=022
#
# Uncomment this to allow the anonymous FTP user to upload files. This only
# has an effect if the above global write enable is activated. Also, you will
# obviously need to create a directory writable by the FTP user.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
#anon_upload_enable=YES
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#
# If you want, you can arrange for uploaded anonymous files to be owned by
# a different user. Note! Using "root" for uploaded files is not
# recommended!
chown_uploads=YES
chown_username=ftpuser

#
# You may override where the log file goes if you like. The default is shown
# below.
#xferlog_file=/var/log/xferlog
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
#
# You may change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# It is recommended that you define on your system a unique user which the
# ftp server can use as a totally isolated and unprivileged user.
#nopriv_user=ftpsecure
#
# Enable this and the server will recognise asynchronous ABOR requests. Not
# recommended for security (the code is non-trivial). Not enabling it,
# however, may confuse older FTP clients.
#async_abor_enable=YES
#
# By default the server will pretend to allow ASCII mode but in fact ignore
# the request. Turn on the below options to have the server actually do ASCII
# mangling on files when in ASCII mode. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# Beware that on some FTP servers, ASCII support allows a denial of service
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd
# predicted this attack and has always been safe, reporting the size of the
# raw file.
# ASCII mangling is a horrible feature of the protocol.
ascii_upload_enable=YES
ascii_download_enable=YES
#
# You may fully customise the login banner string:
ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# You may specify an explicit list of local users to chroot() to their home

ascii_upload_enable=YES
#
# You may fully customise the login banner string:
ftpd_banner=Welcome to blah FTP service.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd/banned_emails
#
# 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=YES
#chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list
#
# You may activate the "-R" option to the builtin ls. This is disabled by
# default to avoid remote users being able to cause excessive I/O on large
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
# the presence of the "-R" option, so there is a strong case for enabling it.
#ls_recurse_enable=YES
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
userlist_deny=NO
allow_writeable_chroot=YES


## 7.開(kāi)始添加目標(biāo)用戶,并為其設(shè)置主目錄

```bash
usera**加粗樣式**dd -d 目錄 -m 目錄 用戶    //-d 目錄 指定用戶主目錄,如果此目錄不存在,則同時(shí)使用-m選項(xiàng),可以創(chuàng)建主目錄。

passwd 用戶名   //設(shè)置密碼
chmod 755 目錄    //以root的視角去修改當(dāng)前目錄的權(quán)限
chown -R ftp用戶名:組名 目錄  //組名可不寫,修改目錄所屬者
systemctl  start  vsftpd   //重啟ftp

8. 將用戶添加列表

vim user_list 
ftptest  

9 訪問(wèn)

ftp://ftptest:ftptest@node01  

ftp://node01 #需要再輸入用戶名和密碼
用戶名:ftptest
密碼  :ftptest

10. ftp 啟動(dòng)關(guān)閉命令

啟動(dòng)vsftpd服務(wù):systemctl  start  vsftpd
停止vsftpd服務(wù):systemct  stop  vsftpd
重啟vsftpd服務(wù):systemctl  restart  vsftpd

11.完整參數(shù)的配置

配置:
anonymous_enable=YES    #設(shè)置是否允許匿名用戶登錄 
local_enable=YES        #設(shè)置是否允許本地用戶登錄 
local_root=/home        #設(shè)置本地用戶的根目錄 
write_enable=YES        #是否允許用戶有寫權(quán)限 
local_umask=022        #設(shè)置本地用戶創(chuàng)建文件時(shí)的umask值 
anon_upload_enable=YES    #設(shè)置是否允許匿名用戶上傳文件 
anon_other_write_enable=YES    #設(shè)置匿名用戶是否有修改的權(quán)限 
anon_world_readable_only=YES    #當(dāng)為YES時(shí),文件的其他人必須有讀的權(quán)限才允許匿名用戶下載,單單所有人為ftp且有讀權(quán)限是無(wú)法下載的,必須其他人也有讀權(quán)限,才允許下載 
download_enbale=YES    #是否允許下載 
chown_upload=YES        #設(shè)置匿名用戶上傳文件后修改文件的所有者 
chown_username=ftpuser    #與上面選項(xiàng)連用,表示修改后的所有者為ftpuser 
ascii_upload_enable=YES    #設(shè)置是否允許使用ASCII模式上傳文件 
ascii_download_enable=YES    #設(shè)置是否允許用ASCII模式下載文件 

chroot_local_user=YES        #設(shè)置是否鎖定本地用戶在自己的主目錄中(將前面#去掉!其他選項(xiàng)可不動(dòng))
chroot_list_enable=YES        #設(shè)置是否將用戶鎖定在自己的主目錄中 
chroot_list_file=/etc/vsftpd/chroot_list    #定義哪些用戶將會(huì)鎖定在自己的主目錄中 (去/etc/vsftpd/chroot_list文件增加用戶名,一行一個(gè))
userlist_enable=YES    #當(dāng)為YES時(shí)表示由userlist_file文件中指定的用戶才能登錄ftp服務(wù)器 
userlist_file=/etc/vsftpd/user_list    #當(dāng)userlist_enable為YES時(shí)才生效

12.常遇到的問(wèn)題

getsebool -a | grep ftp 執(zhí)行命令這個(gè)出不來(lái) 就SELINUX=disabled 修改為1

[root@basenode shell]# getsebool -a | grep ftp
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> on
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_home_dir --> on

SELINUX=disabled 修改為1

[root@basenode shell]# vi /etc/sysconfig/selinux


# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
# SELINUX=enforcing
#SELINUX=disabled
SELINUX=1

# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

在修改

setsebool -P allow_ftpd_full_access on

setsebool -P ftp_home_dir on

13 常用命令

service vsftpd start  //啟動(dòng)ftp服務(wù)(同 systemctl start vsftpd.service )
service vsftpd restart  //重啟ftp服務(wù)
service VSftod stop  //關(guān)閉ftp
service vsftpd status  //查看ftp狀態(tài)
netstat -an | grep 21  //查看服務(wù)有沒(méi)有啟動(dòng)
chkconfig vsftpd on   //開(kāi)機(jī)自啟

14將ftp 的文件下載 到指定目錄

第一種方式 是將文件名 包含時(shí)間 的 名稱 匹配文件名稱進(jìn)行下載

#!/bin/bash
#獲取性能系統(tǒng)小時(shí)粒度的指標(biāo)數(shù)據(jù)
u=ftptest   # ftp用戶名
p=123456789 #ftp密碼
ip=192.168.1.180  # ftp 地址
#T=`date -d yesterday +%Y%m%d`
T=`date  +%Y%m%d`
#寫入日志
echo "(`date`) get data start...." >> /opt/logs/getdata.log
echo "===================================================" >> /opt/logs/getdata.log
    ftp -n <<EOF
      open $ip
      user $u $p
      binary
      cd /home/ftptest/ # ftp 的目錄
      lcd /opt/datas   # 目標(biāo)目錄
      hash
      prompt off
      mget *$T*
      cd /home/ftptest/
      mget *$T*
      close
      bye
EOF
echo "download from ftp successfully"

第二種是是通過(guò)目錄文件夾 新建一個(gè)日期 然后更加日期進(jìn)行下載 到另一個(gè)目錄

#!/bin/sh
#source dircotry
src="/home/ftptest"
yestoday_date=$(date "+%Y%m%d"  -d "1 days ago")
echo "-----" $yestoday_date
[ ! -d ${src}/${yestoday_date} ] && mkdir ${src}/${yestoday_date} -p

ftp -v -n 192.168.1.180 << EOF
user ftptest 123456789
cd /home/ftptest/${yestoday_date}
lcd ${src}/${yestoday_date}
mget *
y
exit

EOF

第三種是直接下載

#!/bin/sh
ftp -v -n 192.168.1.180<<EOF
user ftptest 123456789
binary
cd /home/ftptest/
lcd /opt/datas
prompt
mget *
bye
EOF
echo "download from ftp successfully"

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Linux安裝ftp組件 1安裝vsftpd組件 安裝完后,有/etc/vsftpd/vsftpd.conf 文件...
    JasonXu123閱讀 593評(píng)論 0 0
  • 1.FTP簡(jiǎn)介 File Transfer Protocol 早期的三個(gè)應(yīng)用級(jí)協(xié)議之一? 基于C/S結(jié)構(gòu)? 雙通道...
    尛尛大尹閱讀 2,665評(píng)論 0 0
  • ftp 文件傳輸協(xié)議 跨平臺(tái) 上傳下載文件 vsftpd 工具:非常安全的文件傳輸協(xié)議;默認(rèn)的命令端口21號(hào),數(shù)據(jù)...
    柒夏錦閱讀 4,224評(píng)論 1 9
  • FTP文件服務(wù)器FTP介紹應(yīng)用場(chǎng)景FTP的權(quán)限FTP的工作模式FTP安裝部署FTP配置文件FTP客戶端訪問(wèn)基于虛擬...
    亮仔_c1b5閱讀 664評(píng)論 0 0
  • 1. 檢測(cè)vsftpd是否安裝命令: $ rpm -qa | grep vsftpd 2. 檢測(cè)vsftpd是否啟...
    _micang閱讀 235評(píng)論 0 0

友情鏈接更多精彩內(nèi)容