上午
1
、环境准备
(
1
)
yum
源
(一个云仓库
+pepl
仓库)
[root@web ~]# vim /etc/yum.repos.d/hh.repo
[a]
name=a
baseurl=file:///mnt
gpgcheck=0
[root@web ~]# vim /etc/fstab
/dev/cdrom /mnt iso9660 defaults 0 0
[root@web ~]# mount -a
[root@web ~]# yum repolist
[root@web ~]# ping www.baidu.com
[root@web ~]# yum -y install wget
[root@web ~]# vim /etc/resolv.conf
将
dns
该为
114.114.114.114 8.8.8.8
[root@web ~]# ping www.baidu.com //
可以
ping
通百度即可进行下一步
[root@web ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo
http://mirrors.cloud.tencent.com/repo/centos7_base.repo
[root@web ~]# yum clean all
[root@web ~]# yum makecache
[root@web ~]# yum -y install epel-release.noarch
[root@web ~]# yum clean all
[root@web ~]# yum makecache
(
2
)防火墙和
selinux
[root@web ~]# systemctl stop firewalld
[root@web ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@web ~]# setenforce 0
[root@web ~]# vim /etc/selinux/config
SELINUX=permissive
2
、安装配置
web
服务器
(
1
)使用自建的
yum
仓库下载
nginx
1
、先只安装不下载
nginx
[root@web ~]# yum -y install --downloadonly --downloaddir=./soft/ nginx
2
、下载
cteaterepo
,自建仓库
[root@web ~]# yum -y install createrepo
[root@web ~]# createrepo ./soft/
[root@web ~]# ls soft/
[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx
baseurl=file:///root/soft/
gpgcheck=0
enable=1
[root@web ~]# yum clean all
[root@web ~]# yum makecache
3
、使用自己建造的仓库下载
nginx
[root@web ~]# yum -y install nginx
[root@web ~]# nginx //
启动
nginx
检查服务是否启动:
[root@web ~]# netstat -lnput | grep nginx
[root@web ~]# ps -aux | grep nginx
浏览器访问测试:
(
2
)在
nginx
的首页中插入自己的图片与视频
1
、查找
nginx
的配置文件位置
[root@web html]# find / | grep nginx
/usr/share/nginx/html
[root@web html]# rpm -ql nginx
/usr/share/nginx/html/404.html
[root@web nginx]# cd /usr/share/nginx/html/
[root@web html]# ls
404.html 50x.html en-US icons img index.html nginx-logo.png
poweredby.png
2
、将图片与视频放到指定路径下
可以将其直接拖拽到
vm
中,也可以使用
scp
进行传输文件。
[root@web html]# yum -y install lrzsz
[root@web html]# rz -E //
将图片拖到
/usr/share/nginx/html
中或
者
/usr/share/nginx/html/img
中
[root@web html]# vim index.html
<img src="3.jpg" alt="
示例图片
" width="500" height="300">
[root@web html]# nginx -s reload
浏览器继续访问,查看图片是否上传成功
[root@web html]# rz -E //
将视频拖到
/usr/share/nginx/html
中
[root@web html]# vim index.html
<video width="320" height="240" controls>
<source src="1.mp4" type="video/mp4">
</video>
<img src="3.jpg" alt="
示例图片
" width="500" height="300">
[root@web html]# nginx -s reload
最终浏览器效果如下图:
3
、安装配置
nfs
服务器
NFS
(
Network File System
,网络文件系统)是一种分布式文件系统协议,允许一个系统在网络上
与他人共享目录和文件。
rpcbind
是一个用于支持
RPC
(
Remote Procedure Call
,远程过程调用)的服务。在一些操作系统
中,如
CentOS 8
,
NFS
(
Network File System
,网络文件系统)的实现依赖于
RPC
机制,而
rpcbind
充当了
NFS
服务器和客户端之间的中介,用于完成从远程到本地的映射过程。
搭建
NFS
服务器时,需要安装
rpcbind
以及
nfs-utils
软件包来提供
NFS
共享服务。
(另起一台机子)
(
1
)下载
nfs
软件与其依赖软件
[root@nfsserver ~]# yum -y install nfs-utils.x86_64 rpcbind.x86_64
(
2
)创建一个目录
/share
并创建文件
/share/passwd
[root@nfsserver ~]# mkdir /share
[root@nfsserver ~]# touch /share/passwd
(
3
)将
web
机子的图片和视频拷贝过来到
/share
目录下
[root@nfsserver ~]# scp root@10.0.0.30:/usr/share/nginx/html/3.jpg /share/
[root@nfsserver ~]# scp root@10.0.0.30:/usr/share/nginx/html/1.mp4 /share/
(
4
)编辑配置文件
/etc/exports
指定要暴露的文件(
/share
)
[root@nfsserver ~]# vim /etc/exports
/share *(rw,sync)
这段配置的含义是,将
/share
目录共享给所有的客户端(
*
代表所有),并且赋予客户端可读写
(
rw
)和数据同步(
sync
)的权限。
(
5
)测试
下午
1
、自动监控备份静态文件
(
1
)修改
hostname
[root@beifen ~]# hostnamectl set-hostname bakserver
[root@beifen ~]# reboot
(
2
)关闭防火墙与
selinux
[root@bakserver ~]# systemctl stop firewalld
[root@nfsserver ~]# systemctl disable firewalld
[root@bakserver ~]# setenforce 0
[root@nfsserver ~]# vim /etc/selinux/config
SELINUX=permissive
(
3
)安装
rsync
,
2
个主机都有安装
[root@bakserver ~]# yum -y install rsync
[root@nfsserver ~]# yum -y install rsync
(
4
)安装
inotify
监控
[root@nfsserver ~]# yum -y install inotify-tools-devel.x86_64
(
5
)同步一份文件到
bakserver
上
(
每次都需要输入密码不方便,设置免密
)
[root@nfsserver ~]# rsync -av /share/ root@10.0.0.31:/tmp/
(
6
)设置免密
[root@nfsserver ~]# ssh-keygen
[root@nfsserver ~]# ssh-copy-id root@10.0.0.31
(
将公钥字符串添加到对方家目录中的
authorized_keys
文件中
也可以使用
scp
将
id_rsa.pub
文件上传到
bakserver
主机,手工将文件中的字符串复制到
authorized_keys)
(
7
)确定全部都免密,测试实时
rsync
是否能够免密备份
[root@nfsserver ~]# rsync -av /share/ root@10.0.0.31:/tmp/
(
8
)在备份主机上创建
/bakpu
目录
[root@bakserver ~]# mkdir /bakpu //
同步的信息都存放在这里
(
9
)在
nfs
上编辑脚本,并进行监控,实现自动同步
[root@nfsserver ~]# vim rn.sh
#!/bin/bash
inotifywait -mrq -e modify,create,delete,attrib,move /share/ | while read
events
do
rsync -av /share/ root@10.0.0.31:/bakpu
echo "`date +%F\ %T`
出现时间
$events" >> /var/log/rsync.log 2>&1
done
[root@nfsserver ~]# chmod +x rn.sh
[root@nfsserver ~]# nohup ./rn.sh &
[root@nfsserver ~]# touch /share/1.txt
(
10
)查看是否同步成功
[root@bakserver ~]# ls /bakpu/
1.mp4 1.txt 3.jpg a.txt passwd
2
、
SAMBA
文件共享
Samba
是在
Linux
和
Unix
系统上实现
SMB
(
Server Message Block
)协议的一个免费软件。
SMB
协议主要用于
Windows
操作系统的文件和打印共享。通过
Samba
,
Linux
和
Unix
系统可以
与
Windows
系统进行良好的交互和资源共享。
Samba
的主要特点和优势包括:
1.
跨平台文件共享:允许
Windows
、
Linux
和
macOS
等不同操作系统之间共享文件和打印机。
2.
易于配置:可以通过简单的配置文件和命令来设置共享目录、用户权限等。
3.
支持多种身份验证方式:例如本地用户、域用户等。
在实际应用中,
Samba
常用于以下场景:
企业办公环境:让
Windows
客户端能够访问
Linux
服务器上的共享文件。
家庭网络:在不同操作系统的设备之间共享多媒体文件。
例如,在一个小型办公室中,可以使用
Samba
在一台
Linux
服务器上设置共享目录,使得办公室内
的
Windows
电脑能够方便地访问和存储文件。
配置
Samba
时,需要编辑
/etc/samba/smb.conf
文件来定义共享目录、访问权限、用户等信
息。
(
1
)下载
samba
软件
[root@bakserver ~]# yum -y install samba
[root@bakserver ~]# ls /etc/samba/
lmhosts smb.conf smb.conf.example
(
2
)修改
samba
的配置文件
[root@bakserver ~]# vim /etc/samba/smb.conf
[smb_share] //
这是共享的名称,客户端在访问时会看到这个名称。
comment = samba service //
这是对该共享的注释说明,用于提供一些描述信息,这里说明
是
“Samba
服务
”
path = /bakpu //
指定了实际共享的目录路径为
`/bakpu`
guest ok = no //
表示不允许访客访问,只有经过授权的用户才能访问
writable = yes //
表示允许对该共享目录进行写入操作
(
3
)设置用户
user01
,
samba
认证
123
密码
[root@bakserver ~]# useradd user01
[root@bakserver ~]# smbpasswd -a user01
New SMB password:
Retype new SMB password:
Added user user01.
window
要访问
bakpu
中的文件,使用
user01 123
(
4
)启动名称管理
[root@bakserver ~]# systemctl start nmb.service
[root@bakserver ~]# systemctl start smb.service
(
5
)测试
在本机(
window
)上直接搜索启用或关闭
windows
功能
找到
SMB
直接全部开启
点击映射网络驱动器
输入备份主机的地址与配置文件中【】里的信息
输入用户名与密码
即可查看到
samba
文件共享的内容
发现并无修改权限
为共享文件添加可写的权限
(完成以后在
window
中可以读写)
[root@bakserver ~]# setfacl -m u:user01:rwx /bakpu/*
3
、在
linux
系统中安装
samba
客户端
(
1
)安装客户端软件包
[root@web ~]# yum -y install samba-client
(
2
)使用
smb
客户端访问服务器
[root@web ~]# smbclient -L 10.0.0.31 -U user01 //
这条命令用于以用户
user01
的
身份列出指定
IP
地址
10.0.0.31
上
Samba
服务器所提供的共享资源列表
Enter SAMBA\user01's password:
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
smb_share Disk samba service
IPC$ IPC IPC Service (Samba 4.10.16)
user01 Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
SAMBA BAKSERVER
[root@web ~]# smbclient //10.0.0.31/smb_share -U user01 //
以用户
user01
的身
份连接到
IP
地址为
10.0.0.31
上名为
smb_share
的共享资源
Enter SAMBA\user01's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Mon Jul 22 16:32:56 2024
.. D 0 Mon Jul 22 16:20:01 2024
1.mp4 N 5882370 Mon Jul 22 13:17:38 2024
1.txt N 0 Mon Jul 22 16:27:44 2024
3.jpg N 114639 Mon Jul 22 11:44:14 2024
a.txt N 10 Mon Jul 22 16:01:59 2024
passwd N 0 Mon Jul 22 11:43:15 2024
2.txt N 0 Mon Jul 22 16:34:17 2024
18855936 blocks of size 1024. 16848524 blocks available
(
3
)挂载
smb
共享文件
cifs-utils
是一个包含一系列实用工具的软件包,主要用于在
Linux
系统上实现对
CIFS
(
Common
Internet File System
,通用
Internet
文件系统)共享的挂载和管理。
CIFS
是
SMB
(
Server Message Block
)的一种变体,它允许
Linux
系统与其他支持
CIFS/SMB
协议
的设备(如
Windows
系统)进行文件和打印机的共享访问。
通过
cifs-utils
包中的
mount.cifs
命令,可以将远程的
CIFS
共享目录挂载到
Linux
本地的某个目录
上,从而像操作本地文件一样对共享目录中的文件进行操作,例如查看、编辑、复制等。
要使用
cifs-utils
,通常需要进行以下步骤(具体命令可能因
Linux
发行版而略有不同):
1.
安装
cifs-utils
包:
在
Ubuntu
或
Debian
系统上,使用命令
sudo apt-get install cifs-utils
。
在
CentOS
或
Fedora
系统上,使用命令
sudo dnf install cifs-utils
。
2.
创建用于挂载共享目录的本地目录(例如
/mnt/share
):
sudo mkdir /mnt/share
。
3.
使用
mount.cifs
命令挂载共享目录:
sudo mount.cifs //server/share /mnt/share -o
username=user,password=pass
。其中,
//server/share
是共享目录的
UNC
路径(包括服
务器地址和共享目录名称),
/mnt/share
是本地挂载目录,
-o
后面指定了连接共享所需的用
户名和密码。
此外,还可以设置其他选项,如工作组域名等。例如,如果用户在
Windows
工作组或域中,可以这
样设置:
sudo mount.cifs -o username=<win_share_user>,domain=<win_domain>
//win_share_ip/<share_name> /mnt/share
。
为了提高安全性,也可以使用凭证文件来保存用户名、密码和域名等信息,而不是直接在命令行中
指定。首先创建凭证文件(如
/etc/win-credentials
),设置正确的权限和所有者(如
sudo chown
root: /etc/win-credentials && sudo chmod 600 /etc/win-credentials
),然后在挂载命令中
使用
Credentials=/etc/win-credentials
选项来指定凭证文件。
如果希望在系统启动时自动挂载共享目录,可以编辑
/etc/fstab
文件,在其中添加相应的挂载信
息。
需要注意的是,在使用
CIFS
共享时,要确保具有足够的权限访问远程共享目录,并且网络连接正
常。同时,出于安全考虑,在公共网络环境中使用
CIFS
共享时要谨慎。另外,
CIFS
协议已逐渐被
SMB2
和
SMB3
协议所取代,它们提供了更好的安全性和性能。但
cifs-utils
通常也支持这些较新的协议版本。
实际使用中,还需根据具体的网络环境和需求进行配置和调整。
[root@web ~]# yum -y install cifs-utils
[root@web ~]# mount.cifs -o user=user01,pass=123 //10.0.0.31/smb_share ~/aaa/
[root@web ~]# ls aaa/
1.mp4 1.txt 2.txt 3.jpg a.txt passwd
总结
1. ftp
局域⽹和外⽹都可以
2. nfs
局域⽹ 挂载⽅式访问
mount.nfs
侧重于
Linux
与
Linux
之间
3. samba
局域⽹ 直接访问
(smbclinet)
挂载的⽅式
mount.cifs
侧重于
Windows
与
Linux
之间