Linux文件服務器的搭建
Samba
vsftpd
nfs
Samba服務
作用:共享目錄(smb協議)
軟件:samba 服務端, samba-client 客戶端
配置文件:/etc/samba/smb.conf
服務:smb, nmb
端口:smb ---> 139/tcp, 445/tcp 提供文件共享功能
nmb ---> 137/udp, 138/udp 提供解析計算機名稱
配置文件:/etc/samba/smb.conf
全局配置
[global]
workgroup = MYGROUP >>>設置工作組名稱
server string = Samba Server Version %v >>>顯示samba軟件版本信息
interfaces = lo eth0 192.168.12.2/24 192.168.13.2/24 >>>samba服務監聽的IP地址
hosts allow = 127. 192.168.12. 192.168.13. >>>設置僅允許哪些主機可訪問
hosts deny = 192.168.12. 192.168.1.1/24 >>>拒絕哪些主機可訪問
security = user >>> 基於用戶認證的訪問
share >>> 匿名訪問
共享目錄配置
[共享名稱]
comment = >>> 描述信息
path = /bj >>> 指定目錄名稱
browseable = yes >>> 可下載文件
writable = yes >>> 可上傳文件
public = yes >>> 允許所有用戶訪問
write list = user1 >>> 僅允許user1可上傳文件
示例:
環境描述:
Linux 192.168.122.105 Centos 7.2 文件共享服務器
Windows/Linux 客戶端
需求:
通過samba軟件將本地的/caiwu目錄共享, 客戶端可通過martin用戶訪問,僅允許其下載文件
- 關閉SELinux, 防火牆
[root@file-server ~]# setenforce 0
[root@file-server ~]# getenforce
Permissive
[root@file-server ~]# vim /etc/sysconfig/selinux
[root@file-server ~]# systemctl stop firewalld.service
[root@file-server ~]# systemctl disable firewalld.service
- 安裝軟件
[root@file-server ~]# yum install -y samba samba-client
- 編輯配置文件,共享/caiwu目錄
[root@file-server ~]# mkdir /caiwu
[root@file-server ~]# touch /caiwu/{1..5}.mp3
[root@file-server ~]# vim /etc/samba/smb.conf
[caiwu]
comment = It is a test
path = /caiwu
browseable = yes
- 創建共享用戶
[root@file-server ~]# useradd martin
[root@file-server ~]# smbpasswd -a martin
New SMB password:
Retype new SMB password:
Added user martin.
[root@file-server ~]# pdbedit -L >>> 查看共享用戶
martin:1001:
[root@file-server ~]#
- 啟動服務
[root@file-server ~]# systemctl start smb
[root@file-server ~]# systemctl enable smb
[root@file-server ~]# ss -antp | grep smbd
LISTEN 0 50 :139 : users:(("smbd",pid=2804,fd=38))
LISTEN 0 50 :445 : users:(("smbd",pid=2804,fd=37))
LISTEN 0 50 :::139 ::: users:(("smbd",pid=2804,fd=36))
LISTEN 0 50 :::445 ::: users:(("smbd",pid=2804,fd=35))
- 測試訪問
Windows客戶端:
\\192.168.122.105
取消用戶宿主目錄的共享
[root@file-server ~]# vim /etc/samba/smb.conf
[homes]
comment = Home Directories
browseable = no
writable = yes
[root@file-server ~]# systemctl restart smb
Linux客戶端:
[root@client ~]# yum install -y samba-client
[root@client ~]# smbclient //192.168.122.105/caiwu -U martin
配置允許martin用戶可上傳文件
- 編輯配置文件
[root@file-server ~]# vim /etc/samba/smb.conf
[caiwu]
...
writable = yes
[root@file-server ~]# systemctl restart smb
- 設置目錄的本地權限
[root@file-server ~]# setfacl -m u:martin:rwx /caiwu/
示例:
通過samba軟件將本地的/shichang目錄共享,允許martin用戶下載文件,允許admin用戶上傳文件
- 創建目錄,創建共享用戶
[root@file-server ~]# mkdir /shichang
[root@file-server ~]# touch /shichang/{1..5}.jpg
[root@file-server ~]#
[root@file-server ~]# useradd admin
[root@file-server ~]# smbpasswd -a admin
New SMB password:
Retype new SMB password:
Added user admin.
[root@file-server ~]#
[root@file-server ~]# pdbedit -L
martin:1001:
admin:1002:
[root@file-server ~]#
- 編輯配置文件
[root@file-server ~]# vim /etc/samba/smb.conf
[shichang]
path = /shichang
browseable = yes
write list = admin
[root@file-server ~]# systemctl restart smb
[root@file-server ~]# chown admin /shichang/
[root@file-server ~]# ls -ldh /shichang/
drwxr-xr-x. 2 admin root 66 2月 21 12:00 /shichang/
[root@file-server ~]#
-
測試訪問
清除windows的共享緩存
net use * /del
windows設置網絡映射驅動器訪問共享
FTP ------- File Transport Protocol 文件傳輸協議
FTP協議的連接模式:
主動連接
被動連接
軟件:vsftpd
配置文件:/etc/vsftpd/vsftpd.conf
服務:vsftpd
端口:21/tcp 命令連接端口
20/tcp 數據連接端口(主動)
FTP根目錄:
用戶宿主目錄
訪問方式:
匿名用戶訪問(ftp)
用戶認證的訪問
示例:搭建匿名訪問的FTP服務器
- 安裝vsftpd軟件
[root@file-server ~]# yum install -y vsftpd
[root@file-server ~]# systemctl start vsftpd
[root@file-server ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
[root@file-server ~]# ss -antp | grep :21
LISTEN 0 32 :::21 ::😗 users:(("vsftpd",pid=5748,fd=3))
測試訪問:
Windows:
ftp://192.168.122.105
FileZilla FTP客戶端軟件
允許匿名上傳的文件
[root@file-server ~]# chmod o+w /var/ftp/pub/
anon_upload_enable=YES >>>允許上傳文件
anon_mkdir_write_enable=YES >>>允許上傳目錄
anon_other_write_enable=YES >>>允許其他的修改(刪除、重命名等)
anon_umask=022 >>>允許其他用戶能下載匿名用戶的文件
anon_root=/company >>>更改匿名用戶的FTP的根目錄
本地用戶認證的FTP服務
示例: 搭建FTP yum源提供MySQL安裝包
[root@file-server ~]# ls /rpm/mysql/
mysql-community-client-5.7.16-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-common-5.7.16-1.el7.x86_64.rpm mysql-community-server-5.7.16-1.el7.x86_64.rpm
mysql-community-libs-5.7.16-1.el7.x86_64.rpm
[root@file-server ~]# createrepo /rpm/mysql/
[root@file-server ~]# vim /etc/vsftpd/vsftpd.conf
anon_root=/rpm
[root@file-server ~]# systemctl restart vsftpd
使用ftp源:
[root@client ~]# cat /etc/yum.repos.d/centos.repo
[Centos]
name=centos7u2
baseurl=ftp://172.16.8.100/centos7u2
enabled=1
gpgcheck=0
[mysql]
name=mysql
baseurl=ftp://192.168.122.105/mysql
enabled=1
gpgcheck=0
nfs -------- Network File System 網絡文件系統
作用:在Linux服務器間實現數據共享
軟件:
nfs-utils
rpcbind
[root@file-server ~]# rpm -q rpcbind
rpcbind-0.2.0-32.el7.x86_64
[root@file-server ~]# rpm -q nfs-utils
nfs-utils-1.3.0-0.21.el7.x86_64
[root@file-server ~]#
目錄導出文件 --- /etc/exports
文件格式:
目錄名稱 客戶端地址(權限)
客戶端地址:
IP地址 192.168.1.1
網段 192.168.1.0/24
*
權限:
ro 只讀
rw 讀寫
sync 同步
async 異步
all_squash 客戶端所有用戶上傳的文件的所屬均為nfsnobody
root_squash 客戶端root用戶上傳的文件的所屬會被映射為nfsnobody
no_root_squash 客戶端root用戶上傳的文件的所屬仍為root
anonuid=<number>
anongid=<number>
示例:
通過nfs共享本地目錄/webdata, 允許192.168.122.121以只讀方式掛載
[root@file-server ~]# mkdir /webdata
[root@file-server ~]# touch /webdata/{1..10}.html
[root@file-server ~]# cat /etc/exports
/webdata 192.168.122.121(ro)
[root@file-server ~]#
[root@file-server ~]# systemctl restart rpcbind
[root@file-server ~]# systemctl restart nfs-server
[root@file-server ~]# systemctl enable nfs-server
[root@file-server ~]# showmount -e localhost
Export list for localhost:
/webdata 192.168.122.121
[root@file-server ~]#
客戶端:
[root@client ~]# mount 192.168.122.105:/webdata /www/
[root@client ~]# ls /www/
10.html 1.html 2.html 3.html 4.html 5.html 6.html 7.html 8.html 9.html
自動掛載:
vim /etc/fstab
192.168.122.105:/webdata /www nfs defaults 0 0
示例2:
通過nfs共享本地目錄/mysqldata,允許192.168.122.121以讀寫的方式掛載
[root@file-server ~]# mkdir /mysqldata
[root@file-server ~]# touch /mysqldata/{1..10}.sql
[root@file-server ~]# chmod o+w /mysqldata/
[root@file-server ~]# vim /etc/exports
...
/mysqldata 192.168.122.121(rw)
[root@file-server ~]# exportfs -rav
exporting 192.168.122.121:/mysqldata
exporting 192.168.122.121:/webdata
[root@file-server ~]#
客戶端:
[root@client ~]# vim /etc/fstab
192.168.122.105:/mysqldata /database nfs defaults 0 0
[root@client ~]# mount -a
[root@client ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/centos-root 7.3G 4.4G 3.0G 60% /
devtmpfs 230M 0 230M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 4.7M 240M 2% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
/dev/mapper/centos-home 2.0G 33M 2.0G 2% /home
/dev/vda1 512M 141M 372M 28% /boot
tmpfs 49M 0 49M 0% /run/user/0
192.168.122.105:/webdata 7.3G 3.6G 3.8G 49% /www
192.168.122.105:/mysqldata 7.3G 3.6G 3.8G 49% /database