码迷,mamicode.com
首页 > 其他好文 > 详细

理论+实操:FTP服务详细介绍优化,创建虚拟用户

时间:2019-12-08 23:41:21      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:ash   virt   哈希   mon   get   using   mnt   session   force   

搭建FTP服务

一 :安装ftp服务

1.1 挂载镜像文件

技术图片
安装vsftpd软件包

1.2 也可以使用yum仓库

[root@localhost ~]# yum install vsftpd-sysvinit.x86_64  -y
Installed:
  vsftpd-sysvinit.x86_64 0:3.0.2-25.el7                                  

Complete!

1.3 相关文件路径

[root@localhost ~]# cd /etc/vsftpd/
[root@localhost vsftpd]# ls
ftpusers  user_list  vsftpd.conf  vsftpd_conf_migrate.sh

vsftpd.conf 配置文件

user_list 用户列表 ,通过修改配置文件去决定用户列表中的用户是否可以登录,即修改黑白名单的登录权限

  • 默认的配置文件参数如下
[root@localhost vsftpd]# grep -v ‘#‘ vsftpd.conf  ‘过滤出有效的执行参数‘
anonymous_enable=YES        ‘启用匿名用户‘
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=NO
listen_ipv6=YES

pam_service_name=vsftpd     ‘pam模块认证‘
userlist_enable=YES         ‘用户列表开启‘        
tcp_wrappers=YES

这个时候不做修改其实也可以用

[root@localhost vsftpd]# systemctl start vsftpd
[root@localhost vsftpd]# systemctl status vsftpd
● vsftpd.service - Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-12-07 14:35:44 CST; 22s ago
  Process: 93638 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
 Main PID: 93639 (vsftpd)
   CGroup: /system.slice/vsftpd.service
           └─93639 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

Dec 07 14:35:44 localhost.localdomain systemd[1]: Starting Vsftpd ftp ...
Dec 07 14:35:44 localhost.localdomain systemd[1]: Started Vsftpd ftp d...
Hint: Some lines were ellipsized, use -l to show in full.

二 : 优化ftp服务

2.1 需要注意的就是要关闭防火墙,还有setenforce

[root@localhost vsftpd]# systemctl stop firewalld.service 
[root@localhost vsftpd]# setenforce 0
[root@localhost vsftpd]# 

2.1.1 使用另一台主机,去登陆ftp服务器,使用匿名登陆的方式

C:\Users\GSY>ftp 192.168.247.140    ‘开启vsftpd的服务器ip‘
连接到 192.168.247.140。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.140:(none)): ftp
331 Please specify the password.
密码:
230 Login successful.
ftp>
ftp> pwd    ‘查看所在位置‘
257 "/"     ‘这里的根不代表服务器的根目录,代表vsftpd的站点‘
ftp> ls -a      ‘查看‘
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
.
..
centos7
ks.cfg
pub     ‘公共文件夹‘
226 Directory send OK.
ftp: 收到 32 字节,用时 0.00秒 16.00千字节/秒。
ftp>

可以到服务器的相应站点去查看

[root@localhost vsftpd]# ls -a /var/ftp
.  ..  centos7  ks.cfg  pub

在服务器上创建一个文件去测试

[root@localhost vsftpd]# echo "778899" > /var/ftp/test.txt
[root@localhost vsftpd]# ls -a /var/ftp
.  ..  centos7  ks.cfg  pub  test.txt
[root@localhost vsftpd]# cat /var/ftp/test.txt 
778899

去客户机查看

ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
centos7
ks.cfg
pub
test.txt        ‘增加对应的文件‘
226 Directory send OK.
ftp: 收到 35 字节,用时 0.01秒 7.00千字节/秒。
ftp>

2.2 客户机切换到e盘去进行下载测试

ftp> get test.txt
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for test.txt (7 bytes).
226 Transfer complete.
ftp: 收到 7 字节,用时 0.00秒 7000.00千字节/秒。
ftp>

技术图片

测试完下载,然后测试上传

技术图片

反馈权限不够

ftp> put 999.txt
200 PORT command successful. Consider using PASV.
550 Permission denied.      
ftp>

2.3 现在去修改服务器的vsftpd配置文件,使匿名登陆的权限最大化

#anon_mkdir_write_enable=YES        ‘代表匿名用户可以创建目录‘
#anon_upload_enable=YES         ‘代表匿名用户可以上传‘

将#号键去掉,保存退出

还可以使用man 去查看vsftpd.conf配置文件的用法

[root@localhost vsftpd]# man vsftpd.conf

进入man模式,:anon去查看关键信息

          Default: NO

   anon_other_write_enable
          If  set to YES, anonymous users will be permitted to per‐
          form write operations other than upload and create direc‐
          tory,  such  as  deletion and renaming. This is generally
          not recommended but included for completeness.

          Default: NO

   anon_upload_enable
          If set to YES,  anonymous  users  will  be  permitted  to
          upload  files under certain conditions. For this to work,
          the option write_enable must be activated, and the anony‐
          mous  ftp  user  must  have  write  permission on desired
          upload locations. This setting is also required for  vir‐
          tual  users  to  upload;  by  default,  virtual users are
          treated with anonymous (i.e. maximally restricted) privi‐
          lege.

权限默认为no

anon_other_write_enable 代表可以对文件重命名或者删除

把这条命令追加入到配置文件,可以令anon的权限最大化

可以使用过滤去查看验证功能是否开启

[root@localhost vsftpd]# grep -v ‘#‘ vsftpd.conf 
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
[root@localhost vsftpd]# 

2.4 修改过的配置文件,需要重新加载服务才能生效

[root@localhost vsftpd]# systemctl reload vsftpd
Failed to reload vsftpd.service: Job type reload is not applicable for unit vsftpd.service.
See system logs and ‘systemctl status vsftpd.service‘ for details.
[root@localhost vsftpd]# systemctl restart vsftpd
[root@localhost vsftpd]# service reload vsftpd
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
[root@localhost vsftpd]# service vsftpd reload
Reloading vsftpd configuration (via systemctl):  Failed to reload vsftpd.service: Job type reload is not applicable for unit vsftpd.service.
See system logs and ‘systemctl status vsftpd.service‘ for details.
                                                           [失败]

重新加载不好使,只好重启了

2.5 在vsftpd中将anon的权限最大化放宽,还要考虑到文件自身的权限有没有被放宽

[root@localhost vsftpd]# cd /var/ftp
[root@localhost ftp]# ls -al
total 12
drwxr-xr-x.  4 root root   62 Dec  7 14:53 .
drwxr-xr-x. 24 root root 4096 Nov 22 14:43 ..
drwxr-xr-x.  2 root root    6 Nov 22 14:51 centos7
-rw-r--r--.  1 root root  909 Nov 22 15:17 ks.cfg
drwxr-xr-x.  2 root root    6 Oct 31  2018 pub
-rw-r--r--.  1 root root    7 Dec  7 14:53 test.txt

先测试一下

ftp> put 999.txt
200 PORT command successful. Consider using PASV.
553 Could not create file.

依旧不可以,把ftp权限放开

[root@localhost ftp]# chmod 777 /var/ftp
[root@localhost ftp]# ls -al
total 12
drwxrwxrwx.  4 root root   62 Dec  7 14:53 .
drwxr-xr-x. 24 root root 4096 Nov 22 14:43 ..
drwxr-xr-x.  2 root root    6 Nov 22 14:51 centos7
-rw-r--r--.  1 root root  909 Nov 22 15:17 ks.cfg
drwxr-xr-x.  2 root root    6 Oct 31  2018 pub
-rw-r--r--.  1 root root    7 Dec  7 14:53 test.txt

可以了

ftp> put 999.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 7 字节,用时 0.00秒 3.50千字节/秒。

去服务端查看验证

[root@localhost ftp]# ls -al
total 16
drwxrwxrwx.  4 root root   77 Dec  7 15:32 .
drwxr-xr-x. 24 root root 4096 Nov 22 14:43 ..
-rw-------.  1 ftp  ftp     7 Dec  7 15:32 999.txt
drwxr-xr-x.  2 root root    6 Nov 22 14:51 centos7
-rw-r--r--.  1 root root  909 Nov 22 15:17 ks.cfg
drwxr-xr-x.  2 root root    6 Oct 31  2018 pub
-rw-r--r--.  1 root root    7 Dec  7 14:53 test.txt

同理也可以去改pub

2.6 接下来测试删除自己上传的文件

ftp> delete 999.txt
250 Delete operation successful.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
centos7
ks.cfg
pub
test.txt
226 Directory send OK.
ftp: 收到 35 字节,用时 0.00秒 35.00千字节/秒。
[root@localhost ftp]# ls -al
total 12
drwxrwxrwx.  4 root root   62 Dec  7 15:36 .
drwxr-xr-x. 24 root root 4096 Nov 22 14:43 ..
drwxr-xr-x.  2 root root    6 Nov 22 14:51 centos7
-rw-r--r--.  1 root root  909 Nov 22 15:17 ks.cfg
drwxr-xr-x.  2 root root    6 Oct 31  2018 pub
-rw-r--r--.  1 root root    7 Dec  7 14:53 test.txt

测试删除原有的文件

ftp> delete test.txt
250 Delete operation successful.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
centos7
ks.cfg
pub
226 Directory send OK.
ftp: 收到 25 字节,用时 0.00秒 12.50千字节/秒。
[root@localhost ftp]# ls -al
total 8
drwxrwxrwx.  4 root root   46 Dec  7 15:37 .
drwxr-xr-x. 24 root root 4096 Nov 22 14:43 ..
drwxr-xr-x.  2 root root    6 Nov 22 14:51 centos7
-rw-r--r--.  1 root root  909 Nov 22 15:17 ks.cfg
drwxr-xr-x.  2 root root    6 Oct 31  2018 pub

也可以执行

三 :接下来测试普通用户

[root@localhost ftp]# useradd zhangsan
[root@localhost ftp]# useradd lisi
[root@localhost ftp]# echo "123123" | passwd zhangsan --stdin
Changing password for user zhangsan.
passwd: all authentication tokens updated successfully.
[root@localhost ftp]# echo "123123" | passwd lisi --stdin
Changing password for user lisi.
passwd: all authentication tokens updated successfully.

接下来在客户机以新创建的用户去远程登陆

E:\>ftp 192.168.247.140
连接到 192.168.247.140。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.140:(none)): zhangsan
331 Please specify the password.
密码:
230 Login successful.
ftp>
ftp>
ftp>
ftp> pwd
257 "/home/zhangsan"

3.1 可以发现在自己的家目录下

接下来进行上传测试

ftp> put 999.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 7 字节,用时 0.00秒 7.00千字节/秒。
[root@localhost ftp]# ls /home/zhangsan
999.txt
[root@localhost ftp]# 

3.2 切换目录,发现普通用户可以随意切换

ftp> cd /
250 Directory successfully changed.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
226 Directory send OK.
ftp: 收到 107 字节,用时 0.01秒 9.73千字节/秒。

这对服务器安全而言是一种隐患

接下来去修改相应的配置选项

#chroot_local_user=YES      ‘禁止本地用户切换目录‘
allow_writeable_chroot=YES      ‘centos7中还需要额外开启这一项‘

启用它,保存退出,重启服务

ftp> ls
远程主机关闭连接。

使用bye命令退出ftp模式,重新登录

E:\>ftp 192.168.247.142
连接到 192.168.247.142。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.142:(none)): zhangsan
331 Please specify the password.
密码:
230 Login successful.
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
226 Directory send OK.
ftp> ls -a
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
.
..
.bash_logout
.bash_profile
.bashrc
.mozilla
226 Directory send OK.
ftp: 收到 58 字节,用时 0.00秒 14.50千字节/秒。
ftp> pwd
257 "/"
ftp> cd /etc
550 Failed to change directory.
ftp>

3.3 接下来配置用户名单user——list

如果使用userlist_deny=NO,则仅允许名单内的这些用户访问

如果使用userlist_deny=YES (默认),则不允许名单内的这些用户访问

可以把zhangsan追加进去测试

[root@localhost ftp]# echo "zhangsan" >> /etc/vsftpd/user_list 
[root@localhost ftp]# 

重新加载服务后再次登陆,发现登陆失败

E:\>ftp 192.168.247.140
连接到 192.168.247.140。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.140:(none)): zhansan
331 Please specify the password.
密码:
530 Login incorrect.
登录失败。
ftp>
ftp> ls
530 Please login with USER and PASS.
530 Please login with USER and PASS.
ftp>

增加用户列表限制,只允许列表内的用户访问,然后重启服务

[root@localhost ftp]# vim /etc/vsftpd/vsftpd.conf 
userlist_enable=YES         ‘代表用户列表开启‘
userlist_deny=NO        ‘增加仅允许用户列表内用户访问‘

测试,发现生效

E:\>ftp 192.168.247.140
连接到 192.168.247.140。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.140:(none)): lisi
530 Permission denied.
登录失败。
ftp> bye
221 Goodbye.

E:\>ftp 192.168.247.140
连接到 192.168.247.140。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.140:(none)): zhangsan
331 Please specify the password.
密码:
230 Login successful.
ftp>

把刚才增加的配置删除,继续测试

四 :虚拟用户

4.1 先创建/etc/vsftpd/vuser文件

里面写虚拟账户的账号和密码

[root@localhost ftp]# cd /etc/vsftpd/
[root@localhost vsftpd]# vim vuser
[root@localhost vsftpd]# cat vuser 
lisa
123123
tom
123123

4.2 把vuser文件转换成数据库文件

-T 代表转换 -t 代表类型 hash 代表哈希算法类型 -f 指定文件

[root@localhost vsftpd]# db_load -T -t hash -f vuser vuser.db
[root@localhost vsftpd]# 
[root@localhost vsftpd]# ls -al
total 48
drwxr-xr-x.   2 root root   117 Dec  7 16:37 .
drwxr-xr-x. 140 root root  8192 Dec  7 15:41 ..
-rw-------.   1 root root   125 Oct 31  2018 ftpusers
-rw-------.   1 root root   370 Dec  7 16:03 user_list
-rw-------.   1 root root  5142 Dec  7 16:11 vsftpd.conf
-rwxr--r--.   1 root root   338 Oct 31  2018 vsftpd_conf_migrate.sh
-rw-r--r--.   1 root root    23 Dec  7 16:33 vuser
-rw-r--r--.   1 root root 12288 Dec  7 16:37 vuser.db

为了安全,修改这两个文件为600权限

[root@localhost vsftpd]# chmod 600 vuse*
[root@localhost vsftpd]# ls -al
total 48
drwxr-xr-x.   2 root root   117 Dec  7 16:37 .
drwxr-xr-x. 140 root root  8192 Dec  7 15:41 ..
-rw-------.   1 root root   125 Oct 31  2018 ftpusers
-rw-------.   1 root root   370 Dec  7 16:03 user_list
-rw-------.   1 root root  5142 Dec  7 16:11 vsftpd.conf
-rwxr--r--.   1 root root   338 Oct 31  2018 vsftpd_conf_migrate.sh
-rw-------.   1 root root    23 Dec  7 16:33 vuser
-rw-------.   1 root root 12288 Dec  7 16:37 vuser.db

4.3 创建虚拟用户对应的账户

[root@localhost vsftpd]# useradd -d /opt/vuser -s /sbin/nologin vuser
[root@localhost vsftpd]# grep ‘vuser‘ /etc/passwd
vuser:x:1003:1003::/opt/vuser:/sbin/nologin
[root@localhost vsftpd]# ls /opt
192.168.139.132  rh  vuser

4.4 使用pam认证模块开启支持虚拟用户登陆

新建一个pam模块文件,/etc/pam.d/vsftpd.vu,在里面写入

auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=etc/vsftpd/vuser

注意,db后面的文件代表的是以db为后缀的文件

[root@localhost vsftpd]# cd /etc/pam.d/
[root@localhost pam.d]# cat vsftpd 
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
auth       required     pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
auth       required     pam_shells.so
auth       include      password-auth
account    include      password-auth
session    required     pam_loginuid.so
session    include      password-auth
[root@localhost pam.d]# vim vsftpd.vu
[root@localhost pam.d]# cat vsftpd.vu 
auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=etc/vsftpd/vuser
[root@localhost pam.d]# 

4.5 接下来开始修改配置文件

[root@localhost vsftpd]# vim vsftpd.conf 

#pam_service_name=vsftpd        ‘关掉这个命令‘
guest_enable=YES            ‘加入来宾功能‘
guest_username=vuser
pam_service_name=vsftpd.vu      ‘pam模块服务指定刚才新建的模块‘
[root@localhost pam.d]# tail -9 /etc/vsftpd/vsftpd.conf 
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES

#pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
guest_enable=YES
guest_username=YES
pam_service_name=vsftpd.vu

修改完毕,开始重启服务

4.6 接下来就可以使用虚拟用户登陆,上传文件

E:\>ftp 192.168.247.142
连接到 192.168.247.142。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.142:(none)): lisa
331 Please specify the password.
密码:
230 Login successful.
ftp>
ftp>
ftp>
ftp>
ftp>
ftp> put 999.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 7 字节,用时 0.00秒 7.00千字节/秒。
ftp>

查看服务端验证

[root@localhost vuser]# cd /etc/vsftpd/
[root@localhost vsftpd]# cd /opt/vuser
[root@localhost vuser]# ls
999.txt
[root@localhost vuser]# 

切换另一个用户

E:\>ftp 192.168.247.142
连接到 192.168.247.142。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.142:(none)): tom
331 Please specify the password.
密码:
230 Login successful.
ftp> put 911.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 发送 7 字节,用时 0.00秒 7.00千字节/秒。
ftp>
[root@localhost vuser]# ls -al
total 20
drwx------. 3 vuser vuser 108 Dec  8 16:46 .
drwxr-xr-x. 5 root  root  142 Dec  8 15:13 ..
-rw-------. 1 vuser vuser   7 Dec  8 16:46 911.txt
-rw-------. 1 vuser vuser   7 Dec  8 16:39 999.txt
-rw-r--r--. 1 vuser vuser  18 Aug  3  2017 .bash_logout
-rw-r--r--. 1 vuser vuser 193 Aug  3  2017 .bash_profile
-rw-r--r--. 1 vuser vuser 231 Aug  3  2017 .bashrc
drwxr-xr-x. 4 vuser vuser  39 Oct 23 13:35 .mozilla

4.7 还可以单独给用户创建配置目录,去指定用户的权限

[root@localhost vuser]# vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vu_dir      ‘添加‘
[root@localhost vuser]# cd /etc/vsftpd
[root@localhost vsftpd]# mkdir vu_dir
[root@localhost vsftpd]# cd vu_dir/
[root@localhost vu_dir]# vim tom
anon_umask=022
E:\>ftp 192.168.247.142
连接到 192.168.247.142。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用户(192.168.247.142:(none)): tom
331 Please specify the password.
密码:
230 Login successful.
ftp> put 744.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp>
[root@localhost vuser]# ls -al
total 20
drwx------. 3 vuser vuser 138 Dec  8 16:53 .
drwxr-xr-x. 5 root  root  142 Dec  8 15:13 ..
-rw-r--r--. 1 vuser vuser   0 Dec  8 16:53 744.txt
-rw-------. 1 vuser vuser   0 Dec  8 16:50 755.txt
-rw-------. 1 vuser vuser   7 Dec  8 16:46 911.txt
-rw-------. 1 vuser vuser   7 Dec  8 16:39 999.txt
-rw-r--r--. 1 vuser vuser  18 Aug  3  2017 .bash_logout
-rw-r--r--. 1 vuser vuser 193 Aug  3  2017 .bash_profile
-rw-r--r--. 1 vuser vuser 231 Aug  3  2017 .bashrc
drwxr-xr-x. 4 vuser vuser  39 Oct 23 13:35 .mozilla

理论+实操:FTP服务详细介绍优化,创建虚拟用户

标签:ash   virt   哈希   mon   get   using   mnt   session   force   

原文地址:https://blog.51cto.com/14558445/2456992

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!