linnux 之网络文件共享服务专题

基本概念和原理

1 存储类型

 

 

存储类型分为三种直连式存储:Direct-Attached Storage简称DAS网络附加存储

Network-Attached Storage,简称NAS存储区域网络

Storage Area Network,简称SAN

存储基础知识---存储网络

 

 

 存储基础知识---存储网络

 

 

 

 存储基础知识---存储网络

 

 

 存储基础知识---存储网络

 

 

 存储基础知识---存储网络

 

 

 ==============================================================进入实战==================================================================================================

1、实现基于MYSQL验证的vsftpd虚拟用户访问
FTP工作原理介绍
文件传输协议:File Transfer Protocol 早期的三个应用级协议之一,
基于C/S结构数据传输格式:二进制(默认)和文本双通道协议:
命令和数据连接
两种模式:从服务器角度
主动(PORT style):服务器主动连接
命令(控制):客户端:随机port ---> 服务器:21/tcp数据
客户端:随机port <---服务器:20/tcp
被动(PASV style):客户端主动连接
命令(控制):客户端:随机port ---> 服务器:21/tcp数据:
客户端:随机port ---> 服务器:随机port /tcp
FTP服务状态码:

 

 用户认证:

 

 FTP服务器端和客户端常用软件

 

 

 

 

=============================================================================================================================================

1. 环境网络拓扑

192.168.170.17 #数据库服务器
192.168.170.7  #应用服务器
192.168.170.6 # 测试客户端

2.数据库服务器17上

#建立存储虚拟用户数据库和表
MariaDB [vsftpd]> CREATE DATABASE vsftpd;
MariaDB [vsftpd]> USE vsftpd;
MariaDB [vsftpd]> CREATE TABLE users(id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,name CHAR(50) BINARY NOT NULL,password CHAR(48) BINARY NOT NULL);
#添加虚拟用户,为了安全应该使用PASSWORD函数加密其密码后存储
mysql> INSERT INTO users(name,password) values('ftp_user1',password('111111'));
mysql> INSERT INTO users(name,password) values('ftp_user2',password('111111'));
=====================================
MariaDB [vsftpd]> select * from users;
+----+-----------+-------------------------------------------+
| id | name      | password                                  |
+----+-----------+-------------------------------------------+
|  1 | ftp_user1 | *FD571203974BA9AFE270FE62151AE967ECA5E0AA |
|  2 | ftp_user2 | *FD571203974BA9AFE270FE62151AE967ECA5E0AA |
=========================================
#创建连接的数据库用户
mysql>GRANT SELECT ON vsftpd.* TO vsftpd@'192.168.170.%' IDENTIFIED BY '111111';
mysql> FLUSH PRIVILEGES;

2.在FTP服务器上192.168.170.7安装vsftpd 和 pam_mysql包

[root@centos7 ~]#yum install vsftpd  -y
 #在FTP服务器上安装 pam_mysql对于 centos 6:pam_mysql由EPEL的源中提供,对于 centos7 和 8:无对应rpm包,需手动编译安装
[root@chujiapeng ~]# cd /data/
ll

-rw-r--r-- 1 root root 335240 Jan 3 15:33 pam_mysql-0.7RC1.tar.gz

#安装相关包

yum -y install vsftpd gcc gcc-c++ make mariadb-devel pam-devel

#编译安装pam_mysql

[root@chujiapeng data]# tar -xf pam_mysql-0.7RC1.tar.gz 

[root@chujiapeng pam_mysql-0.7RC1]# ./configure --with-pam-mods-dir=/lib64/security

[root@chujiapeng pam_mysql-0.7RC1]# make  && make install

[root@chujiapeng pam_mysql-0.7RC1]# ll /lib64/security/pam_mysql.*
-rwxr-xr-x 1 root root 882 Jan 3 15:48 /lib64/security/pam_mysql.la
-rwxr-xr-x 1 root root 141696 Jan 3 15:48 /lib64/security/pam_mysql.so

在FTP服务器上建立pam认证所需文件

[root@chujiapeng pam_mysql-0.7RC1]# vim /etc/pam.d/vsftp.sh

#添加如下两行
auth required pam_mysql.so user=vsftpd passwd=111111 host=192.168.170.17 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required pam_mysql.so user=vsftpd passwd=111111 host=192.168.170.17 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

 

 

  4.建立相应用户和修改vsftpd配置文件

#建立虚拟用户映射的系统用户及对应的目录

[root@chujiapeng pam_mysql-0.7RC1]# useradd -s /sbin/nologin -d /data/ftproot -r vuser

#centos7 需除去ftp根目录的写权限

[root@chujiapeng pam_mysql-0.7RC1]# chmod 555 /data/ftproot/

[root@chujiapeng pam_mysql-0.7RC1]# ll /data/ftproot/ -d
dr-xr-xr-x 3 root root 20 Jan 3 16:37 /data/ftproot/

#给上传下载目录读写执行全新

[root@chujiapeng pam_mysql-0.7RC1]# setfacl -m u:vuser:rwx /data/ftproot/upload

#确保/etc/vsftpd/vsftpd.conf中已经启用了以下选项

vim /etc/vsftpd/vsftpd.conf

anonymous_enable=YES

#添加下面两项

guest_enable=YES

guest_username=vuser

#修改下面一项,原系统用户无法登录

pam_service_name=vsftp.sh

#启动vsftpd服务

systemctl restart vsftpd

[root@centos7 ~]#systemctl enable --now vsftpd

测试:用客户端接连如下截图

 

 



 5.在FTP服务器上配置虚拟用户具有不同的访问权限

vsftpd可以在配置文件目录中为每个用户提供单独的配置文件以定义其ftp服务访问权限,每个虚拟用户的配置文件名同虚拟用户的用户名。配置文件目录可以是任意未使用目录,只需要在vsftpd.conf指定其路径及名称即可

#添加如下选项
vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/conf.d/

#创建所需要目录,并为虚拟用户提供配置文件

mkdir /etc/vsftpd/conf.d/

cd /etc/vsftpd/conf.d/

vim ftp_user1

anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

 

 

 



 

 验证ftp_user1是否有上传权限

[root@chujiapeng ~]# ftp 192.168.170.7
Connected to 192.168.170.7 (192.168.170.7).
220 (vsFTPd 3.0.2)
Name (192.168.170.7:root): ftp_user1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ll
?Invalid command
ftp> ls
227 Entering Passive Mode (192,168,170,7,195,124).
150 Here comes the directory listing.
drwxrwxr-x    2 0        0               6 Jan 03 08:37 upload
226 Directory send OK.
ftp> cd upload
250 Directory successfully changed.
ftp> !ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates  wo.txt
Desktop         Downloads  Music          Public    Videos
ftp> put wo.txt
local: wo.txt remote: wo.txt
227 Entering Passive Mode (192,168,170,7,44,93).
150 Ok to send data.
226 Transfer complete.
ftp> ls
227 Entering Passive Mode (192,168,170,7,70,244).
150 Here comes the directory listing.
-rw-------    1 989      984             0 Jan 03 10:11 wo.txt
226 Directory send OK.
ftp> 
过程细节如下截图
测试另外一个账号

[root@chujiapeng ~]# ftp 192.168.170.7
Connected to 192.168.170.7 (192.168.170.7).
220 (vsFTPd 3.0.2)
Name (192.168.170.7:root): ftp_user2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ll
?Invalid command
ftp> ls
227 Entering Passive Mode (192,168,170,7,212,130).
150 Here comes the directory listing.
drwxrwxr-x 2 0 0 20 Jan 03 10:11 upload
226 Directory send OK.
ftp> cd upload
250 Directory successfully changed.
ftp> !ls
anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos wo.txt
ftp> !touch 1.txt
ftp> !ls
1.txt anaconda-ks.cfg Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public Templates Videos wo.txt
ftp> put 1.txt
local: 1.txt remote: 1.txt
227 Entering Passive Mode (192,168,170,7,81,56).
550 Permission denied.

为ftp_user2用户指定根目录

[root@chujiapeng conf.d]# vim /etc/vsftpd/conf.d/ftp_user2

local_root=/data/ftproot2/

mkdir /data/ftproot2

touch /data/ftproot2/ftproot2

#重启服务

测试

systemctl restart vsftpd

[root@chujiapeng ~]# ftp 192.168.170.7
Connected to 192.168.170.7 (192.168.170.7).
220 (vsFTPd 3.0.2)
Name (192.168.170.7:root): ftp_user2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,170,7,205,207).
150 Here comes the directory listing.
-rw-r--r-- 1 0 0 0 Jan 03 10:23 ftproot2
226 Directory send OK.
ftp>

 



 

 

 

 

 

 

 

 

 2、通过NFS实现服务器/www共享访问。

 

 

 

 

 

 

=======================================================================================开始搭建=====================================================================================================

环境 
192.168.170.7 NFS 服务器
192.168.170.6 NFS  测试服务器
192.168.170.7 NFS 服务器上
#查看状态启动服务

systemctl status nfs

systemctl start nfs-server
systemctl enable nfs-server

#创建共享目录/www

mkdir /www

#创建配置文件

vim /etc/exports.d/test.exports

/wwww *(rw)

exportfs -r #生效配置文件

接下来配置客户端#在6上查看7的共享目录

showmount -e 192.168.170.7
#把挂载点写入配置文件中
[root@cjiapeng ~]# vim /etc/fstab

192.168.170.7:/www /mnt/nfs1

192.168.170.7:/www /data/nfs1 nfs defaults 0 0

[root@cjiapeng ~]# mount -a

[root@cjiapeng ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 103081248 7540076 90298292 8% /
tmpfs 502056 72 501984 1% /dev/shm
/dev/sda1 999320 34964 911928 4% /boot
/dev/sda3 5029504 10236 4757124 1% /data
192.168.170.7:/www 104806400 4290816 100515584 5% /data/nfs1

 


 

 

 

 

 

 

 

 

 

 

 

 

 

3、配置samba共享,实现/www目录共享

演示环境:192.168.170.17

[root@chujiapeng www]# yum install samba -y

[root@chujiapeng www]# rpm -ql samba

[root@chujiapeng www]# systemctl start smb

[root@chujiapeng www]# mkdir /www

客户端挂载

 

 


 

 [root@chujiapeng mnt]# rpm -ql samba-common

  

 

 

#创建用户
[root@chujiapeng ~]# useradd -s /sbin/nologin smb1
[root@chujiapeng ~]# useradd -s /sbin/nologin smb2
[root@chujiapeng ~]# useradd -s /sbin/nologin smb3
#将系统系统映射成samba用户

后访问,我们可以看到可正常访问

 

 

 

#把samb用户加到组管理
[root@chujiapeng ~]# groupmems -a smb1 -g admins
[root@chujiapeng ~]# groupmems -a smb2 -g admins
[root@chujiapeng ~]# groupmems -a smb3 -g admins
#察看
[root@chujiapeng ~]# groupmems -l -g admins
chu  fan  smb1  smb2  smb3

  

#准备配置文件
[root@chujiapeng ~]# vim /etc/samba/smb.conf

[test]
path = /www/
valid users=smb1,smb2,smb3,chu,fan,@admins
writeable = no
browseable = no

  

#客户端测试
[root@chujiapeng mnt]# smbclient -L 192.168.170.17 -U smb1%111111

  

 

 

#设置写权限
[root@chujiapeng mnt]# smbclient //192.168.170.17/test -U smb1%111111
Try "help" to get a list of possible commands.
smb: \> pwd
Current directory is \\192.168.170.17\test\
smb: \> pwd
Current directory is \\192.168.170.17\test\

[root@chujiapeng ~]# setfacl -m u:smb1:rwx,g:admins:rwx /www/*

  再次测试

 

4、使用rsync+inotify实现/www目录实时同步

实验环境
#作为 远程同步服务器
192.168.111.7 
#客户端为
192.168.111.156

1服务器上配置如下192.168.111.7

1.[root@chujiapeng www]# rpm -ql rsync 
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
/usr/bin/rsync
/usr/lib/systemd/system/rsyncd.service
/usr/lib/systemd/system/rsyncd.socket
/usr/lib/systemd/system/rsyncd@.service
/usr/share/doc/rsync-3.0.9
/usr/share/doc/rsync-3.0.9/COPYING
/usr/share/doc/rsync-3.0.9/NEWS
/usr/share/doc/rsync-3.0.9/OLDNEWS
/usr/share/doc/rsync-3.0.9/README
/usr/share/doc/rsync-3.0.9/support
/usr/share/doc/rsync-3.0.9/support/Makefile
/usr/share/doc/rsync-3.0.9/support/atomic-rsync
/usr/share/doc/rsync-3.0.9/support/cvs2includes
/usr/share/doc/rsync-3.0.9/support/deny-rsync
/usr/share/doc/rsync-3.0.9/support/file-attr-restore
/usr/share/doc/rsync-3.0.9/support/files-to-excludes
/usr/share/doc/rsync-3.0.9/support/git-set-file-times
/usr/share/doc/rsync-3.0.9/support/logfilter
/usr/share/doc/rsync-3.0.9/support/lsh
/usr/share/doc/rsync-3.0.9/support/mnt-excl
/usr/share/doc/rsync-3.0.9/support/munge-symlinks
/usr/share/doc/rsync-3.0.9/support/rrsync
/usr/share/doc/rsync-3.0.9/support/rsyncstats
/usr/share/doc/rsync-3.0.9/support/savetransfer.c
/usr/share/doc/rsync-3.0.9/tech_report.tex
/usr/share/man/man1/rsync.1.gz
/usr/share/man/man5/rsyncd.conf.5.gz
2.准备配置文件

vim /etc/rsyncd.conf

    uid = root

    gid = root
    use chroot = no
    max connections = 0
    ignore errors
    exclude = lost+found/
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    reverse lookup = no
    hosts allow = 192.168.111.0/24
    [www]
    path =/www
    comment = backup
    read only = no
    auth users = rsyncuser
    secrets file = /etc/rsync.pass

3.准备密码文件

[root@chujiapeng www]# echo "rsyncuser:111111" > /etc/rsync.pass

[root@chujiapeng www]# chmod 600 /etc/rsync.pass 

4.启动服务

[root@chujiapeng www]# systemctl start rsyncd

到此服务端配置完毕

2客户端上配置如下192.168.111.156

1.准备同步脚本
[root@chujiapeng data]# vim ryst.sh

#!/bin/bash
SRC='/data/www'
DEST='rsyncuser@192.168.111.7::www'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
done
2.准备客户端密码文件
[root@chujiapeng data]# echo "111111" > /etc/rsync.pass
[root@chujiapeng data]# chmod 600 /etc/rsync.pass 
3.启动 
./ryst.sh

测试,在客户端启动脚本

 

 2.打开另外一个终端

创建一个文件夹 和一个文件

 

 

3.查看日志

 

 

4.服务端查看

 

 到此实验完毕

 

 

 

 

posted on 2021-01-03 16:01  jiapengchu  阅读(197)  评论(0编辑  收藏  举报

导航