网络文件服务及linux防火墙

2020-04-26  本文已影响0人  jamas

1、实现基于MYSQL验证的vsftpd虚拟用户访问

主机 os 软件 ip
vsftp服务器 centos7.6 vsftpd pam_mysql 172.16.2.137
mysql服务器 centos7.6 mariadb-server 172.16.2.138
[root@node4 ~]# yum install -y mariadb-server
[root@node4 ~]# systemctl start mariadb

创建vsftp所需数据库、表,生成连接mysql用户

[root@node4 ~]# mysql
# 新建数据库
MariaDB [(none)]> create database vsftpd;
Query OK, 1 row affected (0.00 sec)
# 新建表
MariaDB [(none)]> use vsftpd
Database changed
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
    -> );
Query OK, 0 rows affected (0.06 sec)
# 表中录入数据(ftp用户及密码)
MariaDB [vsftpd]> insert users (name,password) value('ftpuser1',password('centos'));
Query OK, 1 row affected (0.02 sec)

MariaDB [vsftpd]> insert users (name,password) value('ftpuser2',password('magedu'));
Query OK, 1 row affected (0.01 sec)
# 生成连接mysql用户
MariaDB [vsftpd]> grant select on vsftpd.users to vsftpd@'172.16.2.%' identified by  '123456';
Query OK, 0 rows affected (0.00 sec)
yum -y groupinstall "Development Tools"
yum -y install mariadb-devel pam-devel vsftpd

编译安装pam_mysql

[root@node3 ~]# tar xvf pam_mysql-0.7RC1.tar.gz
[root@node3 ~]# cd pam_mysql-0.7RC1/
[root@node3 pam_mysql-0.7RC1]# ./configure  --with-pam-mods-dir=/lib64/security/
[root@node3 pam_mysql-0.7RC1]# make && make install

准备ftp目录

[root@node3 /]# mkdir -p /data/ftproot/upload
[root@node3 /]# mkdir -p /data/ftproot/upload
[root@node3 /]# chmod 555 /data/ftproot //去掉目录写权限
[root@node3 /]# setfacl -m u:vuser:rwx/data/ftproot/upload //针对vuser设置权限

编译vsftpd配置文件

[root@node3 /]# vim /etc/vsftpd/vsftpd.conf
pam_service_name=vsftpd.mysql  //指定pam认证文件
guest_enable=YES
guest_username=vuser  //指定用户
user_config_dir=/etc/vsftpd/vusers.d/  //指定各用户配置文件目录

为不同的ftp用户单独配置各自的权限

[root@node3 /]# mkdir /etc/vsftpd/vusers.d/  
[root@node3 /]# vim /etc/vsftpd/vusers.d/ftpuser1
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

[root@node3 /]# vim /etc/vsftpd/vusers.d/ftpuser2
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
local_root=/data/ftproot2

为不同的ftp用户准备目录

[root@node3 /]# mkdir /data/ftproot2
[root@node3 /]# chmod 555 /data/ftproot2
[root@node3 /]# mkdir /data/ftproot2/upload
[root@node3 /]# setfacl -m u:vuser:rwx /data/ftproot2/upload

新建pam认证文件

[root@node3 /]# vim /etc/pam.d/vsftpd.mysql
auth required pam_mysql.so user=vsftpd passwd=123456 host=172.16.2.138 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required pam_mysql.so user=vsftpd passwd=123456 host=172.16.2.138 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

启动服务

[root@node3 /]# systemctl start vsftpd
[root@node4 ~]# ftp 172.16.2.137
Connected to 172.16.2.137 (172.16.2.137).
220 (vsFTPd 3.0.2)
Name (172.16.2.137:root): ftpuser1
331 Please specify the password.
Password:
230 Login successful.
ftp> cd upload
250 Directory successfully changed.
ftp> put anaconda-ks.cfg  
local: anaconda-ks.cfg remote: anaconda-ks.cfg
227 Entering Passive Mode (172,16,2,137,236,176).
150 Ok to send data.
226 Transfer complete.
1357 bytes sent in 0.00815 secs (166.44 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (172,16,2,137,48,202).
150 Here comes the directory listing.
-rw-------    1 1000     1000         1357 Apr 25 15:25 anaconda-ks.cfg
226 Directory send OK.


[root@node4 ~]# ftp 172.16.2.137
Connected to 172.16.2.137 (172.16.2.137).
220 (vsFTPd 3.0.2)
Name (172.16.2.137:root): ftpuser2
331 Please specify the password.
Password:
230 Login successful.

ftp> cd upload
250 Directory successfully changed.
ftp> put anaconda-ks.cfg
local: anaconda-ks.cfg remote: anaconda-ks.cfg
227 Entering Passive Mode (172,16,2,137,83,141).
150 Ok to send data.
226 Transfer complete.
1357 bytes sent in 5.8e-05 secs (23396.55 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (172,16,2,137,230,150).
150 Here comes the directory listing.
-rw-------    1 1000     1000         1357 Apr 25 15:31 anaconda-ks.cfg
226 Directory send OK.

在ftp服务端查看目录,可以看到上传至不同的目录

[root@node3 pam_mysql-0.7RC1]# tree /data
/data
├── ftproot
│   └── upload
│       └── anaconda-ks.cfg
└── ftproot2
    └── upload
        └── anaconda-ks.cfg

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

主机 os 软件 ip
nfs服务端 centos7.6 nfs nfs-utils 172.16.2.131
nfs客户端 centos7.6 - 172.16.2.132
[root@node1 /]#yum install -y nfs-utils

新建共享目录

[root@node1 /]#mkdir www

新建nfs所需用户和组

[root@node1 /]#groupadd -g 600 nfsuser
[root@node1 /]#useradd -u 600 -g 600 -r nfsuser -s /sbin/nologin

设置共享目录权限

[root@node1 /]# setfacl -m u:nfsuser:rwx /www

编辑nfs配置文件

[root@node1 /]# vim /etc/exports
/www    172.16.2.0/24(rw,all_squash,anonuid=600,anongid=600)  指定以nfsuser用户共享

重载配置文件

[root@node1 /]# exportfs -r

查看当前共享

[root@node1 /]# exportfs -v
/www            172.16.2.0/24(sync,wdelay,hide,no_subtree_check,anonuid=600,anongid=600,sec=sys,rw,root_squash,all_squash)

重启服务

[root@node1 /]# systemctl restart nfs
[root@node1 /]# systemctl restart rpcbind
[root@node2 ~]# showmount -e 172.16.2.131
Export list for 172.16.2.131:
/www 172.16.2.0/24

挂载共享目录

[root@node2 /]# mkdir /nfsdir
[root@node2 /]# mount 172.16.2.131:/www /nfsdir
[root@node2 nfsdir]# touch test1.txt
[root@node2 nfsdir]# ls
test1.txt

在服务器端查看文件属性(属主为nfsuser)

[root@node1 /]# ll /www
总用量 0
-rw-r--r-- 1 nfsuser nfsuser 0 4月  24 23:01 test1.txt

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

主机 os 软件 ip
samba服务端 centos7.6 samba 172.16.2.131
samba客户端 centos7.6 cifs-utils 172.16.2.132

安装Samba服务

[root@node1 ~]# yum install -y samba

新建Samba用户

[root@node1 ~]# useradd -s /sbin/nologin  smbuser
[root@node1 ~]# smbpasswd -a smbuser
New SMB password:
Retype new SMB password:

新建共享目录,并设置权限

[root@node1 ~]# mkdir /www
[root@node1 ~]# chmod 777 /www
[root@node1 ~]# touch /www/smb1.txt

编辑samba配置文件

[root@node1 www]# vim /etc/samba/smb.conf
[share]
        path = /www  //设置共享目录文件
        write list = smbuser  //配置可写用户列表

启动服务

[root@node1 www]# systemctl restart smb.service  nmb

安装cifs-utils软件

[root@node2 /]# yum install -y cifs-utils

挂载共享目录

[root@node2 /]# mkdir /mnt/smb
[root@node2 ~]# mount -o user=smbuser1,password=123456 //172.16.2.131/share /mnt/smb

查看目录内容

[root@node2 ~]# cd /mnt/smb
[root@node2 smb1]# ls
smb1.txt

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

实验环境

主机 os 软件 ip
rsync客户端 centos7.6 rsyncd 172.16.2.131
rsync服务端 centos7.6 inotify-tools 172.16.2.132

配置rsyncd服务配置文档

[root@node2 ~]# 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 = 172.16.2.0/24 //指明客户端地址范围
[backup]  
path = /backup/  //备份路径
comment = backup
read only = no //关闭只读
auth users = rsyncuser  //同步用户
secrets file = /etc/rsync.pass //认证文件

生成认证文件(同步用户、密码)

[root@node2 ~]# echo "rsyncuser:123456" > /etc/rsync.pass
[root@node2 ~]# chmod 600 /etc/rsync.pass

新建备份目录

[root@node2 ~]# mkdir /backup

启动服务

[root@node2 ~]# systemctl start rsyncd

设置epel源,安装inotify-tools

[root@node1 ~]# yum install inotify-tools

生成认证文件(只需密码)

[root@node1 ~]# echo "123456" > /etc/rsync.pass
[root@node1 ~]# chmod 600 /etc/rsync.pass

测试同步功能

[root@node1 /]# rsync -avz --password-file=/etc/rsync.pass /www/ rsyncuser@172.16.2.132::backup
sending incremental file list
./
1111
1111111

sent 177 bytes  received 57 bytes  468.00 bytes/sec
total size is 0  speedup is 0.00

编辑实时同步脚本

[root@node1 /]# vim  /data/backup.sh
#!/bin/bash
SRC='/www/' DEST='rsyncuser@172.16.2.132::backup'
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

运行脚本

[root@node1 /]# bash /data/backup.sh
[root@node1 ~]# touch /www/1233535

修改后立即查看备份目录修改情况

[root@node2 ~]# ll /backup/
总用量 0
-rw-r--r-- 1 root root 0 4月  26 09:14 1111
-rw-r--r-- 1 root root 0 4月  26 09:16 1111111
-rw-r--r-- 1 root root 0 4月  26 09:21 1233535
-rw-r--r-- 1 root root 0 4月  26 08:45 test

5、使用iptable实现: 放行telnet, ftp, web服务,放行samba服务,其他端口服务全部拒绝

加载模块

[root@node1 netfilter]# vim /etc/sysconfig/iptables-config
IPTABLES_MODULES="nf_conntrack_ftp"
[root@node1 netfilter]# modprobe nf_conntrack_ftp

编辑iptables规则

[root@node1 netfilter]# iptables -A INPUT -p tcp --dport 23 -j ACCEPT
[root@node1 netfilter]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@node1 netfilter]# iptables -A INPUT -p tcp -m multiport --dport 139,389,445 -j ACCEPT
[root@node1 netfilter]# iptables -A INPUT -p udp -m multiport --dport 137,138 -j ACCEPT
[root@node1 netfilter]# iptables -A INPUT -p tcp  --dport 21 -m state --state NEW -j ACCEPT
[root@node1 netfilter]# iptables -A INPUT  -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@node1 netfilter]# iptables -A OUTPUT  -m state --state ESTABLISHED -j ACCEPT
[root@node1 netfilter]# iptables -A INPUT -j DROP
[root@node1 netfilter]# iptables -A OUTPUT -j DROP

查看规则

[root@node1 netfilter]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:23
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport dports 139,389,445
   44  6588 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport dports 137,138
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:21 state NEW
  525 37900 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
   13  2170 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
  134 12748 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state ESTABLISHED
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0
上一篇下一篇

猜你喜欢

热点阅读