Linux服务与管理-step1
2024-05-30 06:01:40
文章目录
- Linux服务与管理
- vuritual machine的快捷键:
- 开始时创建的分区:
- 第二章 文件管理
- 根目录下的目录
- 文件路径
- 相对路径
- 绝对路径
- 目录与文件的操作
- 目录操作
- 创建目录 ==-p递归创建目录== `mkdir`
- 查看目录 `pwd` `ls`
- 删除目录 `rmdir`
- 文件操作
- 创建文件
- 查看文件
- 复制文件
- 移动文件
- 删除文件
- 常见的问题
- Vim编辑器
- 常用命令
- 光标定位命令
- 文本编辑命令
- 从命令模式进入其他模式
- 末行模式常用的命令
- 文件时间
- 文件类型
- 带三章 用户管理
- 用户/组
- UID和GID
- 用户/组相关文件
- 创建用户/组
- 删除用户/组
- 修改用户密码
- 配置文件
- su/sudo 命令
- 第四章 文件权限
- 基本权限UGO
- 设置文件属性与权限
- 修改属主和属组
- 只修改文件属组
- 所有子目录或文件同时修改属主或属组,`chown 和 chgrp 后边添加 -R`
- `chmod` 赋值的功能格式
- 为一个文件插入权限
- 同时给所有对象添加权限
- 同时给所有用户删除权限
- 同时删除所有对象的全部权限
- 一次给不同的对象增加或删除不同的权限
- 使用递归参数 `-R`
- 使用数字权限 4+2+1
- 基本权限ACL
- ACL的基本用法
- `-m`修改当前文件的ACL权限
- 新建用户 添加ACL权限
- 为组添加权限
- `-` 对文件没有任何权限
- `-x` 刪除用户对文件的所有权限,此时用户属于other
- `-b` 删除所有的扩展ACL权限,回到UGO基本权限
- ACL高级特性
- 最大有限权限 `mask`
- `mask` 的作用和特性
- `default` 继承
- 高级权限
- SUID权限
- SGID权限
- Sticky权限
- 文件属性 chattr
- 进程掩码 umask
- 第五章 进程管理
- 初始进程
- 查看进程
- 静态查看进程
- 动态查看进程
- 信号控制进程
- kill命令 终止指定进程的运行
- 进程的优先级
- 使用top命令查看nice级别
- 使用ps命令查看nice级别
- 作业控制
- 第六章 I/O重定向与管道
- I/O重定向
- 输出重定向
- 错误输出重定向
- 正确结果与错误结果都输出到相同的位置
- 正确结果与错误结果都输出**重定向**到相同的位置
- 正确的保存在 `right.txt`, 错误的保存在 null.txt`
- 输入重定向
- 重定向综合案例
- 建立多行文件 `cat` 在键盘上输入文本按回车换行,Ctrl+d 结束
- 用户自定义结束符号
- 重定向建立脚本操纵Shell
- 多条命令输出重定向
- 两条命令重定向,添加括号
- 让命令在后台运行,输出重定向到文件,终止后台程序
- 重定向综合案例
- 建立多行文件 `cat` 在键盘上输入文本按回车换行,Ctrl+d 结束
- 用户自定义结束符号
- 重定向建立脚本操纵Shell
- 多条命令输出重定向
- 两条命令重定向,添加括号
Linux服务与管理
vuritual machine的快捷键:
- Ctrl+Alt 虚拟环境切换到物理环境
- Ctrl +G 物理→虚拟
开始时创建的分区:
- /boot 引导分区
- /swap 交换分区
- / 根分区
第二章 文件管理
根目录下的目录
目录 | 说明 |
---|---|
/bin | 二进制可执行文件 常用命令 |
/etc | 系统管理和配置文件 |
/home | 用户文件根目录 |
/usr | 用户安装的程序和系统程序 |
/tmp | 临时文件 |
/root | 系统管理员目录 |
/var | 运行时需要改变数据的文件/大文件的溢出区 |
/sbin | 二进制可执行文件,只有root可以访问 |
文件路径
相对路径
/ 根目录
绝对路径
../ 当前目录的上级目录
./当前目录
目录与文件的操作
目录操作
创建目录 -p递归创建目录
mkdir
[root@localhost /]# cd /temp
[root@localhost temp]# mkdir 111
[root@localhost temp]# cd 111
[root@localhost 111]# mkdir -p 222/333/444
[root@localhost 111]# cd 222/333
[root@localhost 333]# ll
总用量 0
drwxr-xr-x. 2 root root 6 3月 16 22:23 444
查看目录
pwd
ls
命令 | 备注 |
---|---|
[root@localhost 333]# pwd /temp/111/222/333 |
显示当前所在的目录 |
[root@localhost 333]# pwd -P /temp/111/222/333 |
-P显示实际工作目录 |
[root@localhost 333]# ls -a . … 444 |
查看隐藏的目录文件 |
[root@localhost 333]# ls -l 总用量 0 drwxr-xr-x. 2 root root 6 3月 16 22:23 444 |
查看目录与文件的属性 |
[root@localhost 333]# cd [root@localhost ~]# |
cd直接回到根目录 |
[root@localhost 333]# cd [root@localhost ~]# cd /temp/111/222/333 [root@localhost 333]# cd - /root |
==cd -==回到上次的目录 |
删除目录
rmdir
命名 | 备注 |
---|---|
[root@localhost ~]# cd /temp/111/222 [root@localhost 222]# rmdir 333 rmdir: 删除 “333” 失败: 目录非空 [root@localhost 222]# cd 333 [root@localhost 333]# rmdir 444 [root@localhost 333]# |
删除目录,非空会报错 |
[root@localhost 222]# cd /temp/111/222/333 [root@localhost 333]# cd …/ [root@localhost 222]# cd …/ [root@localhost 111]# rmdir 222 rmdir: 删除 “222” 失败: 目录非空 [root@localhost 111]# rmdir -p 222 rmdir: 删除 “222” 失败: 目录非空 [root@localhost 111]# rmdir -p 222/333 |
-p连同上层空目录一起删除 |
文件操作
创建文件
touch filename
创建一个空白的文件echo "content">>filename
写入新的内容到filename中
[root@localhost 111]# cd /temp/111
[root@localhost 111]# touch test.txt
[root@localhost 111]# ll
总用量 0
-rw-r--r--. 1 root root 0 3月 16 22:47 test.txt
[root@localhost 111]# echo "hello world" >>test.txt
[root@localhost 111]# ll
总用量 4
-rw-r--r--. 1 root root 12 3月 16 22:48 test.txt
echo设置文件、属性
[root@localhost 111]# touch -d "2022-03-16 22:50" test.txt
[root@localhost 111]# ll
总用量 4
-rw-r--r--. 1 root root 12 3月 16 2022 test.txt
查看文件
1.cat命令
-n 显示行号
-A 显示不可显示的换行符/制表符
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# cat -n /etc/hosts1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain42 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@localhost ~]# cat -A /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4$
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6$
2.more命令
以逐页的方式显示文件的内容
空格键——下一页
b——上一页
[root@localhost ~]# more /etc/profile
# /etc/profile# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
3.less命令
分页显示
pageUp——向上看文件
pageDown——向下看文件 q键退出
[root@localhost ~]# less /etc/profile
4.head命令
看文件的前几行 -n
[root@localhost ~]# head -3 /etc/profile
# /etc/profile# System wide environment and startup programs, for login setup
5.tail命令
看文件的后几行 -n
-f ——查看动态文件
[root@localhost ~]# tail -3 /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
shigen:x:1000:1000:shigen:/home/shigen:/bin/bash
6.grep命令
对文件的内容过滤、搜索关键词,快速找到内容
[root@localhost ~]# grep 'shigen' /etc/passwd
shigen:x:1000:1000:shigen:/home/shigen:/bin/bash
[root@localhost ~]# grep '^shigen' /etc/passwd
shigen:x:1000:1000:shigen:/home/shigen:/bin/bash
[root@localhost ~]# grep 'shigen$' /etc/passwd
复制文件
cp sourcefile targetfile
[root@localhost ~]# cd /temp
[root@localhost temp]# touch aa.txt
[root@localhost temp]# echo 'hello shigen'>>aa.txt
[root@localhost temp]# cat -n aa.txt1 hello shigen
[root@localhost temp]# touch bb.txt
[root@localhost temp]# echo 'hello world'>>bb.txt
[root@localhost temp]# cat -n bb.txt1 hello world
[root@localhost temp]# cp aa.txt bb.txt
cp:是否覆盖"bb.txt"? y
[root@localhost temp]# cat aa.txt
hello shigen
[root@localhost temp]# cat bb.txt
hello shigen
[root@localhost temp]# ll
总用量 8
drwxr-xr-x. 2 root root 22 3月 16 22:47 111
-rw-r--r--. 1 root root 13 3月 16 23:08 aa.txt
-rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt
文件目录较长时,花括号括起不同的部分
[root@localhost temp]# touch cc.txt
[root@localhost temp]# echo 'hello cc'>>cc.txt
[root@localhost temp]# cat -n cc.txt1 hello cc
[root@localhost temp]# cd
[root@localhost ~]# cp -rf /temp/{aa.txt,cc.txt}
cp:是否覆盖"/temp/cc.txt"? y
[root@localhost ~]# cat -n cc.txt
cat: cc.txt: 没有那个文件或目录
[root@localhost ~]# cat -n /temp/cc.txt1 hello shigen
移动文件
mv sourcefile targetfile
[root@localhost 111]# ll
总用量 4
-rw-r--r--. 1 root root 12 3月 16 2022 test.txt
[root@localhost 111]# mv /temp/aa.txt /temp/111
[root@localhost 111]# ll
总用量 8
-rw-r--r--. 1 root root 13 3月 16 23:08 aa.txt
-rw-r--r--. 1 root root 12 3月 16 2022 test.txt
删除文件
rm file2(目录文件或目录)
rm -rf 删除目录和文件且不提示
rm -rf /
进行递归操作
[root@localhost 111]# cd /temp
[root@localhost temp]# ll
总用量 8
drwxr-xr-x. 2 root root 36 3月 16 23:19 111
-rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt
-rw-r--r--. 1 root root 13 3月 16 23:13 cc.txt
[root@localhost temp]# rm cc.txt
rm:是否删除普通文件 "cc.txt"?y
[root@localhost temp]# ll
总用量 4
drwxr-xr-x. 2 root root 36 3月 16 23:19 111
-rw-r--r--. 1 root root 13 3月 16 23:10 bb.txt
常见的问题
在Linux环境下正常打开Windows文件
yum -y install dos2unix
Vim编辑器
[root@localhost ~]# which vim
/usr/bin/vim
[root@localhost ~]# yum -y install vim-enhanced
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
常用命令
光标定位命令
命令 | 描述 |
---|---|
h,j,k,l | 光标上下左右移动 也可用方向键 |
0 $ | 光标移到行首 行尾 |
gg G | 光标移到第一行首个字符的位置 最后一行首个字符的位置 |
/字符串 | 快速定位到字符串所在的行 |
/^d | 定位首个字母为d的行 |
/txt$ | 定位结尾为txt的行 |
文本编辑命令
命令 | 描述 |
---|---|
n yy | 复制当前行 |
n dd | 删除当前行 |
p | 粘贴 |
x | 删除光标所在字符 |
D | 从光标处删除到行尾 |
u | 撤销 |
^r | 重做 |
R | 进入替换模式 |
从命令模式进入其他模式
命令 | 描述 |
---|---|
o | 进入编辑模式,光标下面另起一行 |
a | 进入编辑模式 光标后一位 |
i | 进入编辑模式 Esc 退出 |
: | 进入末行模式 |
V | 进入可视行模式 多行会被选中 |
v | 可视模式 |
^v | 可视块模式 |
R | 替换模式 |
末行模式常用的命令
命令 | 备注 |
---|---|
:w | 保存 |
:q | 退出 |
:wq | 保存并退出 |
:w! | 强制保存 |
:q! | 不保存强制退出 |
:wq! | |
:set nu | 显示行号 |
:set nonu | 不显示行号 |
:整数 | 跳转到该行 |