Linux操作系统应用
- 一、用户与用户组管理
- 1.1 管理用户
- 1.1.1 与用户账号相关的文件
- 1.1.2 用户管理命令
- 1.1.3 任务实施
- 1.2 管理用户组
- 1.2.1 用户组
- 1.2.2 用户组管理命令
- 1.3 查看用户和用户组状态
- 1.3.1 用户查看命令 id、who、whoami
- 1.3.2 用户身份切换命令 su、sudo
- 二、文件管理
- 2.1 管理文件
- 2.1.1 目录操作命令
- mkdir(-p:多级目录)
- rmdir(-p:递归删除空目录)
- cd、pwd、ls
- 2.1.2 文件操作命令
- touch
- fiel:查看文件类型
- cp(-r:递归复制、-f:强制覆盖)、mv(移动)、rm
- find、which:查找命令的绝对路径
- ln:为文件创建链接(默认软链接-s:硬链接)不能对目录创建
- 2.1.3 文件内容操作命令
- cat
- more/less
- head/tail
- wc
- grep
- 2.2 压缩文件
- gzip(-d:解压缩,-r:递归,-v:显示过程)
- tar(-c:创.tar,-x:解.tar,-f:目标文件名,-v:显示过程,-z:gzip,-j:bzip2)
- 2.3 设置文件权限属性
- chmod 更改文件访问权限(u、g、o — r、w、x)
- chown 更改文件所属者和所属组
- chgrp 更改文件所属组
- 三、磁盘与文件系统管理
- 3.1 创建磁盘分区
- 1. 分区管理(fdisk)
- 2. 格式化(mkfs)
- 3. 挂载(mount)
- df:查看文件系统挂载状态和磁盘使用情况(-h:易读方式查看)
- 3.2 设置磁盘配额 quota
- 3.3 管理逻辑卷
- 四、软件包与进程管理
- 4.1 管理软件包
- rpm 安装
- yum 安装(先搭建仓库)
- 源码包安装
- 4.2 管理进程(进程号:PID)
- 五、服务管理与任务制定
- 5.1 管理 Linux 服务
- 5.2 管理计划任务
- 一次性计划任务 at
- 周期性计划任务 crontab
- 六、shell简易编程
- 6.1 重定向
- 6.2 shell 命令与 vim 编辑器
- 七、网络基本配置
- 7.1 网络配置
- 显示网卡的配置信息 ifconfig
- 配置网卡 nmcli
- 修改 IP nmcli connection modify 网卡(method、IP、gateway、dns、connection.autoconnect)
- 修改网卡
- 7.2 网络调试
- 测试网络连通性 ping
- 显示网络相关信息 netstat
- 显示数据包到达主机所经过的路由信息 tracepath
- 显示域名和 IP 地址的对应关系 nslookup
- 八、远程连接 OenpSSH
- 8.1 配置与启动OpenSSH
- 8.2 远程控制服务
- 远程连接主机 ssh
- 远程传输命令 scp
- 九、网络服务器
- 9.1 web服务器
- 9.2 Apache
- 9.3 Nginx
- 十、
- 十一、NFS
一、用户与用户组管理
1.1 管理用户
用户类型 | UID | 描述 |
---|---|---|
超级用户 | 0 | root用户、系统管理员或根用户 |
系统用户 | 1~999 | 方便系统管理 |
普通用户 | 1000~60 000 | 又超级用户创建,用于用户的日常工作 |
1.1.1 与用户账号相关的文件
/etc/passwd:系统识别用户的文件,保存用户账号的基本信息
username:password:UID:GID:comment:home directory:shell
字段 | 含义 |
---|---|
username | 用户名,必须唯一 |
password | 用户密码,x填充,真正密码以MD5加密方式保存在shadow文件中 |
UID | 用户ID,如果多个用户名对应一个UID,系统内部回将它们视为同一个用户 |
GID | 组ID,区分不同组 |
comment | 备注信息 |
home directory | 用户主目录 |
shell | 命令解释器,默认bash |
/etc/shadow
1.1.2 用户管理命令
-
添加用户账号 useradd
- -d:指定用户主目录,默认为 /home/用户名
- -g:指定用户所属的基本组或 GID,-G:指定用户所属的附加组,各组之间逗号相隔,-u:指定用户的 UID,且必须唯一
- -e:指定用户账号的失效时间
- -s:指定用户的登录 shell,默认为 /bin/bash
-
管理用户密码 passwd
- -d:删除用户密码,将其设置为空,使之无需密码即可登录
- -l:锁定用户账号,可暂时禁止该用户登录系统,-u:解锁用户账号
- -S:查询用户账号的状态“(是否锁定)
-
修改用户账号 usermod
- -l:更改用户名,该账号的主目录、UID 及 GID 保持不变,
- -L:锁定用户账号,-U:解锁
-
删除用户账号 userdel
- -userdel -r 用户名
UUID:
-
创建一个新的 UUID 值 uuidgen
-
识别磁盘设备和其他硬件设备,并显示它们的 UUID 和元数据 blkid
- 修改成新的:
- blkid /dev/sda -o set_uuid=”8CCB6C99-6C68-475C-9056-77460848426F”
- tune2fs /dev/sda -U"8CCB6C99-6C68-475C-9056-77460848426F"
- 修改成新的:
1.1.3 任务实施
任务说明:
小李为研发部门和运维部门的4位成员分别添加个人账号,并设置初始密码均为kd*123
用户名:Lina,UID:1005
用户名:xuxi,不允许登录
用户名:suxi
用户名:junji
[root@host ~]# useradd -u 1005 Lina #-u:指定用户的UID,且必须唯一
[root@host ~]# passwd Lina #设置初始密码
Changing password for user Lina.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.[root@host ~]# useradd -s /sbin/nologin xuxi #不允许用户登录[root@host ~]# useradd suxi
[root@host ~]# passwd suxi
Changing password for user suxi.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.[root@host ~]# useradd junji
[root@host ~]# passwd junji
Changing password for user junji.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.[root@host ~]# cat /etc/passwd
Lina:x:1005:1005::/home/Lina:/bin/bash
xuxi:x:1006:1006::/home/xuxi:/sbin/nologin
suxi:x:1007:1007::/home/suxi:/bin/bash
junji:x:1008:1008::/home/junji:/bin/bash[root@host ~]# cat /etc/shadow
Lina:$6$mknKGRqdHSh.DGdR$svPbl0F0ToHsIu3z/uhAay7CQ1xSazSgWOFYqkXGmDGbpK9jknIavkh0qI0hidcsAk8QGdXq7gA6585zZA9RL.:20146:0:99999:7:::
xuxi:!!:20146:0:99999:7:::
suxi:$6$kyHAU/4Cj70JnMVA$ShPGjE7OZYNUa.jDzwjPvQ/M1TC8AlGLXoFGkySqk.QTLu/iPh1Ewd1eclFXHsqCxwPnxjVZ666ozqGVCwF8O0:20146:0:99999:7:::
junji:$6$7TYi0jPnE5queNh/$UvfTDQtH28ljuOf5xyXgkK55oJlv/688zFECmGUirdosGtkJ8zNASEKGu3mKw0otw/LO3f/PVuLy7QU1utXeV1:20146:0:99999:7:::
1.2 管理用户组
1.2.1 用户组
/etc/group:保存着用户名、用户组名等基本信息
groupname:password:GID:user_list
字段 | 含义 |
---|---|
groupname | 用户组名 |
password | 用户组密码,x填充,真正密码以MD5加密方式保存在gshadow文件中 |
GID | 组 ID |
user_list | 用户组成员列表 |
/etc/gshadow:
groupname:encypted password:admin:user_list
字段 | 含义 |
---|---|
groupname | 用户组名 |
encypted password | 加密后的用户组密码 |
admin | 用户组管理员 |
user_list | 用户组成员列表 |
1.2.2 用户组管理命令
- 添加用户组账号 groupadd
- groupadd -g GID 用户组名,如果不指定 GID ,则由系统自动分配
- 管理用户组密码 gpasswd [选项] [用户名] 用户组名
- -a:为用户组添加组成员
- -d:从用户组移除组成员
- 修改用户组属性 groupmod
- -n:更改用户组名,GID 保持不变,
- -g 新 GID:修改 GID
- 删除用户组账号 groupdel
- groupdel 用户组名
1.3 查看用户和用户组状态
1.3.1 用户查看命令 id、who、whoami
UUID:
-
创建一个新的 UUID 值 uuidgen
-
识别磁盘设备和其他硬件设备,并显示它们的 UUID 和元数据 blkid
- 修改成新的:
- blkid /dev/sda -o set_uuid=”8CCB6C99-6C68-475C-9056-77460848426F”
- tune2fs /dev/sda -U"8CCB6C99-6C68-475C-9056-77460848426F"
- 修改成新的:
1.3.2 用户身份切换命令 su、sudo
su 命令格式中,”-“表示完全切换到新的用户,即环境变量信息也变为新用户的相关信息
普通用户可使用 sudo 命令临时或的root用户的权限执行操作(前提:root 用户必须使用 visudo 命令打开并编辑 sudoers 配置文件/etc/sudoers)
- sudo常用选项:
- -b:在后台执行指定命令
- -u 用户名/UID:以指定的用户身份执行命令
- -l:显示出执行 sudo 命令的使用者的权限
二、文件管理
2.1 管理文件
2.1.1 目录操作命令
mkdir(-p:多级目录)
多个同级目录:
mkidr {a,b,c} 或 mkdir a b c
rmdir(-p:递归删除空目录)
执行删除命令时,需要对父目录有写权限
cd、pwd、ls
2.1.2 文件操作命令
- -:普通文件
- d:目录文件
- l:链接文件
- c、b:设备文件
- p:管道文件
touch
fiel:查看文件类型
cp(-r:递归复制、-f:强制覆盖)、mv(移动)、rm
# 把本主机的/etc/yum.repos.d/dnf.repo,复制到主机192.168.72.136中的/etc/yum.repos.d/下
scp /etc/yum.repos.d/dnf.repo root@192.168.72.136:/etc/yum.repos.d/
find、which:查找命令的绝对路径
ln:为文件创建链接(默认软链接-s:硬链接)不能对目录创建
默认软链接-符号链接->快捷方式,-s:硬链接->与原始文件互相影响,不能对目录创建
2.1.3 文件内容操作命令
cat
more/less
head/tail
wc
grep
2.2 压缩文件
gzip(-d:解压缩,-r:递归,-v:显示过程)
tar(-c:创.tar,-x:解.tar,-f:目标文件名,-v:显示过程,-z:gzip,-j:bzip2)
2.3 设置文件权限属性
chmod 更改文件访问权限(u、g、o — r、w、x)
r | w | x |
---|---|---|
4 | 2 | 0 |
chown 更改文件所属者和所属组
chgrp 更改文件所属组
三、磁盘与文件系统管理
3.1 创建磁盘分区
1. 分区管理(fdisk)
2. 格式化(mkfs)
磁盘分区后不能直接使用,须创建分区的文件系统(格式化 mkfs)才能使用
mkfs | mkfs.xfs | mkfs.ext4 [选项] 分区的设备名
eg.
mkfs.ext4 /dev/sda3 /mkfs -t ext4 /dev/sda3
[root@bogon ~]# mkfs -t xfs /dev/sda2
...
3. 挂载(mount)
磁盘创建文件系统后,须将该文件系统挂载(mount)到某个目录(挂载点,必须是空目录)才能使用
命令行模式:手动挂载
系统启动时自动挂载(/etc/fstab) 设备文件 挂载点 文件系统类型 权限选项 是否备份 是否自检
#自动挂载配置:设备文件 挂载点 文件系统类型 权限选项 是否备份 是否自检
df:查看文件系统挂载状态和磁盘使用情况(-h:易读方式查看)
3.2 设置磁盘配额 quota
磁盘配额信息:wfs_quota (-x、-c)
指定用户或用户组磁盘配额信息:edquota
开启或关闭:quotaon、quotaoff
查看指定用户或用户组的磁盘配额信:quota
查看文件系统的磁盘配额信:requota
3.3 管理逻辑卷
创建逻辑卷流程:
- 创建物理卷
- 创建卷组
- 创建逻辑卷
- 创建文件系统
- 挂载文件系统
扩展逻辑卷流程:
- 卸载文件系统
- 扩展逻辑卷容量
- 扩展文件系统
缩小逻辑卷流程:
- 卸载逻辑卷
- 检查文件系统的完整性
- 缩小文件系统
- 缩小逻辑卷
- 重新挂载文件系统
四、软件包与进程管理
4.1 管理软件包
rpm 安装
- 安装 rpm [选项] 软件名
- -U:升级(若没有该安装包则安装)
- -F:升级(若没有该安装包则取消安装)
- 卸载 rpm -e 软件名(-evh:显示卸载过程))
- 查询 rpm [选项] 软件名
- -qa:查询已安装
- -qf:查询文件或目录属于哪个RPM软件包的格式
- -qp:查询未安装的软件包
- -ql:查询文件安装路径
yum 安装(先搭建仓库)
[BaseOS] --- YUM软件仓库名称,具有唯一性
name= YUM软件仓库的描述,用于说明软件仓库的用处
baseurl= YUM软件仓库的位址 FTP/HTTP地址/本地地址
enabled= 是否启动该YUM软件仓库,1:启动,0:禁用
gpgcheck= 是否校验RPM文件内的数字签名
[AppStream]
name= YUM软件仓库的描述,用于说明软件仓库的用处
baseurl= YUM软件仓库的位址 FTP/HTTP地址/本地地址
enabled= 是否启动该YUM软件仓库,1:启动,0:禁用
gpgcheck= 是否校验RPM文件内的数字签名
源码包安装
- 下载及解压缩源码包文件
- 配置
- 编译
- 安装
4.2 管理进程(进程号:PID)
优先级:-20~19(取值越小,优先级越高)
五、服务管理与任务制定
5.1 管理 Linux 服务
systemctl is-enable #查看服务是否为开机自启动
5.2 管理计划任务
一次性计划任务 at
at -l = atq # 显示待执行的任务
at -d = atrm # 删除任务
ctrl + d # 提交任务
周期性计划任务 crontab
在配置时,所有命令使用 绝对路径
- -e:创建并编辑
- -l:查看当前任务
- -r:删除某条任务
- -u:指定用户名称(root用户下)
配置格式:
#分钟字段必须有数值,日期与星期不可同时使用
分钟 小时 日期 月份 星期 命令
0~59 0~23 1~31 1~12 0~7(0、7都为周天)0 17 * * 1-5 #周一到周五每天17:00
30 8 * * 1,3,5 #每周一、三、五的8:30
0 8-18/2 * * * #8:00到18:00之间每隔2小时
0 * */3 * * #每隔3天
- 周期性计划任务配置中的符号:
- *:该范围的任意时间
- ,:间隔的多个不连续时间点
- -:一段连续的时间范围
- /:执行任务的间隔时间
六、shell简易编程
6.1 重定向
符号 | 命令格式 | 作用 |
---|---|---|
< | 命令 < 文件 | 将文件作为命令标准输入 |
< < | 命令 < < 分界符 | 从标准输入中读入,知道遇到分界符才停止 |
> | 命令 > 文件 | 将标准输出重定向到一个文件中(清空原有文件的数据) |
>> | 命令 >> 文件 | 将标准输出重定向到一个文件中(追加到原有内容的后面) |
:2> | 命令 2> 文件 | 将错误输出重定向到一个文件中(清空原有文件的数据) |
2>> | 命令 2>>文件 | 将错误输出重定向到一个文件中(追加到原有内容的后面) |
需求:在 /etc/yum.repos.d/ 中通过重定向配置一个本地仓库,仓库名为 ymrepo.repo,然后将仓库中的内容反序重定向到 /root/myfile 文件中。
# 利用重定向将内容写到文件中
[root@host ~]# cat > /etc/yum.repos.d/ymrepo.repo << EFO
> [OS]
> name=BaseOS
> baseurl=/mnt/BaseOS
> gpgcheck=0
> enable=1
> [AppStream]
> name=AppStream
> baseurl=/mnt/AppStream
> gpgcheck=0
> enable=1
> EFO
[root@host ~]# cat /etc/yum.repos.d/ymrepo.repo
[OS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
enable=1
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0
enable=1
[root@host ~]# tac /etc/yum.repos.d/ymrepo.repo >> /root/myfile
[root@host ~]# cat /root/myfile
enable=1
gpgcheck=0
baseurl=/mnt/AppStream
name=AppStream
[AppStream]
enable=1
gpgcheck=0
baseurl=/mnt/BaseOS
name=BaseOS
[OS]
[root@server ~]# cat /etc/yum.repos.d/dnf.repo | sort -r
name=BaseOS
name=AppStream
gpgcheck=0
gpgcheck=0
enabled=1
baseurl=/mnt/BaseOS
baseurl=/mnt/AppStream
[BaseOS]
[AppStream]
[root@server ~]# cat /etc/yum.repos.d/dnf.repo -n | sort -r9 gpgcheck=08 baseurl=/mnt/AppStream7 name=AppStream6 [AppStream]5 enabled=14 gpgcheck=03 baseurl=/mnt/BaseOS2 name=BaseOS1 [BaseOS]
[root@server ~]# cat /etc/yum.repos.d/dnf.repo -n | sort -rn9 gpgcheck=08 baseurl=/mnt/AppStream7 name=AppStream6 [AppStream]5 enabled=14 gpgcheck=03 baseurl=/mnt/BaseOS2 name=BaseOS1 [BaseOS]
管道 pip “ | ” :连接多个命令
echo:在终端输出 字符串/变量 提取后的值
6.2 shell 命令与 vim 编辑器
vim编辑:
七、网络基本配置
7.1 网络配置
socket相关知识: 什么是Socket?
显示网卡的配置信息 ifconfig
配置网卡 nmcli
修改 IP nmcli connection modify 网卡(method、IP、gateway、dns、connection.autoconnect)
修改 IP(ipv4.addresses ip/掩码,ipv4.dns,ipv4.gateway,authentic )
TCP/UDP:四层协议(传输层),都在 53 端口
TCP协议 提供可靠的连接服务,连接是通过 三次握手 进行初始化的。TCP是全双工模式,所以需要 四次挥手 关闭连接. TCP使用三次握手和四次挥手来建立和终止连接
(强连接-安全),以确保可靠的连接建立和终止,在网络通信中保护数据的完整性和可靠性.
UDP协议 仅保证发送信息,不保证接受(优点:快,缺点:容易丢失数据,安全系数不高)
nmcli connection modify 网卡(ensxxx) ipv4.method manual ipv4.addresses ip/掩码 ipv4.dns,ipv4.gateway,connection.autoconnect yes
[root@client ~]# nmcli connection modify ens33 ipv4.addresses 192.168.88.7/24 ipv4.dns 192.168.88.2 ipv4.gateway 192.168.88.2 ipv4.method manual connection.autoconnect yes
修改网卡
nmcli c modify ens160 ipv4.dns 192.168.88.136
或者
vi /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.88.144
- 查看 dns
nmcli d show | grep DNS
7.2 网络调试
测试网络连通性 ping
执行 ping 命令时,系统会使用 ICMP(Internet控制报文协议)向远程主机发出要求回应的信息
显示网络相关信息 netstat
显示数据包到达主机所经过的路由信息 tracepath
显示域名和 IP 地址的对应关系 nslookup
八、远程连接 OenpSSH
8.1 配置与启动OpenSSH
OpenSSH 基于 SSH(secure shell)协议开发的免费开源软件,可在网络上由一台计算机远程连接另外一台计算机
SSH 是建立在 应用层 基础上的安全协议,sshd 是基于 SSH 协议 开发的 远程管理服务,提供两种安全验证方法:
- 基于密码的验证
- 基于密钥的验证
配置文件:/etc/ssh/sshd_config
[root@host ~]# cat /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.104 2021/07/02 05:11:21 dtucker Exp $# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.# This sshd was compiled with PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.# To modify the system-wide sshd configuration, create a *.conf file under
# /etc/ssh/sshd_config.d/ which will be automatically included below
Include /etc/ssh/sshd_config.d/*.conf# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22 sshd服务端口,默认为22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
....
8.2 远程控制服务
远程连接主机 ssh
首次连接主机时,需要输入yes(Are you sure you want to continue connecting)
ssh [选项] [远程用户名@]远程IP地址 或 域名
- -l:指定远程登录用户名
- -v:追踪调试建立的 SSH 连接情况
- -p:指定远程主机的 sshd 端口
- -b:指定源 IP 地址
- -C:所有通过 SSH 发送或接收的数据将会被压缩,并且都是加密的
- -F:指定配置文件
远程传输命令 scp
使用 scp 命令将文件从本地复制到远程主机
:
scp [选项] 本地文件 [远程主机名@]远程IP地址 或 域名:远程目录
使用 scp 命令将远程主机的文件下载到本地
:
scp [选项] [远程主机名@]远程IP地址 或 域名:远程文件 本地文件
- 选项:
- -r:递归复制整个目录
- -v:显示详细的连接进度
- -p:指定远程主机的 sshd 端口
# 把本主机的/etc/yum.repos.d/dnf.repo,复制到主机192.168.72.136中的/etc/yum.repos.d/下
scp /etc/yum.repos.d/dnf.repo root@192.168.72.136:/etc/yum.repos.d/
[root@server ~]# scp /etc/yum.repos.d/dnf.repo root@192.168.88.142:/etc/yum.repos.d/
The authenticity of host '192.168.88.142 (192.168.88.142)' can't be established.
ED25519 key fingerprint is SHA256:I3/lsrnTEnXOE3LFvTLRUXAJ+AhSVrIEWtqTnleRz9w.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '192.168.88.142' (ED25519) to the list of known hosts.
root@192.168.88.142's password:
dnf.repo
[root@client ~]# ll /etc/yum.repos.d/
total 4
-rw-r--r--. 1 root root 358 Mar 14 13:32 redhat.repo
[root@client ~]# ll /etc/yum.repos.d/
total 8
-rw-r--r--. 1 root root 113 Mar 14 14:10 dnf.repo
-rw-r--r--. 1 root root 358 Mar 14 13:32 redhat.repo
九、网络服务器
9.1 web服务器
9.2 Apache
9.3 Nginx
Nginx 是高性能的 Web 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
在高连接并发的情况下,Nginx是Apache服务器不错的替代品
十、
十一、NFS
在Linux系统中,有一些文件共享协议用于跨网络共享存储和文件,NFS(Network File System,网络文件系统),它允许网络中的计算机(不同的计算机、不同的操作系统)之间通过 TCP/IP 网络共享资源
在 NFS 的应用中,本地 NFS 的客户端应用 可以 透明地读写 位于 远端 NFS 服务器上的文件 ,就像访问本地文件一样
- 创建两台主机,配置仓库,安装基本软件,安装 NFS 软件
- 启动服务(查看服务状态),放行防火墙,关闭SELinux,设置开机自启动
- 配置 NFS ,服务端创建一个需要共享的文件,这个文件的内容会被共享到目的客户端
- 服务端:编辑/etc/exports文件,配置文件和目的网段(IP、主机),设置权限
- 重启服务
- 然后暴露共享服务,服务器查看nfs共享状态
- 客户端创建挂载目录,将创建的目录挂载到服务端的共享目录
练习1
要设置NFS共享,需要至少两台 Linux/Unix 机器。下面使用两台服务器来演示 NFS 安装、配置及使用。
角色 | IP | 主机名 | 系统 |
---|---|---|---|
服务器 | 192.168.88.136 | nfs-server | RHEL 9 |
客户端 | 192.168.88.142 | nfs-client | RHEL 9 |
1.创建两台主机,配置仓库,安装基本软件,安装 NFS 软件
- client:
#配置仓库
[root@client yum.repos.d]# vi dnf.repo
[root@client yum.repos.d]# cat dnf.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0
[root@client yum.repos.d]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@client yum.repos.d]# ls /mnt
AppStream EULA images RPM-GPG-KEY-redhat-beta
BaseOS extra_files.json isolinux RPM-GPG-KEY-redhat-release
EFI GPL media.repo# 把本主机(客户端)的/etc/yum.repos.d/dnf.repo,复制到主机(服务端)192.168.72.136中的/etc/yum.repos.d/下
[root@client yum.repos.d]# scp /etc/yum.repos.d/dnf.repo root@192.168.88.136:/etc/yum.repos.d/
The authenticity of host '192.168.88.136 (192.168.88.136)' can't be established.
ED25519 key fingerprint is SHA256:I3/lsrnTEnXOE3LFvTLRUXAJ+AhSVrIEWtqTnleRz9w.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '192.168.88.136' (ED25519) to the list of known hosts.
root@192.168.88.136's password:
dnf.repo 100% 114 215.5KB/s 00:00
- server:
[root@server yum.repos.d]# ls
dnf.repo EFo EFO EFP redhat.repo
[root@server yum.repos.d]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
- 安装基本软件(需要先配置仓库和挂载后才能成功执行此命令):
# 安装基本软件,bash-completion 用于tab补全
[root@client yum.repos.d]# dnf install net-tools wget curl bash-completion vim -y
[root@client yum.repos.d]# dnf install nfs-utils -y
#设置主机名命令:
hostnamectl hostname nfs-server#设置IP命令:
nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.88.136/24 ipv4.dns 223.5.5.5 ipv4.gateway 192.168.88.2 connection.autoconnect yes
# IP:固定前缀nmcli connection modify 网卡
1.ipv4.method:manual
2.ipv4.addresses(注意写子网掩码)
3.ipv4.dns
4.ipv4.gateway
5.connection.autoconnect:yes
nmcli c up ens160 #启动
需要重新连接才能改变IP#开机自启动
2. 放行防火墙,关闭SELinux,启动服务(查看服务状态)
- 同时执行两个会话的命令
# 启动服务
systemctl enable --now nfs-server
或者
systemctl start nfs-server# 放行防火墙
firewall-cmd --permanent --add-service=nfs
firewall-cmd --reload
firewall-cmd --list-all(验证是否放行成功)
# 永久关闭
[root@server ~]# sed -i "s/^SELINUX=enforcing$/SELINUX=permissive/g" /etc/selinux/config# 关闭SELinux
setenforce 0
- 查看网络相关命令:
[root@server ~]# netstat -lntup | grep 2049tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp6 0 0 :::2049 :::* LISTEN -
[root@server ~]# netstat -lntup | grep 111
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp6 0 0 :::111 :::* LISTEN 1/systemd
udp 0 0 0.0.0.0:111 0.0.0.0:* 1/systemd
udp6 0 0 :::111 :::* 1/systemd
[root@server ~]# ss -lntup | grep 111 #(ss=netstat)
udp UNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=13602,fd=5),("systemd",pid=1,fd=88))
udp UNCONN 0 0 [::]:111 [::]:* users:(("rpcbind",pid=13602,fd=7),("systemd",pid=1,fd=90))
tcp LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=13602,fd=4),("systemd",pid=1,fd=87))
tcp LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=13602,fd=6),("systemd",pid=1,fd=89))
[root@server ~]# ps -ef | grep nfs
root 13607 1 0 13:01 ? 00:00:00 /usr/sbin/nfsdcld
root 13621 2 0 13:01 ? 00:00:00 [nfsd]
root 13622 2 0 13:01 ? 00:00:00 [nfsd]
root 13623 2 0 13:01 ? 00:00:00 [nfsd]
root 13624 2 0 13:01 ? 00:00:00 [nfsd]
root 13625 2 0 13:01 ? 00:00:00 [nfsd]
root 13626 2 0 13:01 ? 00:00:00 [nfsd]
root 13627 2 0 13:01 ? 00:00:00 [nfsd]
root 13628 2 0 13:01 ? 00:00:00 [nfsd]
- 查看服务状态:
[root@client ~]# systemctl status nfs-server
● nfs-server.service - NFS server and >Loaded: loaded (/usr/lib/systemd/>Active: active (exited) since Fri>Docs: man:rpc.nfsd(8)man:exportfs(8)
3. 配置 NFS ,服务端创建一个需要共享的文件,这个文件的内容会被共享到目的客户端
服务端:创建一个用于存储文件目录,编辑/etc/exports文件,配置文件和目的网段(IP、主机),设置权限
# 创建一个用于存储文件目录
[root@server ~]# mkdir /nfs/server -p# 配置文件和目的网段(IP、主机),设置权限,将此目录共享给客户端
[root@server ~]# vim /etc/exports
[root@server ~]# cat /etc/exports
# we will share file | other PC's ip(notice limits of authority:read and write)
/nfs/server 192.168.88.142(rw) (客户端IP)
服务端 server IP:192.168.88.136
客户端 client IP:192.168.88.142
4. 重启服务
[root@server ~]# systemctl restart nfs-server
5. 然后暴露共享服务,服务器查看nfs共享状态
[root@server ~]# showmount -e 192.168.88.136(本机,服务端IP)
Export list for 192.168.88.136:
/nfs/server 192.168.88.142 //此时服务端创建文件
[root@server ~]# touch /nfs/file/one.txt
touch: cannot touch '/nfs/file/one.txt': No such file or directory
[root@server ~]# touch /nfs/server/one.txt
[root@server ~]# vim /nfs/server/one.txt
[root@server ~]# cat /nfs/server/one.txt
a file of share from server to client:
content:this is nfs service//客户端还没能同步到文件,需要在客户端创建目录,再挂载到服务端的共享目录
[root@client ~]# ll /nfs
ls: cannot access '/nfs': No such file or directory
服务端 server IP:192.168.88.136
客户端 client IP:192.168.88.142
6. 客户端创建挂载目录,将创建的目录挂载到服务端的共享目录
# 1. 创建挂载目录
[root@client ~]# mkdir -p /nfs/client# 2. 将创建的目录挂载到服务端的共享目录
[root@client ~]# mount -t nfs 192.168.88.136:/nfs/server /nfs/client# 3. 查看是否挂载成功
[root@client ~]# df /nfs/client
Filesystem 1K-blocks Used Available Use% Mounted on
192.168.88.136:/nfs/server 46587904 1756160 44831744 4% /nfs/client# 挂载成功后即可查看到 服务端想要共享的文件
[root@client nfs]# cd /nfs/client/
[root@client client]# ll
total 4
-rw-r--r--. 1 root root 69 Mar 14 14:34 one.txt
# 有读的权限
[root@client client]# cat one.txt
a file of share from server to client:
content:this is nfs service
服务端 server IP:192.168.88.136
客户端 client IP:192.168.88.142
7. 功能测试
测试服务端读写
- 服务端已创建 one.txt 文件
[root@server ~]# cat /nfs/server/one.txt
a file of share from server to client:
content:this is nfs service
[root@server ~]# echo add second text >> /nfs/server/one.txt
[root@server ~]# cat /nfs/server/one.txt
a file of share from server to client:
content:this is nfs service
add second text
服务端对于要共享的文件,可读可写
测试客户端读写
- 客户端查看此文件
[root@client ~]# ls /nfs/client/
one.txt
[root@client nfs]# cd /nfs/client
[root@client client]# cat one.txt
a file of share from server to client:
content:this is nfs service
add second text#没有写的权限
[root@client client]# vim one.txt
[root@client client]# echo add third text > one.txt
-bash: one.txt: Permission denied
读文件能执行成功,不能写
- 服务端查看权限
#服务端查看权限
[root@server ~]# ll -d /nfs
drwxr-xr-x. 3 root root 20 Mar 14 14:27 /nfs
root(rwx) root的主(r-x) 其他用户(r-x)
u g o
[root@server server]# chmod o+rw /nfs
[root@server server]# ll -d /nfs/
drwxr-xrwx. 3 root root 20 Mar 14 14:27 /nfs/[root@server server]# vim /etc/exports
[root@server server]# cat /etc/exports
# we will share file | other PC's ip(notice limits of authority:read and write)
/nfs/server 192.168.88.142(rw,no_root_squash)#客户端可读写
[root@client ~]# ll -d /nfs/
drwxr-x--x. 3 root root 20 Mar 14 14:35 /nfs/
[root@client client]# systemctl restart nfs-server
[root@client client]# echo a new file > two.txt
[root@client client]# ls
one.txt two.txt# 服务端已同步客户端的读写操作
[root@server server]# ls
one.txt two.txt
-
用户对目录的权限受两方面约束:NFS认证权限、Posix权限;
-
NFS权限:
- NFS服务器器中exports中配置额读写、只读权限
-
Posix权限:
- 发现exports目录权限中,参数 no_root_squash 的其作用是:NFS客户端使用共享目录的用户,如果是root 的话,所有的操作均在服务器端映射为root用户,拥有共享目录的root权限!
-
默认情况使用的是相反参数root_squash:在登入 NFS 主机 export 目录的使用者如果当root时,那么这个使用者的权限将被压缩成为匿名使用者,通常他的 UID 与 GID 都会变成 nobody 那个身份。
因为客户端是使用root登录的,自然权限被压缩为nobody了,难怪无法写入。
将配置信息改为:
/nfs/server 192.168.88.142(rw,no_root_squash)
据说有点不安全,但问题是解决了。
练习2
配置 NFS服务器 作为 ngxin 服务的存储目录,并在目录中创建 index.html 文件,当访问 http://你的IP地址时,可以成功显示 index.html 文件的内容。
角色 | 软件 | IP | 主机名 | 系统 |
---|---|---|---|---|
NFS服务器 | nfs-utils | 192.168.88.7 | server | RHEL 9 |
WEB服务 | nfs-utils, nginx | 192.168.88.8 | web | openEuler |
1. 环境初始化
配置仓库,挂载,对应主机安装基本软件,安装 NFS 软件及nginx软件等
配置仓库、挂载
基本软件安装
bash-completion 用于tab补全
net-tools wget curl bash-completion vim nfs-utils
nginx 安装
- 安装 nginx( ngin官方仓库安装 )
# 配置nginx官方仓库
[root@web ~]# vim /etc/yum.repos.d/nginx.repo
[root@web ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web ~]# dnf install nginx -y
- 查找 nginx 的启动服务文件:
[root@web ~]# find / -name nginx.service
/usr/lib/systemd/system/nginx.service# 查看这个文件的内容:
[root@web ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
#ExecStop=/usr/sbin/nginx -s stop | quit
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true[Install]
WantedBy=multi-user.target
- 查看nginx.conf配置文件
[root@web ~]# vim /etc/nginx/nginx.conf
2. 放行防火墙,关闭SELinux,启动服务
# 启动 nfs 服务
[root@server ~]# systemctl enable --now nfs-server
或者
systemctl start nfs-server
# 启动 nginx
[root@web ~]# systemctl start nginx
[root@web ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled)Active: active (running) since Sun 2025-03-09 23:58:02 CST; 3s agoProcess: 14937 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)Process: 14940 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)Process: 14944 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)Main PID: 14945 (nginx)Tasks: 5 (limit: 21357)Memory: 11.1M ()CGroup: /system.slice/nginx.service├─14945 "nginx: master process /usr/sbin/nginx"├─14946 "nginx: worker process"├─14947 "nginx: worker process"├─14948 "nginx: worker process"└─14949 "nginx: worker process"Mar 09 23:58:02 web systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 09 23:58:02 web nginx[14940]: nginx: the configuration file /etc/nginx/nginx.conf syntax >
Mar 09 23:58:02 web nginx[14940]: nginx: configuration file /etc/nginx/nginx.conf test is suc>
Mar 09 23:58:02 web systemd[1]: Started The nginx HTTP and reverse proxy server.
- 放行防火墙,关闭SELinux
# 放行防火墙
[root@server ~]# firewall-cmd --permanent --add-service=nfs
[root@server ~]# firewall-cmd --reload
firewall-cmd --list-all(验证是否放行成功)
# 永久关闭
sed -i "s/^SELINUX=enforcing$/SELINUX=permissive/g"/etc/selinux/config# 关闭SELinux
[root@server ~]# setenforce 0
3. 配置 Web 服务,放行80端口
# 配置一个web服务
[root@web ~]# vim /etc/nginx/conf.d/nfs.conf
[root@web ~]# cat /etc/nginx/conf.d/nfs.conf
server {access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;server_name 192.168.72.8;root /var/nginx/;
}# 验证配置文件是否有效
[root@web ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful# 重启 nginx 服务
[root@web ~]# systemctl restart nginx
[root@web ~]# firewall-cmd --permanent --add-port=80/tcp
success
[root@web ~]# firewall-cmd --reload
success
[root@web ~]# firewall-cmd --list-all
public (active)target: defaulticmp-block-inversion: nointerfaces: ens33sources:services: dhcpv6-client mdns nfs sshports: 80/tcpprotocols:forward: yesmasquerade: noforward-ports:source-ports:icmp-blocks:rich rules:
4. 搭建 nginx 服务
5. 浏览
打开浏览器,输入 http://192.168.72.8 来访问: