Linux搭建ftp服務器簡單教程及使用方法


參考文章:https://www.waitig.com/linux-or-centos-install-vsftpd-and-setup-it.html

 

步驟概括如下:

安裝:yum install vsftpd

操作: service vsftpd start|stop|restart

配置部分(重點):

       1. 為了系統安全,一般會建立一個ftp用戶,此用戶不能登陸系統,且只能訪問自己主目錄下的文件。

             useradd  -d  /var/ftp/test     -g ftp -s   /sbin/nologin    ftpuser

             其中,-d命令是指定用戶主目錄,-g是指定用戶分組,-s /sbin/nologin 是禁止用戶登陸系統,最后ftpuser是本次新建用戶的用戶名。

             然后設置密碼:passwd fptuser

      2.  配置用戶可登陸名單,並將新建用戶添加進入ftp可登陸名單中。

         配置用戶名單的方式是:打開配置文件,找到如下兩行行,去掉其注釋符號。

              chroot_list_enable=YES

             # (default follows)

             chroot_list_file=/etc/vsftpd/chroot_list

         這樣就定義了一個用戶名單,只有名單中的用戶可以登陸系統。 

    3. 創建這個/etc/vsftpd/chroot_list   文件,將可以訪問的用戶名   ftpuser  添加進去。然后重啟ftp服務即可。

 

 操作解釋:

      -d  /var/ftp/test   ----------------------------------    遠程機器訪問這台ftp服務器的根目錄,文件列表與這個目錄一致。

      useradd/passwd   --------------------------------- 本地的用戶名和密碼

     /etc/vsftpd/chroot_list   ---------------------------  將本地用戶名XX作為ftp用戶名
            

ftp及scp命令的使用

參考文章:http://www.cnblogs.com/weafer/archive/2011/06/13/2079509.html

ftp :

     1.  ftp 192.168.26.66

     2. 輸入用戶名和密碼

     3.get  下載

        格式:get [remote-file] [local-file]
  將文件從遠端主機中傳送至本地主機中.

       注意:文件都下載到了linux主機的當前目錄下。比如,在 /root/yint下運行的ftp命令,則文件都下載到了/root/yint 下。

     4.put  上傳

        格式: put local-file [remote-file]

        注意:上傳文件都來自於主機的當前目錄下。比如,在 /root/test下運行的ftp命令,則只有在/root/test下的文件linux才會 上傳到服務器e:\rose 下。

 

scp:

    scp是安全的文件拷貝,基於ssh的登錄

    假定你想把本地計算機/home下的一個名為a.tar.tz的文件拷貝到遠程服務器192.168.0.2上的/home/tmp。而且你在遠程服務器 上的帳號名為root。可以用這個命令:
scp /home/a.tar.tz root@192.168.0.2:/home/tmp/

    

----------------------------------------------------------

附錄: (參考鏈接:http://www.linuxsv.org/training/l23_linux_ftp.html)

 

FTP File Transfer Protocol allows file transfer between hosts on a network without having to login on a standard shell directly on the remote host. The file transfer is done using a standard set of simple commands without encryption, so it must be used only in a not hostile environment. Features like 'scp' that uses ssh protocol for encrypted file transfer can be used to file transfers on a hostile environment like Internet.

 

FTP Server

 

In order to configure a host as a FTP server the package vsftp must be installed, configured through /etc/vsftpd/vsftpd.conf and configured to be started at boot.

# yum install vsftpd
# chkconfig vsftpd on
# /etc/init.d/vsftpd start

 

/etc/vsftpd/vsftpd.conf

 

This is the main configuration file and specifies the way that the FTP server runs. The most important parameters that can be configured are the following :

anonymous_enable=YES
It allows FTP transfer using the anonymous user with password anonymous.

local_enable=YES
Local accounts are valid FTP accounts.

write_enable=YES
Enables write operations on FTP.

#anon_upload_enable=YES
It allows anonymous user to upload files. By default this line is commented so the anonymous user by default con not upload files to the FTP server.

#chroot_list_enable=YES
With chroot_local_user=YES you can configure users who are logged on FTP server to be confined in to their home directory on the FTP server. Disabled by default.

pam_service_name=vsftpd
Configures Pluggable Authentication Modules (PAM) security for FTP.

userlist_enable=YES
Keeps users such as root and system user listed on /etc/vsftpd/user_list from logging into the FTP server. It must be activated always !!!

tcp_wrappers=YES
Supports the use of security commands in /etc/hosts.allow and /etc/hosts.deny through tcpwrappers

 

FTP Security

 

Firewall

 

The FTP server listen on port 21 TCP so it must be open on the firewall .

-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT


In the case of FTP server is also required to load the nat iptable module that keep track all FTP connections and allows it. This configuration is applied on /etc/sysconfig/iptables-config file :

IPTABLES_MODULES="nf_conntrack_ftp"-->/etc/sysconfig/iptables-config

# /etc/init.d/iptables restart

 

SElinux

 

There are five directives associated with making FTP server work with SELinux in targeted mode:

# setsebool -P allow_ftpd_full_access 1
If this parameter is enabled ftpd will run on a SElinux context without any restriction.

# setsebool -P allow_ftpd_anon_write 1
Supports the writing of files to directories configured with the public_content_rw_t SELinux setting.

# setsebool -P allow_ftpd_use_cifs 1
Allows the use of files shared via CIFS on an FTP server.

# setsebool -P allow_ftpd_use_nfs 1
Allows the use of files shared via NFS on an FTP server.

# setsebool -P ftp_home_directory 1
Supports FTP read/write access to user home directories.

In addition any directory that is going to be used on read-write FTP operations it must be labelled as 'public_content_rw_t' SElinux attribute in order to work correctly in SElinux targered mode .

# chcon -R -t public_content_rw_t /var/pub/ftp

 

FTP anonymous server

 

In this section we are going to configure a FTP server on rhel6 server and only allow anonymous login. Only downloading data from FTP server must be allowed files, uploading must be forbidden.

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

anonymous_enable=YES
local_enable=NO
write_enable=NO
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES


Configure the firewall as defined on 'FTP Security'. If SElinux is running on targered mode the easy way applied in this case is give full access to the ftpd daemon on SElinux context :

# setsebool -P allow_ftpd_full_access 1

Lets create a file on the root of the ftp directory /var/ftp/pub. This file will be downloaded by anonymous user.

# dd if=/dev/null of=/var/ftp/pub/file bs=1024 count=1000

And finally restart the ftp service. Make sure the service starts correctly watching logs on /var/log/messages.

# /etc/init.d/vsftpd restart

From another host login to the FTP server on rhel6 as anonymous user using the FTP client 'lftp'. Download file created previously and verify that uploading is forbidden.

node01> lftp 192.168.1.10
lftp 192.168.1.10:~> cd pub
cd ok, cwd=/pub
lftp 192.168.1.10:/pub> ls
-rw-r--r-- 1 0 0 10240000 Feb 22 20:36 file
lftp 192.168.1.10:/pub> get file
10240000 bytes transferred


By default the FTP client 'lftp' login as anonymous. From there file has been downloaded correctly. Lets try to download a file :

lftp 192.168.1.10:/pub> put anaconda-ks.cfg
put: Access failed: 550 Permission denied. (anaconda-ks.cfg)


Uploads are not allowed.

lftp 192.168.1.10:/> cd /var
cd: Access failed: 550 Failed to change directory. (/var)


Navigate outside the FTP server is not allowed.

Try to login as other user as anonymous and verify that only anonymous logins are permitted.

node01> lftp -u john
Password:
lftp john@:~> ls
ls: Not connected


The same is we try as root and other users ... only anonymous logins are allowed.

 

FTP non-anonymous server

 

In this case we are going to configure an FTP server on rhel6 that must only allow logins to all system users less the listed on /etc/vsftpd/user_list . Download/upload must be allowed for these users.

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

anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES


Configure the firewall as defined on 'FTP Security'. In this case we are going to configure ftpd to run on SElinux environment. This is not the easy way as in previous example :

# setsebool -P allow_ftpd_full_access 0
# setsebool -P allow_ftpd_anon_write 1
# setsebool -P allow_ftpd_use_cifs 1
# setsebool -P allow_ftpd_use_nfs 1
# setsebool -P ftp_home_dir 1


Lets create a file on 'john' /home dir of the ftp directory /home/john. This file will be downloaded by user john.

# cp /var/ftp/pub/file /home/john
# chown john:john /home/john/file


And finally restart the ftp service. Make sure the service starts correctly watching logs on /var/log/messages.

# /etc/init.d/vsftpd restart

From another host login to the FTP server on rhel6 as 'john' user using the FTP client 'lftp'. Download file created previously and verify that uploading is allowed on john /home.

node01> lftp -u john 192.168.1.10
Password:
lftp john@192.168.1.10:~> ls
-rw-r--r-- 1 1001 1001 10240000 Feb 22 22:08 file
lftp john@192.168.1.10:~> get file
10240000 bytes transferred
lftp john@192.168.1.10:~> put install.log
21820 bytes transferred
lftp john@192.168.1.10:~> ls
-rw-r--r-- 1 1001 1001 10240000 Feb 22 22:08 file
-rw-r--r-- 1 1001 1001 21820 Feb 23 20:06 install.log
lftp john@192.168.1.10:~> cd /var
lftp john@192.168.1.10:/var>


As can be seen 'john' user can download/upload files on /home/john through FTP. But 'john' still has access to directories outside his home, on Lab1 we will configure the FTP server to chroot users onto his home directory.

Users listed on /etc/vsftpd/user_list are not allowed to login on FTP server :

node01> lftp -u root 192.168.1.10
Password: lftp root@192.168.1.10:~> dir
`ls' at 0 [Delaying before reconnect: 20]
...

 

FTP Client

 

As has been seen in previous sections the lftp RPM can be used as FTP Client.

# yum install lftp

In order to login as user 'john' on FTP server 192.168.1.10 :

lftp -u john 192.168.1.10
Password:
lftp john@192.168.1.10:~>


If no user is specified the FTP login is done using the anonymous user.

In order to execute a remote command on the FTP server as 'ls' :

lftp john@192.168.1.10:~> ls
-rw-r--r-- 1 1001 1001 10240000 Feb 22 22:08 file
-rw-r--r-- 1 1001 1001 21820 Feb 23 20:06 install.log


In order to execute a local command on the FTP client as 'ls' :

lftp john@192.168.1.10:~> ! ls
file install.log install.log.syslog test


To download a file from FTP server use 'get' command :

lftp john@192.168.1.10:~> get file
10240000 bytes transferred


To upload a file from FTP client to the FTP server use 'put' command :

lftp john@192.168.1.10:~> put install.log
21820 bytes transferred


More info on 'man lftp'.

 

Questions

 

1.- By default FTP data transfer is encrypted (true/false).

 

2.- FTP protocol can be used to transfer files between Linux, Unix and Microsoft Windows S.O. (true/false).

 

3.- In order to get working an FTP server through a firewall the only action required is open 21/TCP port (true/false).

 

4.- Which configuration parameter on file /etc/vsftpd/vsftpd.conf must be configured in order to allow anonymous login on the FTP server?.

 

5.- Which configuration parameter on file /etc/vsftpd/vsftpd.conf must be configured in order disable local logins on the FTP server?.

 

6.- Which configuration parameter on file /etc/vsftpd/vsftpd.conf must be configured in order disable logins from users listed in /etc/vsftpd/vsftpd.conf on the FTP server?.

 

7.- Which command can be used in order to disable SElinux protection to the ftpd service?.

 

8.- Which command can be used in order to give SElinux access to users logged through FTP client on their home directories on the FTP server?.

 

9.- Which command can be used in order to connect to the FTP server using anonymous account?.

A - lftp 192.168.1.10
B - lftp -u anonymous 192.168.1.10
C - Both of them
D - None of them

10.- Which configuration parameter on /etc/vsftpd/vsftpd.conf makes that the only users listed on /etc/vsftpd/user_list are allowed to connect to the FTP server ?.

A - userlist_deny=YES
B - userlist_deny=NO
C - /etc/export
D - /etc/fstab

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM