docs: 添加Linux相关技术文档
新增多篇Linux技术文档,涵盖基础命令、文件操作、用户权限、网络配置、进程管理、软件安装、系统监控和磁盘管理等主题。每篇文档包含详细命令示例和使用说明,适合作为Linux系统管理参考手册。 文档内容包含: 1. Linux基础命令与快捷键 2. 文件操作与查找技巧 3. 用户管理与权限配置 4. 网络配置与防火墙管理 5. 进程与服务管理方法 6. 软件包管理与安装 7. 系统资源监控命令 8. 磁盘分区与LVM管理
This commit is contained in:
parent
ced925ba46
commit
57e553807d
File diff suppressed because it is too large
Load Diff
305
source/_posts/Linux基础命令.md
Normal file
305
source/_posts/Linux基础命令.md
Normal file
@ -0,0 +1,305 @@
|
||||
---
|
||||
title: Linux基础命令
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 命令行]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux基础命令
|
||||
|
||||
## 简介
|
||||
|
||||
> 在 Linux 系统中,没有盘符的概念,只有一个盘,以 `/` 为根目录。
|
||||
> Linux 没有图形化界面,通过指令操作。
|
||||
> Linux 指令可以传递参数。
|
||||
> 在 Linux 系统下,万事万物皆文件。
|
||||
|
||||
---
|
||||
|
||||
## Linux文件结构
|
||||
|
||||
Linux 系统主要目录结构如下:
|
||||
|
||||
| 目录 | 说明 |
|
||||
|-----|------|
|
||||
| `/bin` | 存放二进制可执行文件(重点) |
|
||||
| `/sbin` | 存放二进制可执行文件,只有 root 用户才能访问 |
|
||||
| `/etc` | 存放系统配置文件(重点) |
|
||||
| `/usr` | 存放共享的系统资源 |
|
||||
| `/home` | 普通用户的家目录(重点) |
|
||||
| `/root` | root 用户的家目录 |
|
||||
| `/dev` | 存放设备文件 |
|
||||
|
||||
---
|
||||
|
||||
## 快捷键
|
||||
|
||||
| 快捷键 | 功能 |
|
||||
|-------|------|
|
||||
| `Ctrl + u` | 删除命令行开始至光标处 |
|
||||
| `Ctrl + k` | 删除光标至命令行结尾 |
|
||||
| `Ctrl + a` | 光标移到最前 |
|
||||
| `Ctrl + e` | 光标移到最后 |
|
||||
| `Tab` | 自动补全 |
|
||||
| `↑` / `↓` | 浏览历史指令 |
|
||||
|
||||
---
|
||||
|
||||
## 目录操作
|
||||
|
||||
### 查看IP地址
|
||||
|
||||
```shell
|
||||
ip addr
|
||||
ip a
|
||||
```
|
||||
|
||||
### 清屏
|
||||
|
||||
```shell
|
||||
clear
|
||||
```
|
||||
|
||||
### 查看当前目录
|
||||
|
||||
```shell
|
||||
pwd
|
||||
```
|
||||
|
||||
### 跳转目录(cd)
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
cd [目录路径]
|
||||
```
|
||||
|
||||
**常用示例**:
|
||||
|
||||
```shell
|
||||
# 跳转指定目录(root目录下的www)
|
||||
cd /root/www
|
||||
|
||||
# 返回跳转前的目录
|
||||
cd -
|
||||
|
||||
# 跳转上一级目录
|
||||
cd ../
|
||||
|
||||
# 跳转根目录
|
||||
cd /
|
||||
|
||||
# 跳转root目录
|
||||
cd ~
|
||||
|
||||
# 跳转至主目录(后面跟个空格即可)
|
||||
cd
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 文件操作
|
||||
|
||||
### 列出目录内容(ls)
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
ls [选项] [目录]
|
||||
```
|
||||
|
||||
**常用参数**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-l` | 以长格式显示文件详情 |
|
||||
| `-a` | 显示所有文件(包括隐藏文件) |
|
||||
| `-h` | 以人类可读的方式显示文件大小 |
|
||||
| `-t` | 按修改时间排序 |
|
||||
| `-R` | 递归显示子目录 |
|
||||
|
||||
**常用示例**:
|
||||
|
||||
```shell
|
||||
# 列出当前目录下的所有文件及目录(不含隐藏的)
|
||||
ls
|
||||
|
||||
# 以长格式显示当前目录中的文件和目录
|
||||
ls -l
|
||||
|
||||
# 显示当前目录中的所有文件(包括隐藏文件)
|
||||
ls -a
|
||||
|
||||
# 以人类可读的方式显示文件大小
|
||||
ls -lh
|
||||
|
||||
# 按修改时间排序
|
||||
ls -t
|
||||
|
||||
# 递归显示子目录
|
||||
ls -R
|
||||
|
||||
# 显示指定文件的详细信息
|
||||
ls -l /etc/passwd
|
||||
```
|
||||
|
||||
### 创建目录(mkdir)
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
mkdir [选项] 目录名
|
||||
```
|
||||
|
||||
**常用参数**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-p` | 创建多级目录 |
|
||||
|
||||
**常用示例**:
|
||||
|
||||
```shell
|
||||
# 创建单级目录
|
||||
mkdir test
|
||||
|
||||
# 创建多级目录
|
||||
mkdir -p /root/test/subdir
|
||||
```
|
||||
|
||||
### 复制文件(cp)
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
cp [选项] 源文件 目标路径
|
||||
```
|
||||
|
||||
**常用参数**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-r` | 递归复制目录 |
|
||||
|
||||
**常用示例**:
|
||||
|
||||
```shell
|
||||
# 复制文件到指定目录
|
||||
cp aa.txt init/
|
||||
|
||||
# 复制目录及其内容到另一个目录
|
||||
cp -r init spring/
|
||||
```
|
||||
|
||||
### 移动/重命名文件(mv)
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
mv [选项] 源文件 目标路径
|
||||
```
|
||||
|
||||
**常用参数**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-f` | 覆盖前不提示 |
|
||||
| `-r` | 强行覆盖(慎用) |
|
||||
|
||||
**常用示例**:
|
||||
|
||||
```shell
|
||||
# 重命名文件
|
||||
mv aa.txt xiaoqiang.txt
|
||||
|
||||
# 移动文件到目录
|
||||
mv bb.txt spring/
|
||||
|
||||
# 覆盖前不提示
|
||||
mv -f spring aaaaa
|
||||
```
|
||||
|
||||
### 删除文件(rm)
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
rm [选项] 文件/目录
|
||||
```
|
||||
|
||||
**常用参数**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-f` | 强制删除,无确认提示 |
|
||||
| `-r` | 递归删除目录 |
|
||||
|
||||
> ⚠️ **注意**:`rm -rf` 命令非常危险,请谨慎使用!
|
||||
|
||||
**常用示例**:
|
||||
|
||||
```shell
|
||||
# 删除文件
|
||||
rm xiaoqiang.txt
|
||||
|
||||
# 强制删除文件
|
||||
rm -f spring.xml
|
||||
|
||||
# 递归删除目录
|
||||
rm -r init
|
||||
|
||||
# 递归强制删除(慎用!)
|
||||
rm -rf bbbbb
|
||||
|
||||
# 删除空目录
|
||||
rmdir aa
|
||||
|
||||
# 删除当前目录所有文件(慎用!)
|
||||
rm -rf *
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 文件详情说明
|
||||
|
||||
执行 `ls -l` 后,文件详情格式如下:
|
||||
|
||||
```
|
||||
drwxr-xr-x 2 root root 6 Apr 11 2018 home
|
||||
```
|
||||
|
||||
| 位置 | 含义 |
|
||||
|-----|------|
|
||||
| 第1位 | `d` 表示目录,`-` 表示文件 |
|
||||
| 第2-4位 | 文件创建者的权限(r=读,w=写,x=执行) |
|
||||
| 第5-7位 | 同组用户的权限 |
|
||||
| 第8-10位 | 其他用户的权限 |
|
||||
| 第11位 | 链接数 |
|
||||
| 创建者 | 文件创建者 |
|
||||
| 所属组 | 创建者所在的组 |
|
||||
| 大小 | 文件占用空间(字节) |
|
||||
| 日期 | 最后修改时间 |
|
||||
| 名称 | 文件名或目录名 |
|
||||
|
||||
---
|
||||
|
||||
## 输出操作
|
||||
|
||||
### 打印输出(echo)
|
||||
|
||||
```shell
|
||||
# 打印一句话
|
||||
echo "Hello World"
|
||||
|
||||
# 向文件写入内容(会追加并换行)
|
||||
echo "内容" >> file.txt
|
||||
```
|
||||
|
||||
### 退出操作
|
||||
|
||||
```shell
|
||||
# 退出几乎所有操作
|
||||
Ctrl + c
|
||||
```
|
||||
312
source/_posts/Linux文件操作.md
Normal file
312
source/_posts/Linux文件操作.md
Normal file
@ -0,0 +1,312 @@
|
||||
---
|
||||
title: Linux文件操作
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 文件管理]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux文件操作
|
||||
|
||||
## 文件查看
|
||||
|
||||
### cat 命令
|
||||
|
||||
显示文本文件内容的一部分。
|
||||
|
||||
```shell
|
||||
cat filename
|
||||
```
|
||||
|
||||
适用于查看小型文件:`.java`、`.py`、`.xml`、`.html`、`.js`、`.css` 等。
|
||||
|
||||
### more 命令
|
||||
|
||||
分页显示文本文件内容,只能向下查看,不能向上翻页。
|
||||
|
||||
```shell
|
||||
more filename
|
||||
```
|
||||
|
||||
### less 命令
|
||||
|
||||
分页显示文本文件内容,支持上下翻页。
|
||||
|
||||
```shell
|
||||
less filename
|
||||
```
|
||||
|
||||
**操作方式**:
|
||||
|
||||
| 按键 | 功能 |
|
||||
|-----|------|
|
||||
| `PgUp` / `PgDn` | 上下翻页 |
|
||||
| `↑` / `↓` | 一行一行查看 |
|
||||
| `q` | 退出查看 |
|
||||
|
||||
### head 命令
|
||||
|
||||
查看文本文件的前 N 行。
|
||||
|
||||
```shell
|
||||
# 查看前10行
|
||||
head -n 10 filename
|
||||
```
|
||||
|
||||
### tail 命令
|
||||
|
||||
查看文本文件的后 N 行,或实时监控文件变化。
|
||||
|
||||
```shell
|
||||
# 查看后10行
|
||||
tail -n 10 filename
|
||||
|
||||
# 实时监控文件变化
|
||||
tail -f filename
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 文件查找
|
||||
|
||||
### find 命令
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
find 路径 表达式
|
||||
```
|
||||
|
||||
#### 按文件名查找
|
||||
|
||||
```shell
|
||||
# 在根目录下查找文件(整个硬盘)
|
||||
find / -name httpd.conf
|
||||
|
||||
# 在指定目录下查找
|
||||
find /etc -name httpd.conf
|
||||
|
||||
# 使用通配符模糊查找
|
||||
find /etc -name '*srm*'
|
||||
```
|
||||
|
||||
#### 按文件特征查找
|
||||
|
||||
```shell
|
||||
# 查找最后10分钟访问的文件
|
||||
find / -amin -10
|
||||
|
||||
# 查找最后48小时访问的文件
|
||||
find / -atime -2
|
||||
|
||||
# 查找空文件或空目录
|
||||
find / -empty
|
||||
|
||||
# 查找属于指定组的文件
|
||||
find / -group cat
|
||||
|
||||
# 查找最后5分钟修改的文件
|
||||
find / -mmin -5
|
||||
|
||||
# 查找最后24小时修改的文件
|
||||
find / -mtime -1
|
||||
|
||||
# 查找属于指定用户的文件
|
||||
find / -user fred
|
||||
|
||||
# 查找大于10000字节的文件
|
||||
find / -size +10000c
|
||||
|
||||
# 查找小于1000KB的文件
|
||||
find / -size -1000k
|
||||
```
|
||||
|
||||
**文件大小单位**:`c`(字节)、`w`(双字)、`k`(KB)、`M`(MB)、`G`(GB)
|
||||
|
||||
#### 混合查找
|
||||
|
||||
```shell
|
||||
# 大于10000字节且最后2分钟内修改的文件
|
||||
find /tmp -size +10000c -and -mtime +2
|
||||
|
||||
# 用户是fred或george的文件
|
||||
find / -user fred -or -user george
|
||||
|
||||
# 不属于panda用户的文件
|
||||
find /tmp ! -user panda
|
||||
```
|
||||
|
||||
**逻辑参数**:`!`(非)、`-and` 或 `-a`(与)、`-or` 或 `-o`(或)
|
||||
|
||||
### du 命令
|
||||
|
||||
查看磁盘空间使用情况,查找占用空间较多的文件。
|
||||
|
||||
```shell
|
||||
# 查找/root下5个最大的文件
|
||||
du -ah /root | sort -nr | head -n5
|
||||
|
||||
# 查找当前目录下最大的5个目录
|
||||
du -ah | sort -nr | head -n5
|
||||
|
||||
# 查找根目录下最大的目录/文件(包括子文件夹)
|
||||
du -Sh / | sort -rh | head -n10
|
||||
|
||||
# 只看大小在GB范围内的文件
|
||||
du -ah / | grep "[0-9]G\b"
|
||||
|
||||
# 查看各目录占用
|
||||
du -sh /* 2>/dev/null | sort -rh | head -20
|
||||
|
||||
# 重点查看/var目录
|
||||
du -sh /var/* 2>/dev/null | sort -rh | head -20
|
||||
```
|
||||
|
||||
### 快速查找大文件
|
||||
|
||||
```shell
|
||||
# 查找用户目录下大于100M的文件
|
||||
find ~ -type f -size +100M | xargs ls -lhS
|
||||
|
||||
# 只显示文件大小和路径
|
||||
find ~ -type f -size +100M | xargs du -h | sort -hr
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 文件压缩
|
||||
|
||||
### 压缩概念
|
||||
|
||||
1. **打包**:把多个文件打成一个包
|
||||
2. **压缩**:把文件占用的大小进行压缩
|
||||
|
||||
### tar 命令
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
tar [选项] 文件名
|
||||
```
|
||||
|
||||
**常用参数**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-c` | 建立压缩文件(打包) |
|
||||
| `-x` | 解开压缩文件(解包) |
|
||||
| `-z` | 使用 gzip 压缩 |
|
||||
| `-v` | 显示压缩过程日志 |
|
||||
| `-f` | 指定文件名 |
|
||||
|
||||
**常用组合**:
|
||||
|
||||
| 命令 | 说明 |
|
||||
|-----|------|
|
||||
| `tar -cf` | 只打包,不压缩,不显示日志 |
|
||||
| `tar -xf` | 解压文件,不显示日志 |
|
||||
| `tar -cvf` | 只打包,不压缩,显示日志 |
|
||||
| `tar -xvf` | 解压文件,显示日志 |
|
||||
| `tar -zcvf` | 打包压缩,显示日志 |
|
||||
| `tar -zxvf` | 解压(最常用) |
|
||||
|
||||
**示例**:
|
||||
|
||||
```shell
|
||||
# 打包压缩
|
||||
tar -zcvf wwwroot.tar.gz wwwroot/
|
||||
|
||||
# 解压
|
||||
tar -zxvf wwwroot.tar.gz
|
||||
```
|
||||
|
||||
### tar.gz 与 tgz
|
||||
|
||||
两者本质相同,只是扩展名不同。都是通过 `tar` 打包后使用 `gzip` 压缩。
|
||||
|
||||
---
|
||||
|
||||
## 文件权限
|
||||
|
||||
### 权限说明
|
||||
|
||||
Linux 文件有三种权限:
|
||||
|
||||
| 权限 | 说明 |
|
||||
|-----|------|
|
||||
| `r` | 可读 |
|
||||
| `w` | 可写 |
|
||||
| `x` | 可执行 |
|
||||
|
||||
### chmod 命令
|
||||
|
||||
修改文件权限。
|
||||
|
||||
```shell
|
||||
# 设置权限为755(rwxr-xr-x)
|
||||
chmod 755 file
|
||||
```
|
||||
|
||||
**权限数字说明**:
|
||||
|
||||
| 数字 | 权限 |
|
||||
|-----|------|
|
||||
| 7 | rwx |
|
||||
| 6 | rw- |
|
||||
| 5 | r-x |
|
||||
| 4 | r-- |
|
||||
| 0 | --- |
|
||||
|
||||
### 查看目录权限
|
||||
|
||||
```shell
|
||||
sudo ls -ld /OLAP
|
||||
```
|
||||
|
||||
输出示例:
|
||||
|
||||
```
|
||||
drwxr-xr-x 5 root root 4096 Aug 5 08:27 /OLAP
|
||||
```
|
||||
|
||||
### 修改目录权限
|
||||
|
||||
```shell
|
||||
# 设置权限为775
|
||||
sudo chmod 775 /OLAP
|
||||
```
|
||||
|
||||
### 更改目录所有者
|
||||
|
||||
```shell
|
||||
# 将目录所有者更改为指定用户
|
||||
sudo chown user:user /OLAP
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 路径获取
|
||||
|
||||
```shell
|
||||
# 获取文件绝对路径
|
||||
readlink -f sample.txt
|
||||
|
||||
# 获取文件绝对路径(不解析符号链接)
|
||||
realpath -s sample.txt
|
||||
|
||||
# 在当前目录查找文件并显示完整路径
|
||||
find $(pwd) -name sample.txt
|
||||
|
||||
# 显示当前目录下文件的完整路径
|
||||
ls -l $PWD/sample.txt
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 文件目录大小统计
|
||||
|
||||
```shell
|
||||
# 列出当前目录下所有文件的大小及统计总和
|
||||
ls -lht
|
||||
```
|
||||
226
source/_posts/Linux用户与权限.md
Normal file
226
source/_posts/Linux用户与权限.md
Normal file
@ -0,0 +1,226 @@
|
||||
---
|
||||
title: Linux用户与权限
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 用户管理]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux用户与权限
|
||||
|
||||
## 用户管理
|
||||
|
||||
### 查看用户
|
||||
|
||||
```shell
|
||||
# 查看当前用户
|
||||
who am i
|
||||
|
||||
# 查看当前登录用户数量
|
||||
who --count
|
||||
```
|
||||
|
||||
### 用户组管理
|
||||
|
||||
```shell
|
||||
# 创建用户组
|
||||
groupadd groupname
|
||||
|
||||
# 删除用户组
|
||||
groupdel groupname
|
||||
```
|
||||
|
||||
### 用户管理
|
||||
|
||||
```shell
|
||||
# 创建用户并指定用户组
|
||||
useradd username -g usergroup
|
||||
|
||||
# 设置用户密码
|
||||
passwd username
|
||||
|
||||
# 切换用户
|
||||
# 从 root 切换到其他用户不需要输入密码
|
||||
# 从其他用户切换到 root 需要输入密码
|
||||
su username
|
||||
|
||||
# 退出登录
|
||||
exit
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 权限管理
|
||||
|
||||
### Linux文件权限
|
||||
|
||||
Linux 文件有三种权限:
|
||||
|
||||
| 权限 | 说明 |
|
||||
|-----|------|
|
||||
| `r` | 可读 |
|
||||
| `w` | 可写 |
|
||||
| `x` | 可执行 |
|
||||
|
||||
### chmod 命令
|
||||
|
||||
修改文件权限。
|
||||
|
||||
```shell
|
||||
# 数字方式设置权限
|
||||
chmod 755 file
|
||||
```
|
||||
|
||||
**权限数字说明**:
|
||||
|
||||
| 数字 | 权限 | 说明 |
|
||||
|-----|------|------|
|
||||
| 7 | rwx | 读、写、执行 |
|
||||
| 6 | rw- | 读、写 |
|
||||
| 5 | r-x | 读、执行 |
|
||||
| 4 | r-- | 只读 |
|
||||
| 0 | --- | 无权限 |
|
||||
|
||||
**755 权限含义**:`rwxr-xr-x`
|
||||
- 所有者:读、写、执行
|
||||
- 所属组:读、执行
|
||||
- 其他用户:读、执行
|
||||
|
||||
---
|
||||
|
||||
## sudo权限
|
||||
|
||||
### 开启sudo权限
|
||||
|
||||
#### 1. 添加sudoers文件写权限
|
||||
|
||||
```shell
|
||||
chmod u+w /etc/sudoers
|
||||
```
|
||||
|
||||
#### 2. 编辑sudoers文件
|
||||
|
||||
```shell
|
||||
vim /etc/sudoers
|
||||
```
|
||||
|
||||
找到 `root ALL=(ALL) ALL` 这行,在其下面添加:
|
||||
|
||||
```
|
||||
username ALL=(ALL) ALL
|
||||
```
|
||||
|
||||
#### 3. sudoers配置选项
|
||||
|
||||
```shell
|
||||
# 允许用户执行sudo命令(需要输入密码)
|
||||
username ALL=(ALL) ALL
|
||||
|
||||
# 允许用户组里的用户执行sudo命令(需要输入密码)
|
||||
%usergroup ALL=(ALL) ALL
|
||||
|
||||
# 允许用户执行sudo命令,不需要输入密码
|
||||
username ALL=(ALL) NOPASSWD: ALL
|
||||
|
||||
# 允许用户组里的用户执行sudo命令,不需要输入密码
|
||||
%usergroup ALL=(ALL) NOPASSWD: ALL
|
||||
```
|
||||
|
||||
#### 4. 撤销sudoers文件写权限
|
||||
|
||||
```shell
|
||||
chmod u-w /etc/sudoers
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ACL权限
|
||||
|
||||
> ACL(访问控制列表)提供更细粒度的权限控制,允许为特定用户或组设置特定权限。
|
||||
|
||||
### 安装ACL工具
|
||||
|
||||
```shell
|
||||
# Ubuntu/Debian
|
||||
sudo apt install acl -y
|
||||
|
||||
# Fedora/AlmaLinux
|
||||
sudo dnf install acl -y
|
||||
```
|
||||
|
||||
### 设置ACL权限
|
||||
|
||||
```shell
|
||||
# 为指定用户设置读写权限
|
||||
sudo setfacl -m u:user:rwx /OLAP
|
||||
```
|
||||
|
||||
### 验证ACL权限
|
||||
|
||||
```shell
|
||||
getfacl /OLAP
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
getfacl: Removing leading '/' from absolute path names
|
||||
# file: OLAP
|
||||
# owner: user
|
||||
# group: user
|
||||
user::rwx
|
||||
group::r-x
|
||||
other::r-x
|
||||
```
|
||||
|
||||
### 设置默认ACL权限
|
||||
|
||||
让新创建的文件和子目录自动继承特定权限:
|
||||
|
||||
```shell
|
||||
sudo setfacl -dm u:user:rwx /OLAP
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 目录权限修改
|
||||
|
||||
### 查看目录权限
|
||||
|
||||
```shell
|
||||
sudo ls -ld /OLAP
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
drwxr-xr-x 5 root root 4096 Aug 5 08:27 /OLAP
|
||||
```
|
||||
|
||||
### 修改目录权限
|
||||
|
||||
```shell
|
||||
# 设置权限为775(rwxrwxr-x)
|
||||
sudo chmod 775 /OLAP
|
||||
```
|
||||
|
||||
这样目录的所有者和所属组的用户都可以读写,其他用户只有读取和执行权限。
|
||||
|
||||
### 将用户加入目录所属组
|
||||
|
||||
```shell
|
||||
# 将用户加入root组(不推荐,root组权限过高)
|
||||
sudo usermod -aG root user
|
||||
```
|
||||
|
||||
重新登录或重启系统以使组变更生效。
|
||||
|
||||
### 更改目录所有者
|
||||
|
||||
```shell
|
||||
# 将目录所有者更改为指定用户
|
||||
sudo chown user:user /OLAP
|
||||
```
|
||||
|
||||
这样用户将拥有对目录的完全控制权。
|
||||
565
source/_posts/Linux磁盘管理.md
Normal file
565
source/_posts/Linux磁盘管理.md
Normal file
@ -0,0 +1,565 @@
|
||||
---
|
||||
title: Linux磁盘管理
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 磁盘管理]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux磁盘管理
|
||||
|
||||
## 磁盘空间查看
|
||||
|
||||
### df 命令
|
||||
|
||||
查看磁盘整体情况。
|
||||
|
||||
```shell
|
||||
# 统计磁盘整体情况
|
||||
df -lh
|
||||
```
|
||||
|
||||
### du 命令
|
||||
|
||||
查看目录占用空间。
|
||||
|
||||
```shell
|
||||
# 查看根目录下文件夹大小
|
||||
du -sh /*
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 安装与分区
|
||||
|
||||
### Linux分区顺序
|
||||
|
||||
1. **boot 分区**:引导启动分区,通常 200-500M
|
||||
2. **swap 分区**:缓存分区,通常为物理内存的 2 倍
|
||||
3. **根分区(/)**:剩余空间分配给根分区
|
||||
4. **home 分区**:可选
|
||||
|
||||
---
|
||||
|
||||
## LVM扩容
|
||||
|
||||
### 旧有卷扩容
|
||||
|
||||
将 `/home` 部分空间合并至 `/` 下。
|
||||
|
||||
#### 1. 查看当前分区
|
||||
|
||||
```shell
|
||||
df -h
|
||||
```
|
||||
|
||||
#### 2. 查看卷的文件系统类型
|
||||
|
||||
```shell
|
||||
mount | grep home
|
||||
```
|
||||
|
||||
#### 3. 安装备份工具
|
||||
|
||||
```shell
|
||||
dnf install -y xfsdump
|
||||
```
|
||||
|
||||
#### 4. 备份 /home
|
||||
|
||||
```shell
|
||||
xfsdump -f ~/sdb_dump/ /home -M sdb_home -L sdb_home_1
|
||||
```
|
||||
|
||||
#### 5. 卸载 /home
|
||||
|
||||
```shell
|
||||
umount /home/
|
||||
df -h
|
||||
```
|
||||
|
||||
#### 6. 移除逻辑卷
|
||||
|
||||
> ⚠️ 删除前请确保重要文件已备份!
|
||||
|
||||
```shell
|
||||
lvremove /dev/mapper/almalinux-home
|
||||
```
|
||||
|
||||
#### 7. 扩展根分区
|
||||
|
||||
```shell
|
||||
# 扩展根分区增加8.7G
|
||||
lvresize -L +8.7G /dev/mapper/almalinux-root
|
||||
lsblk
|
||||
```
|
||||
|
||||
#### 8. 扩展文件系统
|
||||
|
||||
```shell
|
||||
xfs_growfs /
|
||||
df -h
|
||||
```
|
||||
|
||||
#### 9. 重新创建 home 逻辑卷
|
||||
|
||||
```shell
|
||||
lvcreate -L 9G -n home almalinux
|
||||
lsblk
|
||||
```
|
||||
|
||||
#### 10. 扩展剩余空间给 home
|
||||
|
||||
```shell
|
||||
lvextend -l +100%FREE -n /dev/mapper/almalinux-home
|
||||
lsblk
|
||||
```
|
||||
|
||||
#### 11. 格式化并挂载
|
||||
|
||||
```shell
|
||||
# 格式化
|
||||
mkfs.xfs /dev/mapper/almalinux-home
|
||||
|
||||
# 挂载(因lv名称和挂载点不变,无需修改/etc/fstab)
|
||||
mount -a
|
||||
df -Th
|
||||
```
|
||||
|
||||
#### 12. 还原数据
|
||||
|
||||
```shell
|
||||
xfsrestore -f sdb_dump /home/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 新加卷扩容
|
||||
|
||||
#### 1. 查看现有分区大小
|
||||
|
||||
```shell
|
||||
df -Th
|
||||
```
|
||||
|
||||
#### 2. 查看扩容后磁盘大小
|
||||
|
||||
```shell
|
||||
lsblk
|
||||
```
|
||||
|
||||
#### 3. 创建分区
|
||||
|
||||
```shell
|
||||
fdisk /dev/sda
|
||||
```
|
||||
|
||||
**fdisk 操作步骤**:
|
||||
- 输入 `n` 创建新分区
|
||||
- 选择分区类型(主分区/扩展分区)
|
||||
- 设置分区大小
|
||||
- 输入 `w` 保存更改
|
||||
|
||||
#### 4. 刷新分区并创建物理卷
|
||||
|
||||
```shell
|
||||
partprobe /dev/sda
|
||||
pvcreate /dev/sda4
|
||||
```
|
||||
|
||||
#### 5. 查看卷组
|
||||
|
||||
```shell
|
||||
vgdisplay
|
||||
```
|
||||
|
||||
#### 6. 将物理卷扩展到卷组
|
||||
|
||||
```shell
|
||||
vgextend rl /dev/sda4
|
||||
```
|
||||
|
||||
#### 7. 查看逻辑卷
|
||||
|
||||
```shell
|
||||
lvdisplay
|
||||
```
|
||||
|
||||
#### 8. 扩展根分区逻辑卷
|
||||
|
||||
```shell
|
||||
lvextend -l +100%FREE /dev/rl/root
|
||||
```
|
||||
|
||||
#### 9. 刷新根分区
|
||||
|
||||
```shell
|
||||
xfs_growfs /dev/rl/root
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 分区格式转换
|
||||
|
||||
### Microsoft 基本数据 → Linux 文件系统
|
||||
|
||||
#### 1. 查看当前分区类型
|
||||
|
||||
```shell
|
||||
fdisk -l
|
||||
```
|
||||
|
||||
#### 2. 转换分区类型
|
||||
|
||||
```shell
|
||||
fdisk /dev/sdb
|
||||
# 输入 t 命令(转换分区类型)
|
||||
t
|
||||
# 输入 20(Linux file system)
|
||||
20
|
||||
# 输入 w(写入更改)
|
||||
w
|
||||
```
|
||||
|
||||
#### 3. 手动挂载硬盘
|
||||
|
||||
```shell
|
||||
mkdir /sdb
|
||||
mount -t ext3 /dev/sdb1 /sdb
|
||||
```
|
||||
|
||||
#### 4. 查看挂载结果
|
||||
|
||||
```shell
|
||||
df -Th
|
||||
```
|
||||
|
||||
#### 5. 设置自动挂载
|
||||
|
||||
查看硬盘UUID:
|
||||
|
||||
```shell
|
||||
blkid /dev/sdb1
|
||||
```
|
||||
|
||||
编辑 `/etc/fstab`:
|
||||
|
||||
```shell
|
||||
vim /etc/fstab
|
||||
# 添加以下行
|
||||
UUID=7d592b46-68dc-41c2-bdb3-7ee410f0bb33 /sdb ext3 defaults 0 0
|
||||
```
|
||||
|
||||
重载配置:
|
||||
|
||||
```shell
|
||||
systemctl daemon-reload
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### ext3 升级 ext4
|
||||
|
||||
#### 1. 确认当前文件系统类型
|
||||
|
||||
```shell
|
||||
df -Th | grep /dev/sdb1
|
||||
```
|
||||
|
||||
#### 2. 卸载目标分区
|
||||
|
||||
> ⚠️ 不能对正在使用的根分区操作
|
||||
|
||||
```shell
|
||||
umount /dev/sdb1
|
||||
```
|
||||
|
||||
#### 3. 检查并修复文件系统
|
||||
|
||||
```shell
|
||||
e2fsck -f /dev/sdb1
|
||||
```
|
||||
|
||||
#### 4. 将 ext3 转换为 ext4
|
||||
|
||||
```shell
|
||||
tune2fs -O has_journal,extent,huge_file,flex_bg,uninit_bg,dir_nlink,extra_isize /dev/sdb1
|
||||
```
|
||||
|
||||
#### 5. 查看转换是否成功
|
||||
|
||||
```shell
|
||||
dumpe2fs -h /dev/sdb1 | grep features
|
||||
```
|
||||
|
||||
确认输出中包含:`extent`、`huge_file`、`flex_bg`、`uninit_bg`、`dir_nlink`、`extra_isize`
|
||||
|
||||
#### 6. 再次检查文件系统
|
||||
|
||||
```shell
|
||||
e2fsck -f /dev/sdb1
|
||||
```
|
||||
|
||||
#### 7. 挂载并验证
|
||||
|
||||
```shell
|
||||
mount -t ext4 /dev/sdb1 /sdb
|
||||
df -Th | grep /dev/sdb1
|
||||
```
|
||||
|
||||
#### 8. 修改 /etc/fstab
|
||||
|
||||
```shell
|
||||
/dev/sdb1 /sdb ext4 defaults 0 0
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### NTFS → ext4
|
||||
|
||||
#### 1. 确认目标分区
|
||||
|
||||
```shell
|
||||
lsblk -f
|
||||
```
|
||||
|
||||
#### 2. 卸载分区
|
||||
|
||||
```shell
|
||||
sudo umount /dev/sda1
|
||||
```
|
||||
|
||||
#### 3. 格式化为 ext4
|
||||
|
||||
```shell
|
||||
sudo mkfs.ext4 /dev/sda1
|
||||
```
|
||||
|
||||
#### 4. 验证结果
|
||||
|
||||
```shell
|
||||
lsblk -f
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 挂载卷
|
||||
|
||||
### Windows 网络共享位置
|
||||
|
||||
#### 1. 创建挂载目录
|
||||
|
||||
```shell
|
||||
mkdir -p /mnt/wdshare/
|
||||
```
|
||||
|
||||
#### 2. 安装依赖
|
||||
|
||||
```shell
|
||||
dnf install -y cifs-utils
|
||||
```
|
||||
|
||||
#### 3. 挂载
|
||||
|
||||
```shell
|
||||
mount -t cifs -o username=user,password=backup //192.168.0.1/备份 /mnt/wdshare/
|
||||
```
|
||||
|
||||
#### 4. 永久挂载
|
||||
|
||||
编辑 `/etc/fstab`:
|
||||
|
||||
```shell
|
||||
//192.168.0.1/备份 /mnt/wdshare/ cifs username=user,password=backup 0 0
|
||||
```
|
||||
|
||||
重载配置:
|
||||
|
||||
```shell
|
||||
sudo systemctl daemon-reload
|
||||
sudo mount -a
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### NTFS 分区
|
||||
|
||||
#### 1. 识别NTFS分区
|
||||
|
||||
```shell
|
||||
sudo parted -l
|
||||
```
|
||||
|
||||
#### 2. 创建挂载点
|
||||
|
||||
```shell
|
||||
sudo mkdir /mnt/ntfs1
|
||||
```
|
||||
|
||||
#### 3. 安装依赖
|
||||
|
||||
```shell
|
||||
sudo apt update
|
||||
sudo apt install fuse -y
|
||||
sudo apt install ntfs-3g -y
|
||||
```
|
||||
|
||||
#### 4. 挂载分区
|
||||
|
||||
```shell
|
||||
sudo mount -t ntfs-3g /dev/sda1 /mnt/ntfs1/
|
||||
```
|
||||
|
||||
#### 5. 验证挂载
|
||||
|
||||
```shell
|
||||
df -Th
|
||||
```
|
||||
|
||||
#### 6. 永久挂载
|
||||
|
||||
编辑 `/etc/fstab`:
|
||||
|
||||
```shell
|
||||
/dev/sda1 /mnt/ntfs1 fuseblk defaults 0 0
|
||||
```
|
||||
|
||||
重载配置:
|
||||
|
||||
```shell
|
||||
sudo systemctl daemon-reload
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Swap配置
|
||||
|
||||
### 创建Swap分区
|
||||
|
||||
#### 1. 查看当前Swap大小
|
||||
|
||||
```shell
|
||||
free -h
|
||||
```
|
||||
|
||||
#### 2. 创建Swap文件
|
||||
|
||||
```shell
|
||||
# 创建4G的swap文件
|
||||
dd if=/dev/zero of=/home/swap bs=1024 count=4194304
|
||||
```
|
||||
|
||||
> `count` 值 = 大小(M) × 1024
|
||||
|
||||
#### 3. 格式化Swap分区
|
||||
|
||||
```shell
|
||||
mkswap /home/swap
|
||||
```
|
||||
|
||||
#### 4. 启用Swap
|
||||
|
||||
```shell
|
||||
swapon /home/swap
|
||||
```
|
||||
|
||||
#### 5. 设置自动挂载
|
||||
|
||||
编辑 `/etc/fstab`:
|
||||
|
||||
```shell
|
||||
/home/swap swap swap default 0 0
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 关闭Swap
|
||||
|
||||
```shell
|
||||
swapoff /home/swap
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### 修改Swap使用率
|
||||
|
||||
`swappiness` 值表示使用 swap 的倾向:
|
||||
- `0`:最大限度使用物理内存
|
||||
- `100`:积极使用 swap
|
||||
|
||||
#### 1. 查看当前值
|
||||
|
||||
```shell
|
||||
cat /proc/sys/vm/swappiness
|
||||
```
|
||||
|
||||
#### 2. 临时修改
|
||||
|
||||
```shell
|
||||
sysctl vm.swappiness=60
|
||||
```
|
||||
|
||||
#### 3. 永久修改
|
||||
|
||||
编辑 `/etc/sysctl.conf`:
|
||||
|
||||
```shell
|
||||
vm.swappiness=60
|
||||
```
|
||||
|
||||
应用更改:
|
||||
|
||||
```shell
|
||||
sysctl -p
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 内核升级
|
||||
|
||||
### CentOS 7.9 内核升级
|
||||
|
||||
#### 1. 查看当前内核版本
|
||||
|
||||
```shell
|
||||
uname -a
|
||||
```
|
||||
|
||||
#### 2. 查看系统版本
|
||||
|
||||
```shell
|
||||
cat /etc/redhat-release
|
||||
```
|
||||
|
||||
#### 3. 导入公钥
|
||||
|
||||
```shell
|
||||
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
|
||||
```
|
||||
|
||||
#### 4. 安装ELRepo
|
||||
|
||||
```shell
|
||||
yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
|
||||
```
|
||||
|
||||
#### 5. 安装新内核
|
||||
|
||||
```shell
|
||||
yum --enablerepo=elrepo-kernel install kernel-ml -y &&
|
||||
sed -i s/saved/0/g /etc/default/grub &&
|
||||
grub2-mkconfig -o /boot/grub2/grub.cfg
|
||||
```
|
||||
|
||||
#### 6. 重启系统
|
||||
|
||||
```shell
|
||||
reboot
|
||||
```
|
||||
|
||||
#### 7. 验证内核版本
|
||||
|
||||
```shell
|
||||
uname -a
|
||||
```
|
||||
220
source/_posts/Linux系统监控.md
Normal file
220
source/_posts/Linux系统监控.md
Normal file
@ -0,0 +1,220 @@
|
||||
---
|
||||
title: Linux系统监控
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 系统监控]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux系统监控
|
||||
|
||||
## CPU信息
|
||||
|
||||
### lscpu 命令
|
||||
|
||||
查看 CPU 的统计信息。
|
||||
|
||||
```shell
|
||||
lscpu
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
Architecture: x86_64
|
||||
CPU op-mode(s): 32-bit, 64-bit
|
||||
Byte Order: Little Endian
|
||||
CPU(s): 40
|
||||
On-line CPU(s) list: 0-39
|
||||
Thread(s) per core: 2
|
||||
Core(s) per socket: 10
|
||||
Socket(s): 2
|
||||
NUMA node(s): 2
|
||||
Vendor ID: GenuineIntel
|
||||
CPU family: 6
|
||||
Model: 85
|
||||
Model name: Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
|
||||
Stepping: 7
|
||||
CPU MHz: 999.963
|
||||
CPU max MHz: 3200.0000
|
||||
CPU min MHz: 1000.0000
|
||||
BogoMIPS: 4400.00
|
||||
Virtualization: VT-x
|
||||
L1d cache: 32K
|
||||
L1i cache: 32K
|
||||
L2 cache: 1024K
|
||||
L3 cache: 14080K
|
||||
```
|
||||
|
||||
### 查看 CPU 详细信息
|
||||
|
||||
```shell
|
||||
cat /proc/cpuinfo
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
processor : 0
|
||||
vendor_id : GenuineIntel
|
||||
cpu family : 6
|
||||
model : 85
|
||||
model name : Intel(R) Xeon(R) Silver 4210 CPU @ 2.20GHz
|
||||
stepping : 7
|
||||
microcode : 0x5003303
|
||||
cpu MHz : 999.963
|
||||
cache size : 14080 KB
|
||||
physical id : 0
|
||||
siblings : 20
|
||||
core id : 0
|
||||
cpu cores : 10
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 内存信息
|
||||
|
||||
### free 命令
|
||||
|
||||
概要查看内存情况。
|
||||
|
||||
```shell
|
||||
free -m
|
||||
```
|
||||
|
||||
**输出示例**(单位:MB):
|
||||
|
||||
```
|
||||
total used free shared buff/cache available
|
||||
Mem: 31595 14770 3182 253 13643 16150
|
||||
Swap: 65535 0 65535
|
||||
```
|
||||
|
||||
### 查看内存详细使用
|
||||
|
||||
```shell
|
||||
cat /proc/meminfo
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
MemTotal: 32354112 kB
|
||||
MemFree: 3377564 kB
|
||||
MemAvailable: 16657484 kB
|
||||
Buffers: 725916 kB
|
||||
Cached: 12127832 kB
|
||||
SwapCached: 0 kB
|
||||
Active: 21031256 kB
|
||||
Inactive: 5694748 kB
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 硬盘信息
|
||||
|
||||
### lsblk 命令
|
||||
|
||||
查看硬盘和分区分布。
|
||||
|
||||
```shell
|
||||
lsblk
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
|
||||
sda 8:0 0 447.1G 0 disk
|
||||
├─sda1 8:1 0 200M 0 part /boot/efi
|
||||
├─sda2 8:2 0 1G 0 part /boot
|
||||
├─sda3 8:3 0 380G 0 part /
|
||||
└─sda4 8:4 0 64G 0 part [SWAP]
|
||||
sdb 8:16 0 2.2T 0 disk
|
||||
└─sdb1 8:17 0 2.2T 0 part /test
|
||||
```
|
||||
|
||||
### fdisk 命令
|
||||
|
||||
查看硬盘和分区的详细信息。
|
||||
|
||||
```shell
|
||||
fdisk -l
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
磁盘 /dev/sda:480.1 GB, 480103981056 字节,937703088 个扇区
|
||||
Units = 扇区 of 1 * 512 = 512 bytes
|
||||
扇区大小(逻辑/物理):512 字节 / 4096 字节
|
||||
I/O 大小(最小/最佳):4096 字节 / 4096 字节
|
||||
磁盘标签类型:gpt
|
||||
Disk identifier: F6E9395D-610B-4BB3-B289-8F6A96811113
|
||||
|
||||
# Start End Size Type Name
|
||||
1 2048 411647 200M EFI System EFI System Partition
|
||||
2 411648 2508799 1G Microsoft basic
|
||||
3 2508800 799426559 380G Microsoft basic
|
||||
4 799426560 933644287 64G Linux swap
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 网卡信息
|
||||
|
||||
### 查看网卡硬件信息
|
||||
|
||||
```shell
|
||||
lspci | grep -i 'eth'
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
04:00.0 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
|
||||
04:00.1 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
|
||||
```
|
||||
|
||||
### 查看所有网络接口
|
||||
|
||||
```shell
|
||||
ifconfig -a
|
||||
```
|
||||
|
||||
**输出示例**:
|
||||
|
||||
```
|
||||
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
|
||||
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
|
||||
ether 02:42:66:fe:52:a2 txqueuelen 0 (Ethernet)
|
||||
|
||||
em1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
|
||||
inet 192.168.6.20 netmask 255.255.255.0 broadcast 192.168.6.255
|
||||
ether 2c:ea:7f:a9:fc:76 txqueuelen 1000 (Ethernet)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 综合监控
|
||||
|
||||
### top 命令
|
||||
|
||||
动态显示进程和系统资源使用情况。
|
||||
|
||||
```shell
|
||||
top
|
||||
```
|
||||
|
||||
### htop 命令
|
||||
|
||||
更友好的交互式进程查看器(需安装)。
|
||||
|
||||
```shell
|
||||
# 安装
|
||||
yum install -y htop
|
||||
|
||||
# 运行
|
||||
htop
|
||||
```
|
||||
183
source/_posts/Linux网络配置.md
Normal file
183
source/_posts/Linux网络配置.md
Normal file
@ -0,0 +1,183 @@
|
||||
---
|
||||
title: Linux网络配置
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 网络配置]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux网络配置
|
||||
|
||||
## 配置网络
|
||||
|
||||
### 编辑网络配置文件
|
||||
|
||||
```shell
|
||||
cd /etc/sysconfig/network-scripts
|
||||
vi ifcfg-ens33
|
||||
```
|
||||
|
||||
### 配置文件示例
|
||||
|
||||
```shell
|
||||
TYPE=Ethernet
|
||||
PROXY_METHOD=none
|
||||
BROWSER_ONLY=no
|
||||
BOOTPROTO=dhcp
|
||||
DEFROUTE=yes
|
||||
IPV4_FAILURE_FATAL=no
|
||||
IPV6INIT=yes
|
||||
IPV6_AUTOCONF=yes
|
||||
IPV6_DEFROUTE=yes
|
||||
IPV6_FAILURE_FATAL=no
|
||||
IPV6_ADDR_GEN_MODE=stable-privacy
|
||||
NAME=ens33
|
||||
UUID=08913b58-0bc2-42c5-8b59-6782e0029d7b
|
||||
DEVICE=ens33
|
||||
ONBOOT=yes
|
||||
```
|
||||
|
||||
> **重要**:修改 `ONBOOT=yes` 以启用网络。
|
||||
|
||||
### 重启网络服务
|
||||
|
||||
```shell
|
||||
systemctl restart network
|
||||
```
|
||||
|
||||
### 查看IP地址
|
||||
|
||||
```shell
|
||||
ip addr
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 端口映射
|
||||
|
||||
### 使用 iptables 映射端口
|
||||
|
||||
```shell
|
||||
# 将80端口映射到8080端口
|
||||
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to-port 80
|
||||
```
|
||||
|
||||
**参数说明**:
|
||||
- `--dport`:目标端口
|
||||
- `--to-port`:来源端口
|
||||
|
||||
### 查看 iptables 规则
|
||||
|
||||
```shell
|
||||
iptables -t nat -L -n -v
|
||||
```
|
||||
|
||||
输出示例:
|
||||
|
||||
```
|
||||
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
|
||||
pkts bytes target prot opt in out source destination
|
||||
12925 4377K DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0
|
||||
0 0 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 redir ports 80
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 防火墙配置
|
||||
|
||||
### firewall-cmd 命令
|
||||
|
||||
CentOS 7 防火墙管理工具。
|
||||
|
||||
#### 放行端口
|
||||
|
||||
```shell
|
||||
# 放行特定端口(永久生效)
|
||||
firewall-cmd --add-port=6379/tcp --permanent
|
||||
```
|
||||
|
||||
#### 移除端口
|
||||
|
||||
```shell
|
||||
# 移除已放行的端口
|
||||
firewall-cmd --permanent --remove-port=8080/tcp
|
||||
```
|
||||
|
||||
#### 查询端口
|
||||
|
||||
```shell
|
||||
# 查询端口是否开放
|
||||
firewall-cmd --query-port=8080/tcp
|
||||
```
|
||||
|
||||
#### 查看开放端口
|
||||
|
||||
```shell
|
||||
# 查看所有开放的端口
|
||||
firewall-cmd --list-all
|
||||
```
|
||||
|
||||
#### 重载防火墙
|
||||
|
||||
```shell
|
||||
# 修改配置后需重载生效
|
||||
firewall-cmd --reload
|
||||
```
|
||||
|
||||
#### 指定作用域放行
|
||||
|
||||
```shell
|
||||
# 在public作用域放行端口
|
||||
firewall-cmd --zone=public --add-port=3306/tcp --permanent
|
||||
```
|
||||
|
||||
**参数说明**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `--zone` | 作用域 |
|
||||
| `--add-port` | 添加端口,格式为端口/协议 |
|
||||
| `--permanent` | 永久生效,无此参数重启后失效 |
|
||||
|
||||
---
|
||||
|
||||
## 网络状态查看
|
||||
|
||||
### netstat 命令
|
||||
|
||||
```shell
|
||||
# 查看监听中的端口
|
||||
netstat -lnp | grep 8080
|
||||
```
|
||||
|
||||
**参数说明**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-l` | 显示监控中的服务器的 Socket |
|
||||
| `-n` | 直接使用 IP 地址 |
|
||||
| `-p` | 显示程序识别码和名称 |
|
||||
|
||||
---
|
||||
|
||||
## 网卡信息查看
|
||||
|
||||
### 查看网卡硬件信息
|
||||
|
||||
```shell
|
||||
lspci | grep -i 'eth'
|
||||
```
|
||||
|
||||
输出示例:
|
||||
|
||||
```
|
||||
04:00.0 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
|
||||
04:00.1 Ethernet controller: Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe
|
||||
```
|
||||
|
||||
### 查看所有网络接口
|
||||
|
||||
```shell
|
||||
ifconfig -a
|
||||
```
|
||||
192
source/_posts/Linux软件安装.md
Normal file
192
source/_posts/Linux软件安装.md
Normal file
@ -0,0 +1,192 @@
|
||||
---
|
||||
title: Linux软件安装
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 软件安装]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux软件安装
|
||||
|
||||
## 包管理器
|
||||
|
||||
### rpm 命令
|
||||
|
||||
本地安装软件包。
|
||||
|
||||
```shell
|
||||
# 安装rpm包
|
||||
rpm -ivh package.rpm
|
||||
|
||||
# 卸载rpm包
|
||||
rpm -e package
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### yum 命令
|
||||
|
||||
CentOS 软件包管理器,需要配置源。
|
||||
|
||||
#### 常用命令
|
||||
|
||||
```shell
|
||||
# 安装软件
|
||||
yum install -y package
|
||||
|
||||
# 更新软件
|
||||
yum update package
|
||||
|
||||
# 删除软件
|
||||
yum remove package
|
||||
|
||||
# 搜索软件
|
||||
yum search package
|
||||
|
||||
# 清理缓存
|
||||
yum clean all
|
||||
|
||||
# 生成缓存
|
||||
yum makecache
|
||||
```
|
||||
|
||||
#### 更换阿里源
|
||||
|
||||
```shell
|
||||
yum install -y wget &&
|
||||
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup &&
|
||||
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &&
|
||||
yum clean all &&
|
||||
yum makecache
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### dnf 命令
|
||||
|
||||
Fedora/AlmaLinux 软件包管理器(yum 的替代)。
|
||||
|
||||
#### 常用命令
|
||||
|
||||
```shell
|
||||
# 安装软件
|
||||
dnf install -y package
|
||||
|
||||
# 更新系统
|
||||
dnf update
|
||||
|
||||
# 删除软件
|
||||
dnf remove package
|
||||
|
||||
# 搜索软件
|
||||
dnf search package
|
||||
|
||||
# 清理缓存
|
||||
dnf clean all
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## wget 下载工具
|
||||
|
||||
### 基本语法
|
||||
|
||||
```shell
|
||||
wget [参数] [URL地址]
|
||||
```
|
||||
|
||||
### 基本示例
|
||||
|
||||
```shell
|
||||
# 下载文件
|
||||
wget https://example.com/file.zip
|
||||
|
||||
# 指定保存文件名
|
||||
wget -O 图片名.png https://www.baidu.com/img/bd_logo1.png
|
||||
```
|
||||
|
||||
### 记录和输入文件参数
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-o` | 把记录写到文件中 |
|
||||
| `-a` | 把记录追加到文件中 |
|
||||
| `-d` | 打印调试输出 |
|
||||
| `-q` | 安静模式(没有输出) |
|
||||
| `-v` | 冗长模式(缺省设置) |
|
||||
| `-nv` | 关掉冗长模式 |
|
||||
| `-i` | 下载文件中出现的 URLs |
|
||||
| `-F` | 把输入文件当作 HTML 格式 |
|
||||
|
||||
### 下载参数
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-t` | 设定最大尝试链接次数(0 表示无限制) |
|
||||
| `-O` | 把文档写到文件中 |
|
||||
| `-nc` | 不要覆盖存在的文件 |
|
||||
| `-c` | 接着下载没下载完的文件 |
|
||||
| `-N` | 不要重新下载文件除非比本地文件新 |
|
||||
| `-S` | 打印服务器的回应 |
|
||||
| `-T` | 设定响应超时的秒数 |
|
||||
| `-w` | 两次尝试之间间隔秒数 |
|
||||
| `-Q` | 设置下载的容量限制 |
|
||||
| `--limit-rate` | 限定下载速率 |
|
||||
|
||||
---
|
||||
|
||||
## 常用软件安装
|
||||
|
||||
### 安装 Vim 编辑器
|
||||
|
||||
```shell
|
||||
yum install -y vim
|
||||
```
|
||||
|
||||
### 安装 screenFetch
|
||||
|
||||
```shell
|
||||
# 下载安装包
|
||||
wget https://github.com/KittyKatt/screenFetch/archive/master.zip
|
||||
|
||||
# 安装 unzip
|
||||
yum install unzip
|
||||
|
||||
# 解压
|
||||
unzip master.zip
|
||||
|
||||
# 移动到系统目录
|
||||
mv screenFetch-master/screenfetch-dev /usr/bin/screenfetch
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 环境变量配置
|
||||
|
||||
### 配置 Java 环境变量
|
||||
|
||||
#### 1. 解压并移动 JDK
|
||||
|
||||
```shell
|
||||
mv jdk1.8.0_301/ /usr/
|
||||
```
|
||||
|
||||
#### 2. 编辑 profile 文件
|
||||
|
||||
```shell
|
||||
vim /etc/profile
|
||||
```
|
||||
|
||||
在文件末尾添加:
|
||||
|
||||
```shell
|
||||
export JAVA_HOME=/usr/jdk1.8.0_301
|
||||
export PATH=$PATH:$JAVA_HOME/bin
|
||||
```
|
||||
|
||||
#### 3. 重新加载配置
|
||||
|
||||
```shell
|
||||
source /etc/profile
|
||||
```
|
||||
156
source/_posts/Linux进程与服务管理.md
Normal file
156
source/_posts/Linux进程与服务管理.md
Normal file
@ -0,0 +1,156 @@
|
||||
---
|
||||
title: Linux进程与服务管理
|
||||
date: 2021-04-07 16:04:58
|
||||
author: 文永达
|
||||
top_img: https://gcore.jsdelivr.net/gh/volantis-x/cdn-wallpaper/abstract/00E0F0ED-9F1C-407A-9AA6-545649D919F4.jpeg
|
||||
tags: [Linux, Shell, 进程管理]
|
||||
categories: [操作系统, Linux]
|
||||
---
|
||||
|
||||
# Linux进程与服务管理
|
||||
|
||||
## 进程管理
|
||||
|
||||
### ps 命令
|
||||
|
||||
查看进程状态。
|
||||
|
||||
```shell
|
||||
# 查看前台进程
|
||||
ps
|
||||
|
||||
# 查看所有进程详细信息
|
||||
ps -aux
|
||||
|
||||
# 查看所有进程详细信息(含父进程ID)
|
||||
ps -ef
|
||||
```
|
||||
|
||||
**输出字段说明**:
|
||||
|
||||
| 字段 | 说明 |
|
||||
|-----|------|
|
||||
| UID | 用户 |
|
||||
| PID | 进程ID |
|
||||
| PPID | 父进程ID |
|
||||
|
||||
> 父进程ID为1表示系统进程。
|
||||
|
||||
### top 命令
|
||||
|
||||
动态显示进程状态。
|
||||
|
||||
```shell
|
||||
top
|
||||
```
|
||||
|
||||
### 查找特定进程
|
||||
|
||||
```shell
|
||||
# 查看所有进程并搜索指定进程
|
||||
ps -aux | grep network
|
||||
```
|
||||
|
||||
### kill 命令
|
||||
|
||||
根据 PID 终止进程。
|
||||
|
||||
```shell
|
||||
# 终止进程
|
||||
kill PID
|
||||
|
||||
# 强制终止进程
|
||||
kill -9 PID
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 服务管理
|
||||
|
||||
### systemctl 命令
|
||||
|
||||
操作系统服务。
|
||||
|
||||
**基本语法**:
|
||||
|
||||
```shell
|
||||
systemctl [操作] 服务名
|
||||
```
|
||||
|
||||
**常用操作**:
|
||||
|
||||
| 操作 | 说明 |
|
||||
|-----|------|
|
||||
| `status` | 查看服务状态 |
|
||||
| `stop` | 终止服务 |
|
||||
| `start` | 启动服务 |
|
||||
| `restart` | 重启服务 |
|
||||
| `enable` | 设置开机自启 |
|
||||
| `disable` | 禁用开机自启 |
|
||||
|
||||
**示例**:
|
||||
|
||||
```shell
|
||||
# 查看MySQL服务状态
|
||||
systemctl status mysqld
|
||||
|
||||
# 启动网络服务
|
||||
systemctl start network
|
||||
|
||||
# 设置服务开机自启
|
||||
systemctl enable mysqld
|
||||
|
||||
# 启动并设置开机自启
|
||||
systemctl enable --now mysqld
|
||||
```
|
||||
|
||||
### 常见服务名称
|
||||
|
||||
| 服务 | 服务名 |
|
||||
|-----|------|
|
||||
| 网络服务 | `network` |
|
||||
| 防火墙服务 | `firewalld` |
|
||||
| MySQL | `mysqld` |
|
||||
| Containerd | `containerd` |
|
||||
| BuildKit | `buildkit` |
|
||||
|
||||
---
|
||||
|
||||
## 网络状态
|
||||
|
||||
### netstat 命令
|
||||
|
||||
查看网络连接状态。
|
||||
|
||||
```shell
|
||||
# 查看监听中的端口
|
||||
netstat -lnp | grep 8080
|
||||
```
|
||||
|
||||
**参数说明**:
|
||||
|
||||
| 参数 | 说明 |
|
||||
|-----|------|
|
||||
| `-l` | 显示监控中的服务器的 Socket |
|
||||
| `-n` | 直接使用 IP 地址,不通过域名服务器 |
|
||||
| `-p` | 显示正在使用 Socket 的程序识别码和名称 |
|
||||
|
||||
---
|
||||
|
||||
## Shell脚本
|
||||
|
||||
### shebang 说明
|
||||
|
||||
Shell 脚本开头通常包含 `#!/bin/bash`,这行称为 **shebang** 或 **hashbang**。
|
||||
|
||||
```shell
|
||||
#!/bin/bash
|
||||
|
||||
echo 'Hello, World!'
|
||||
```
|
||||
|
||||
**作用**:告诉系统使用 `bash` 作为脚本的解释器,无需在运行时指定。
|
||||
|
||||
**注意事项**:
|
||||
- `#!` 和 `/bin/bash` 之间的空格无关紧要
|
||||
- 使用 `#!/bin/zsh` 表示使用 zsh 解释器
|
||||
Loading…
x
Reference in New Issue
Block a user