▲就业班和全程班的小伙伴看这里:(学习老王视频的作业第35-36节)
1、实现基于MYSQL验证的vsftpd虚拟用户访问
实验环境:
一台ftp服务器:192.168. 37.7
一台mariadb服务器:192.168. 37.17
配置过程:
1 mariadb服务器-37.17
#yum install mariadb-server
#systemctl start mariadb
#mysql
mysql> create database vsftpd;
mysql> use vsftpd;
mysql> CREATE TABLE users (
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
name CHAR(50) BINARY NOT NULL,
password CHAR(48) BINARY NOT NULL
);
mysql> desc users;
mysql> CREATE TABLE users ( id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, name CHAR(50) BINARY NOT NULL, password CHAR(48) BINARY NOT NULL );
mysql> insert users (name,password) value('ftpuser1',password('centos'));
mysql> insert users (name,password) value('ftpuser2',password('magedu'));
mysql> grant select on vsftpd.users to vsftpd@'192.168.37.%' identified by 'centos';
2 编译安装pam_mysql-37.7
#tar xvf pam_mysql-0.7RC1.tar.gz
#cd pam_mysql-0.7RC1/
#yum install gcc gcc-c++ pam-devel mariadb-devel
#./configure --with-pam-mods-dir=/lib64/security/
#make && make install
#vim /etc/pam.d/vsftpd.mysql
auth required pam_mysql.so user=vsftpd passwd=centos
host=192.168.37.17 db=vsftpd table=users usercolumn=name
passwdcolumn=password crypt=2
account required pam_mysql.so user=vsftpd passwd=centos
host=192.168.37.17 db=vsftpd table=users usercolumn=name
passwdcolumn=password crypt=2
:wq
3 配置ftp服务器-37.7
#yum install vsftpd
#useradd -d /data/ftproot -s /sbin/nologin vuser
#chmod 555 /data/ftproot
#mkdir /data/ftproot/upload
#setfacl -m u:vuser:rwx /data/ftproot/upload
#vim /etc/vsftpd/vsftpd.conf
pam_service_name=vsftpd.mysql
guest_enable=YES
guest_username=vuser
user_config_dir=/etc/vsftpd/vusers.d/
:wq
#mkdir /etc/vsftpd/vusers.d/
#vim /etc/vsftpd/vusers.d/ftpuser1
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
:wq
#vim /etc/vsftpd/vusers.d/ftpuser2
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
local_root=/data/ftproot2
:wq
#mkdir /data/ftproot2
#chmod 555 /data/ftproot2
#mkdir /data/ftproot2/upload
#setfacl -m u:vuser:rwx /data/ftproot2/upload
2、通过NFS实现服务器/www共享访问。
实验环境:
NFS服务器:192.168.37.7
客户端:192.168.37.17
配置过程:
1、NFS端操作-37.7
#systemctl start nfs-server
#mkdir /www
#cp /etc/fstab /www/f1.txt
#cp /etc/fstab /www/f2.txt
#useradd -r -s /bin/nologin -u 987 apache
#vim /etc/exports
/www 192.168.37.0/24(rw,all_squash,anonuid=987,anongid=987)
:wq
#exportfs -r
#setfacl -m u:apache:rwx /www
2、客户端操作
#showmount -e 192.168.37.7
Export list for 192.168.37.7:
/www
#mount 192.168.37.7:/www /mnt/www
#vim /etc/fstab
192.168.37.7:/www /mnt/www nfs defaults 0 0
:wq
3、配置samba共享,实现/www目录共享
1 安装服务:
#yum install samba
#systemctl start smb
2 创建系统账号
#useradd -s /sbin/nologin smbuser1
#useradd -s /sbin/nologin smbuser2
#useradd -s /sbin/nologin smbuser3
3 变为SMB账号(将系统账号加入SMB数据库,输入两次密码centos)
#smbpasswd -a smbuser1
#smbpasswd -a smbuser2
#smbpasswd -a smbuser3
4 查看smb数据库
#ll /var/lib/samba/private/passdb.tdb
#pdbedit -L
#pdbedit -L -v
#smbstatus
5 WIN客户端访问UNC
\\192.168.37.7 输入用户名及密码 smbuser1 可以进入共享的文件夹
6 Linux命令行方式
#smbclient -L 192.168.37.1 -U smbuser1%centos
#smbclient //192.168.37.7/home/smbuser1 -U smbuser1%centos
[root@centos6~]#mount -o sec=ntlmssp, username=smbuser1, password=centos //192.168.253.1home/smbuser1
[root@centos7~]#mount -o username=smbuser1, password=centos //192.168.253.1home/smbuser1
4、使用rsync+inotify实现/www目录实时同步
实验环境:
192.168.37.7:安装inotify
192.168.37.17:安装配置rsync
实验过程:
1、安装配置inotify-37.7
[37.7]#epel
[37.7]#yum install inotify-tools
2、安装配置rsync-37.17
2.1 查询rsync包是否安装,如果没装,用yum安装
[root@37.17]#rpm -qf `which rsync`
rsync-3.1.2-4.el7.x86_64
2.2 修改rsync配置文件
[root@37.17]#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.37.0/24
[backup]
path = /backupdir/
comment = backupdir
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass
:wq
2.3 端生成验证文件,用于存放远程登录rsync服务器(用户名及密码)同步数据
[root@37.17]#echo "rsyncuser:magedu" > /etc/rsync.pass
[root@37.17]#chmod 600 /etc/rsync.pass
2.4 服务器端准备同步备份的目录
[root@37.17]#mkdir /backupdir
2.5 启动服务并设为开机启动
[root@37.17]#systemctl start rsyncd
[root@37.17]#systemctl enable rsyncd
3、客户端配置密码文件-37.7
[37.7]#echo "magedu" > /etc/rsync.pass
[37.7]#chmod 600 /etc/rsync.pass
4、客户端测试同步数据
[37.7]#rsync -avz --password-file=/etc/rsync.pass /data/www rsyncuser@192.168.37.17::backup
5、客户端数据同步脚本
[37.7]#vim inotify_rsync.sh
#!/bin/bash
SRC='/data/www/'
DEST='rsyncuser@192.168.37.17::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
:wq
[37.7]#chmod +x inotify_rsync.sh
6、数据同步测试
[root@37.17]#watch -n1 ls -l /backupdir
[37.7]#. inotify_rsync.sh
[37.7/data/www]#touch rm cp ……
5、使用iptable实现: 放行telnet, ftp, web服务,放行samba服务,其他端口服务全部拒绝
允许放行的端口:
ssh :22
ftp tcp:20、21
telnet tcp:23
web tcp:80、443
samba udp:137、138
tcp:139、445
#允许本机访问ssh端口
[root@centos7 ~]# iptables -I INPUT -s 192.168.37.11 -p tcp --dport 22 -j ACCEPT
#保持后续连接
[root@centos7 ~]# iptables -I INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
#放行端口
[root@centos7 ~]# iptables -A INPUT -p tcp -m multiport --dports 20,21,23,80,443,139,445 -j ACCEPT
[root@centos7 ~]# iptables -A INPUT -p udp --dport 137:138 -j ACCEPT
#拒绝连接
[root@centos7 ~]# iptables -A INPUT -j REJECT