抄袭来源
vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序。特点是小巧轻快,安全易用。
以下操作都是在关闭防火墙的情况下进行的。
一、配置vsftpd 安装
编辑
1 vi /etc/vsftpd/vsftpd.conf
配置文件详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 anonymous_enable =NO local_enable =YES write_enable =YES local_umask =022 dirmessage_enable =YES xferlog_enable =YESxferlog_std_format=YES xferlog_file =/var/log/xferlog connect_from_port_20 =YES ascii_upload_enable =NO ascii_download_enable =NO chroot_local_user =YES chroot_list_enable =YES chroot_list_file =/etc/vsftpd/chroot_list pam_service_name =vsftpd tcp_wrappers =YES allow_writeable_chroot =YES listen_port =60021 pasv_enable =YES pasv_min_port =65400 pasv_max_port =65410 local_root =/data/ftp/ use_localtime =YES
像我配置ftp时,就只需要进行追加以下内容
1 2 3 4 5 6 7 8 9 local_root =/data/ftp/ chroot_local_user =YES chroot_list_enable =YES chroot_list_file =/etc/vsftpd/chroot_list allow_writeable_chroot =YES use_localtime =YES
检查服务,并启动,设置开机自启动。
1 2 3 systemctl status vsftpd systemctl start vsftpd systemctl enable vsftpd
二、添加ftp专用用户 Linux useradd命令 | 菜鸟教程
创建用户,并指定用户使用的文件夹,-d表示用户登入时的默认文件夹。此操作会自动创建一级文件夹,如果是多级的,需要先把父级建出来
1 2 mkdir /data useradd -d /data/ftp/ ftpadmin
如果添加用户不对,可以删除,-r表示连同用户和用户文件夹一并删掉
设置密码
设置文件夹的所有者,-R
表示递归设置子目录
1 chown -R ftpadmin /data/ftp
如果想要给其他用户授权,可以执行chmod,777就是给所有者、同组用户、其他组用户开放所有权限。
r=4 w=2 x=1 rwx=4+2+1=7
r: read
w:write
x:execute
并不一定是777,可以根据需求赋值,比如744,就是除了所有者,都只有读取权限
搞完以上步骤,重启ftp,测试连接
1 2 3 4 5 6 7 8 [root@localhost ~]# yum -y install ftp [root@localhost ~]# ftp 192.168.1.128 Connected to 192.168.1.128 (192.168.1.128). 220 (vsFTPd 3.0.2) Name (192.168.1.128:root): ftpadmin 331 Please specify the password. Password: 500 OOPS: chroot
出现choot,表示没有权限,此时其实已经授权了,但是被seLinux这玩意给拦截了,这是linux核心自带的控制访问权限的。查看 SELinux状态及关闭SELinux_孤星雨的技术博客_51CTO博客
查看selinux状态,如果是enforcing,就临时改为permissive模式,再测试ftp是否可用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 [root@localhost ~]# /usr/sbin/sestatus -v SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 31 Process contexts: Current context: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 Init context: system_u:system_r:init_t:s0 /usr/sbin/sshd system_u:system_r:sshd_t:s0-s0:c0.c1023 File contexts: Controlling terminal: unconfined_u:object_r:user_devpts_t:s0 /etc/passwd system_u:object_r:passwd_file_t:s0 /etc/shadow system_u:object_r:shadow_t:s0 /bin/bash system_u:object_r:shell_exec_t:s0 /bin/login system_u:object_r:login_exec_t:s0 /bin/sh system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0 /sbin/agetty system_u:object_r:getty_exec_t:s0 /sbin/init system_u:object_r:bin_t:s0 -> system_u:object_r:init_exec_t:s0 /usr/sbin/sshd system_u:object_r:sshd_exec_t:s0 [root@localhost ~]# getenforce Enforcing [root@localhost ~]# setenforce 0 [root@localhost ~]# /usr/sbin/sestatus -v SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: permissive Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 31 Process contexts: Current context: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 Init context: system_u:system_r:init_t:s0 /usr/sbin/sshd system_u:system_r:sshd_t:s0-s0:c0.c1023 File contexts: Controlling terminal: unconfined_u:object_r:user_devpts_t:s0 /etc/passwd system_u:object_r:passwd_file_t:s0 /etc/shadow system_u:object_r:shadow_t:s0 /bin/bash system_u:object_r:shell_exec_t:s0 /bin/login system_u:object_r:login_exec_t:s0 /bin/sh system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0 /sbin/agetty system_u:object_r:getty_exec_t:s0 /sbin/init system_u:object_r:bin_t:s0 -> system_u:object_r:init_exec_t:s0 /usr/sbin/sshd system_u:object_r:sshd_exec_t:s0
注意,上面这个操作是临时暂停seLinux。
如果想要永久关闭,需要修改/etc/selinux/config 文件。
将SELINUX=enforcing改为SELINUX=disabled,之后重启机器才可。
重新进行测试,出现下面结果表示使用正常了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@localhost ~]# ftp 192.168.1.128 Connected to 192.168.1.128 (192.168.1.128). 220 (vsFTPd 3.0.2) Name (192.168.1.128:root): ftpadmin 331 Please specify the password. Password: 230 Login successful. Remote system type is UNIX. Using binary mode to transfer files. ftp> put /root/anaconda-ks.cfg test.cfg local: /root/anaconda-ks.cfg remote: test.cfg 227 Entering Passive Mode (192,168,1,128,161,79). 150 Ok to send data. 226 Transfer complete. 1229 bytes sent in 1.8e-05 secs (68277.78 Kbytes/sec) ftp> ls 227 Entering Passive Mode (192,168,1,128,177,209). 150 Here comes the directory listing. -rw-r--r-- 1 1000 1000 1229 Apr 08 00:39 test.cfg 226 Directory send OK.
使用xshell或者xftp直接选择ftp协议、21端口,连接即可。