内容全为个人理解和自查资料梳理,欢迎各位大神指点!
每天学习较为零散。
day12
参数/命令 | 作用 | 示例 |
---|---|---|
yum install <包名> |
安装指定软件包 | yum install nginx |
yum remove <包名> |
卸载指定软件包 | yum remove nginx |
yum update |
更新所有已安装的软件包 | yum update |
yum update <包名> |
更新指定软件包 | yum update nginx |
yum search <关键词> |
搜索包含关键词的软件包 | yum search python3 |
yum provides <文件> |
查找哪个软件包提供指定文件 | yum provides /usr/sbin/nginx |
yum list |
列出所有已安装和可安装的软件包 | yum list |
yum list installed |
列出已安装的软件包 | yum list installed |
yum list available |
列出可安装但未安装的软件包 | yum list available |
yum repolist |
列出所有已启用的仓库 | yum repolist |
yum repolist all |
列出所有仓库(包括禁用和启用的) | yum repolist all |
yum repoinfo <仓库> |
查看指定仓库的详细信息 | yum repoinfo epel |
yum clean all |
清除所有缓存(修复元数据问题) | yum clean all |
yum makecache |
重新生成缓存(配合 clean all 使用) |
yum makecache |
yum history |
查看 yum 操作历史记录 | yum history |
yum history undo |
撤销指定历史操作(需先查看 history 获取 ID) |
yum history undo 3 |
yum groupinstall <组> |
安装软件包组(如开发工具) | yum groupinstall "Development Tools" |
yum groupremove <组> |
卸载软件包组 | yum groupremove "Development Tools" |
yum --help |
查看 yum 帮助文档 | yum --help |
#安装并配置 EPEL 仓库
[root@626nmn /]# sudo yum install -y epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
#下载安装 nginx
[root@626nmn /]# sudo yum install -y nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 5.1 kB 00:00:00
* base: mirrors.aliyun.com
* epel: d2lzkl7pfhq30w.cloudfront.net
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
#启动ngninx
[root@626nmn /]# systemctl start nginx
#开机自启nginx
[root@626nmn /]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@626nmn /]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2025-05-13 20:32:38 CST; 35s ago
Main PID: 2561 (nginx)
CGroup: /system.slice/nginx.service
├─2561 nginx: master process /usr/sbin/nginx
└─2563 nginx: worker process
#查看通过 rpm 安装的 Nginx 的所有文件路径
[root@626nmn /]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params(部分)
#查看配置仓库
[root@626nmn /]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: d2lzkl7pfhq30w.cloudfront.net
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
#列出可安装的软件包组
[root@626nmn /]# yum group list
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: d2lzkl7pfhq30w.cloudfront.net
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Environment Groups:
Minimal Install
Compute Node
Infrastructure Server
File and Print Server
Cinnamon Desktop
#安装需要的软件包
[root@626nmn /]# yum groupinstall "Development Tools"
命令 | 作用 |
---|---|
yum repolist all |
列出所有仓库(包括禁用和启用的) |
yum repoinfo |
查看某个仓库的详细信息(如 yum repoinfo epel ) |
yum-config-manager --enable |
启用指定仓库 |
yum-config-manager --disable |
禁用指定仓库 |
yum clean all |
清除仓库缓存(修复仓库元数据问题) |
#在官网找到下载连接
[root@626nmn /]# wget https://nginx.org/download/nginx-1.27.5.tar.gz
#下载完之后解压缩
[root@626nmn date]# tar -zxvf nginx-1.27.5.tar.gz
[root@626nmn date]# cd nginx-1.27.5
[root@626nmn nginx-1.27.5]# ll
total 868
drwxr-xr-x. 6 502 games 4096 May 13 21:30 auto
-rw-r--r--. 1 502 games 331666 Apr 16 20:07 CHANGES
-rw-r--r--. 1 502 games 507311 Apr 16 20:07 CHANGES.ru
-rw-r--r--. 1 502 games 5215 Apr 16 20:01 CODE_OF_CONDUCT.md
drwxr-xr-x. 2 502 games 168 May 13 21:30 conf
-rwxr-xr-x. 1 502 games 2611 Apr 16 20:01 configure
drwxr-xr-x. 4 502 games 72 May 13 21:30 contrib
-rw-r--r--. 1 502 games 4049 Apr 16 20:01 CONTRIBUTING.md
drwxr-xr-x. 2 502 games 40 May 13 21:30 html
-rw-r--r--. 1 502 games 1319 Apr 16 20:01 LICENSE
drwxr-xr-x. 2 502 games 21 May 13 21:30 man
-rw-r--r--. 1 502 games 14220 Apr 16 20:01 README.md
-rw-r--r--. 1 502 games 4857 Apr 16 20:01 SECURITY.md
drwxr-xr-x. 9 502 games 91 Apr 16 20:01 src
[root@626nmn nginx-1.27.5]#
# ./configure 运行当前目录下的 configure 脚本,检测系统环境并生成编译配置。
# --prefix= 指定 Nginx 的安装目录
[root@626nmn nginx-1.27.5]# ./configure --prefix=/date/new_nginx/
[root@626nmn nginx-1.27.5]# vim Makefile
[root@626nmn nginx-1.27.5]# make
[root@626nmn nginx-1.27.5]# make install
# ./configure 运行当前目录下的 configure 脚本,检测系统环境并生成编译配置。
# --prefix= 指定 Nginx 的安装目录
[root@626nmn nginx-1.27.5]# ./configure --prefix=/date/new_nginx/
[root@626nmn date]# cd new_nginx/
[root@626nmn new_nginx]# ll
total 4
drwxr-xr-x. 2 root root 4096 May 13 21:55 conf
drwxr-xr-x. 2 root root 40 May 13 21:55 html
drwxr-xr-x. 2 root root 6 May 13 21:55 logs
drwxr-xr-x. 2 root root 19 May 13 21:55 sbin
#找到nginx
[root@626nmn new_nginx]# cd sbin/
[root@626nmn sbin]# ll
total 3876
-rwxr-xr-x. 1 root root 3968120 May 13 21:55 nginx
[root@626nmn new_nginx]# ll
total 4
drwx------. 2 nobody root 6 May 13 21:57 client_body_temp
drwxr-xr-x. 2 root root 4096 May 13 21:55 conf
drwx------. 2 nobody root 6 May 13 21:57 fastcgi_temp
drwxr-xr-x. 2 root root 40 May 13 21:55 html
drwxr-xr-x. 2 root root 41 May 13 21:57 logs
drwx------. 2 nobody root 6 May 13 21:57 proxy_temp
drwxr-xr-x. 2 root root 19 May 13 21:55 sbin
drwx------. 2 nobody root 6 May 13 21:57 scgi_temp
drwx------. 2 nobody root 6 May 13 21:57 uwsgi_temp
[root@626nmn new_nginx]# cd conf
[root@626nmn conf]# ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
#编辑nginx.conf
[root@626nmn conf]# vim nginx.conf
#编辑user和listen端口
user root;
....
server {
listen 8081;
#重新读取nginx的配置文件
[root@626nmn new_nginx]# ./sbin/nginx -s reload
[root@626nmn new_nginx]# cd sbin/
[root@626nmn sbin]# ./nginx
[root@626nmn sbin]#
#可编辑界面
[root@626nmn new_nginx]# vim html/index.html
# Nginx 已成功运行,并且可以通过 8081 端口正常访问
[root@626nmn /]# curl http://localhost:8081
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@626nmn /]#
#配置环境
[root@626nmn /]# /date/new_nginx/sbin/nginx -s reload
[root@626nmn /]# vim /etc/profile.d/nginx.sh
export PATH=date/new_nginx/sbin:$PATH
[root@626nmn ~]# nginx
nginx: [emerg] bind() to 0.0.0.0:8081 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8081 failed (98: Address already in use)
nginx: [emerg] still could not bind()
执行方式 | 命令示例 | 是否需要 +x 权限 |
执行环境 | 是否影响当前 Shell | 典型场景 |
---|---|---|---|---|---|
直接执行 | ./script.sh |
✅ 需要 | 子进程 | ❌ 不影响 | 普通脚本执行 |
指定解释器 | bash script.sh |
❌ 不需要 | 子进程 | ❌ 不影响 | 快速测试无权限脚本 |
source 加载 |
source script.sh 或 . script.sh |
❌ 不需要 | 当前 Shell | ✅ 影响 | 加载环境变量、函数 |
绝对路径执行 | /path/to/script.sh |
✅ 需要 | 子进程 | ❌ 不影响 | 系统服务脚本 |
放入 PATH 后调用 |
myscript (需提前安装到 PATH ) |
✅ 需要 | 子进程 | ❌ 不影响 | 全局可用的自定义命令 |
管道传递 | echo "echo Hello" | bash |
❌ 不需要 | 子进程 | ❌ 不影响 | 动态生成命令并执行 |
输入重定向 | bash < script.sh |
❌ 不需要 | 子进程 | ❌ 不影响 | 从文件读取命令执行 |
SSH 远程执行 | ssh user@host "ls -l" |
视远程权限而定 | 远程主机子进程 | ❌ 不影响 | 批量管理服务器 |
定时任务(Cron) | * * * * * /path/to/script.sh |
✅ 需要 | 子进程 | ❌ 不影响 | 定时任务、自动化 |
后台运行 | nohup ./script.sh & |
✅ 需要 | 子进程(脱离终端) | ❌ 不影响 | 长期运行的服务或任务 |
[root@626nmn a1]# touch test.sh
#编写shell脚本
[root@626nmn a1]# vim test.sh
#! /bin/bash
echo "10.0.0.2" >> ip.txt
[root@626nmn a1]# sh test.sh
[root@626nmn a1]# cat ip.txt
10.0.0.7
10.0.0.8
10.0.0.5
10.0.0.7
10.0.0.7
10.0.0.7
10.0.0.7
10.0.0.2
[root@626nmn a1]# ./test.sh
-bash: ./test.sh: Permission denied
#加可执行权限
[root@626nmn a1]# chmod u+x test.sh
[root@626nmn a1]# ./test.sh
[root@626nmn a1]# cat ip.txt
10.0.0.7
10.0.0.8
10.0.0.5
10.0.0.7
10.0.0.7
10.0.0.7
10.0.0.7
10.0.0.2
10.0.0.2
[root@626nmn a1]#
# * 代表引导分区 不带 * 是主分区
[root@626nmn a1]# fdisk -l
磁盘名 容量大小 扇区数量
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
#每个扇区 512b大小
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos #dos标签 表示MBR格式磁盘
Disk identifier: 0x000ae0a9
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 2050047 819200 82 Linux swap / Solaris
/dev/sda3 2050048 41943039 19946496 83 Linux
[root@626nmn ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ae0a9
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 411647 204800 83 Linux
/dev/sda2 411648 2050047 819200 82 Linux swap / Solaris
/dev/sda3 2050048 41943039 19946496 83 Linux
[root@626nmn ~]# fdisk /dev/sda1
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xcabc08ba.
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n #扩展新分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p #主分区
Partition number (1-4, default 1): # 敲回车 默认
First sector (2048-409599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-409599, default 409599): +50M #分50m
Partition 1 of type Linux and of size 50 MiB is set
Command (m for help): P #打印分区
Disk /dev/sda1: 209 MB, 209715200 bytes, 409600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xcabc08ba
Device Boot Start End Blocks Id System
/dev/sda1p1 2048 104447 51200 83 Linux
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): p #打印分区
Disk /dev/sda1: 209 MB, 209715200 bytes, 409600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xcabc08ba
Device Boot Start End Blocks Id System
/dev/sda1p1 2048 104447 51200 83 Linux
Command (m for help): n #扩展新分区
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e #扩展额外分区
Partition number (2-4, default 2): 2
First sector (104448-409599, default 104448): #默认
Using default value 104448
Last sector, +sectors or +size{K,M,G} (104448-409599, default 409599): #默认
Using default value 409599
Partition 2 of type Extended and of size 149 MiB is set
Command (m for help): p #打印
Disk /dev/sda1: 209 MB, 209715200 bytes, 409600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xcabc08ba
Device Boot Start End Blocks Id System
/dev/sda1p1 2048 104447 51200 83 Linux
/dev/sda1p2 104448 409599 152576 5 Extended
Command (m for help): n #扩展
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l # 扩展逻辑分区
Adding logical partition 5
First sector (106496-409599, default 106496): #默认
Using default value 106496
Last sector, +sectors or +size{K,M,G} (106496-409599, default 409599): +50M #分50m
Partition 5 of type Linux and of size 50 MiB is set
Command (m for help): P #打印
Disk /dev/sda1: 209 MB, 209715200 bytes, 409600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xcabc08ba
Device Boot Start End Blocks Id System
/dev/sda1p1 2048 104447 51200 83 Linux
/dev/sda1p2 104448 409599 152576 5 Extended
/dev/sda1p5 106496 208895 51200 83 Linux
Command (m for help): q #q不保存退出 w保存
内核重新读取指定磁盘的分区表
#查看挂载空间剩余量
[root@626nmn ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda3 20G 2.0G 18G 11% /
/dev/sda1 197M 109M 89M 56% /boot
tmpfs 199M 0 199M 0% /run/user/0
#内核重新读取指定磁盘的分区表
[root@626nmn ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-3
可以处理 2TB 以上的大容量磁盘分区
修改磁盘标签为gpt
# 删除所有磁盘分区
[root@626nmn ~]# fdisk /dev/sda1
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x6e9768eb.
Command (m for help): d
No partition is defined yet!
#修改磁盘标签为gpt
[root@626nmn ~]# parted /dev/sda1
GNU Parted 3.1
Using /dev/sda1
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
align-check TYPE N check partition N for TYPE(min|opt) alignment
help [COMMAND] print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END make a partition
name NUMBER NAME name partition NUMBER as NAME
print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition
quit exit program
rescue START END rescue a lost partition near START and END
resizepart NUMBER END resize partition NUMBER
rm NUMBER delete partition NUMBER
select DEVICE choose the device to edit
disk_set FLAG STATE change the FLAG on selected device
disk_toggle [FLAG] toggle the state of FLAG on selected device
set NUMBER FLAG STATE change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER
unit UNIT set the default unit to UNIT
version display the version number and copyright information of GNU Parted
#选择 mklabel,mktable LABEL-TYPE, 只能对新磁盘操作,会丢失数据
(parted) mklabel gpt
#主分区
(parted) mkpart prinmary 0 500
#逻辑分区(gpt格式只有主分区、逻辑分区,没有扩展分区)
(parted) mkpart logical 501 1024
设备类型 | 设备文件命名规则 | 典型示例 | 说明 |
---|---|---|---|
IDE磁盘 | /dev/hd[a-z] |
/dev/hda , /dev/hdb |
旧式IDE接口硬盘,已逐步淘汰 |
SATA/SAS/USB磁盘 | /dev/sd[a-z] |
/dev/sda , /dev/sdb |
当前主流磁盘类型,按检测顺序分配字母 |
NVMe磁盘 | /dev/nvme[0-9]n[1-9]p[1-9] |
/dev/nvme0n1p1 |
NVMe协议SSD,格式为:nvme{控制器编号}n{命名空间编号}p{分区编号} |
虚拟磁盘 | /dev/vd[a-z] |
/dev/vda , /dev/vdb |
常见于虚拟化环境(如KVM) |
SCSI磁盘 | /dev/sd[a-z] 或 /dev/sg[0-9] |
/dev/sdc , /dev/sg2 |
SCSI/SATA/USB设备都映射到sdX,原始SCSI设备可能用sgX |
软盘 | /dev/fd[0-9] |
/dev/fd0 |
现已罕见 |
光驱 | /dev/cdrom |
/dev/cdrom |
通常是/dev/sr0 的符号链接 |
设备映射 | /dev/dm-[0-9] |
/dev/dm-0 |
LVM/RAID等创建的虚拟设备 |
分区 | /dev/{disk}[1-9] |
/dev/sda1 , /dev/nvme0n1p2 |
主分区1-4,逻辑分区从5开始 |
内存虚拟磁盘 | /dev/ram[0-9] |
/dev/ram0 |
内存虚拟磁盘设备 |