Files
security-book/00.基础阶段/02.Linux基础/03.Linux命令.md
2025-08-27 14:07:52 +08:00

554 lines
18 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 03.Linux命令
## 03.Linux命令
### 1. 初识shell
虽然我们已经安装好了系统,但是光会安装不会操作是不够的。我们还要像玩手机一样熟悉并记忆操作方法。
shell是系统的**用户界面**,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。实际上shell是一个**命令解释器**,它解释用户输入的命令并且把用户的意图传达给内核。(可以理解为用户与内核之间的翻译官角色)
![img](03.Linux命令/OZOSHbNQHEoUDF1z.png!thumbnail)
我们可以使用shell实现对Linux系统的大部分管理例如
1. 文件管理
2. 用户管理
3. 权限管理
4. 磁盘管理
5. 软件管理
6. 网络管理
使用shell的两种方式
* 交互式命令行
* 默认等待用户输入命令,输入一行回车后执行一行命令
* 效率低 适合少量的工作
* shell脚本
* 将需要执行的命令和逻辑判断语句都写入一个文件中,一起运行
* 效率高 适合完成复杂,重复性工作
### 2. bash shell提示符
登录Linux系统之后默认进入交互式的命令行界面在光标前边会出现提示符
```shell
[root@localhost ~]# 
[用户名@主机名 目录名]权限标识
```
* 用户名
* 当前登录的用户
* 主机名
* 当前这台主机的名字,默认叫`localhost`
* 目录名
* 当前光标所在的目录
* 当前用户家目录表示成`~`
* 权限标识
* 超级管理员权限就表示为`#`
* 普通用户标识为`$`
这个提示符格式被`$PS1`控制,我们可以查看这个变量
```shell
[root@localhost ~]# echo $PS1
[\u@\h \W]\$
# \u表示是用户名 \h表示的是主机名 \W表示的当前所在目录 \$是权限标识
[root@localhost ~]# export PS1="{\u@\h}\W \$"
{root@localhost}~ $
# 可以通过export命令修改PS1变量让提示符可以根据你的习惯变化
# 修改回原来的样子
{root@localhost}~ $ $export PS1="[\u@\h \W]\$"
```
### 3. shell语法
命令 选项 参数
```shell
[root@localhost ~]# cal --year -m 2024
```
* 命令
* `cal`是命令,用于查看日历
* 选项
* `--year`是选项,表示显示一整年,这个是一个长选项,也就是单词都拼全了,需要两条`-`符号
* `-m`是短选项,是首字母,表示每个星期的星期一作为第一天
* 对于有些命令而言,可以不写选项,这样命令会有个默认的行为
* 短选项可以多个合并在一起,比如上面的命令可以写成`-ym`其中y是year简写可以和m写在一起而长选项不支持写在一起
* 参数
* `2020`是参数参数是命令作用的对象表示查看的是2020年的日历
我们也可以查看这个命令的所有选项
```shell
[root@localhost ~]# cal --help
用法:
 cal [选项] [[[]]]
选项:
 -1, --one        只显示当前月份(默认)
 -3, --three      显示上个月、当月和下个月
 -s, --sunday     周日作为一周第一天
 -m, --monday     周一用为一周第一天
 -j, --julian     输出儒略日
 -y, --year       输出整年
 -V, --version    显示版本信息并退出
 -h, --help       显示此帮助并退出
```
### 4. 常用命令
Linux的常见命令比较多这边只列出初学者最常用的部分命令大家可以根据命令意思去进行练习。
注意Linux会准确的识别出命令的大小写所以大家需要注意大小写的问题。命令选项和参数之间是用空格进行分隔请大家在输入的时候注意不要缺失空格。
学习Linux最重要的就是以下三个方面
1. 命令的积累
2. 原理的掌握
3. 大量的实战
下面就是开始第一步,积累基础的命令
#### 4.1 ls
用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录)
```shell
ls [-alrtAFR] [name...]
```
##### 4.1.1 选项
* **-a**:显示所有文件及目录 (**.**开头的隐藏文件也会列出)
* **-l**除文件名称外亦将文件型态、权限、拥有者、文件大小等资讯详细列出r
* **-r**:将文件以相反次序显示(原定依英文字母次序)
* **-t**:将文件依建立时间之先后次序列出
* **-A**:同 -a ,但不列出 "." (目前目录) 及 ".." (父目录)
* **-F**:在列出的文件名称后加一符号;例如可执行档则加 "*", 目录则加 "/",链接加"@"
```shell
[root@localhost etc]# ls -F
adjtime gnupg/ modprobe.d/ sestatus.conf
aliases GREP_COLORS modules-load.d/ setroubleshoot/
alternatives/ groff/ motd sgml/
anacrontab group motd.d/ shadow
at.deny group- mtab@ shadow-
audit/ grub2.cfg@ nanorc shells
authselect/ grub.d/ netconfig skel/
bash_completion.d/ gshadow NetworkManager/ smartmontools/
bashrc gshadow- networks sos/
bindresvport.blacklist gss/ nftables/ ssh/
binfmt.d/ host.conf nsswitch.conf@ ssl/
bluetooth/ hostname nsswitch.conf.bak sssd/
chrony.conf hosts nvme/ statetab.d/
chrony.keys inittab openldap/ subgid
cifs-utils/ inputrc opt/ subgid-
cockpit/ iproute2/ os-release@ subuid
cron.d/ issue PackageKit/ subuid-
cron.daily/ issue.d/ pam.d/ sudo.conf
cron.deny issue.net passwd sudoers
cron.hourly/ kdump/ passwd- sudoers.d/
cron.monthly/ kdump.conf pinforc sudo-ldap.conf
crontab kernel/ pkcs11/ sysconfig/
cron.weekly/ krb5.conf pkgconfig/ sysctl.conf
crypto-policies/ krb5.conf.d/ pki/ sysctl.d/
crypttab ld.so.cache plymouth/ systemd/
csh.cshrc ld.so.conf pm/ system-release@
csh.login ld.so.conf.d/ polkit-1/ system-release-cpe
dbus-1/ libaudit.conf popt.d/ terminfo/
dconf/ libibverbs.d/ printcap tmpfiles.d/
debuginfod/ libnl/ profile tpm2-tss/
default/ libreport/ profile.d/ trusted-key.key
depmod.d/ libssh/ protocols tuned/
dhcp/ libuser.conf rc.d/ udev/
DIR_COLORS locale.conf rc.local@ updatedb.conf
DIR_COLORS.lightbgcolor localtime@ redhat-release@ vconsole.conf
dnf/ login.defs resolv.conf vimrc
dracut.conf logrotate.conf rocky-release virc
dracut.conf.d/ logrotate.d/ rocky-release-upstream vmware-tools/
environment lsm/ rpc wgetrc
ethertypes lvm/ rpm/ X11/
exports machine-id rsyncd.conf xattr.conf
favicon.png@ magic rsyslog.conf xdg/
filesystems mailcap rsyslog.d/ xml/
firewalld/ makedumpfile.conf.sample rwtab.d/ yum/
fonts/ man_db.conf samba/ yum.conf@
fprintd.conf mcelog/ sasl2/ yum.repos.d/
fstab microcode_ctl/ security/
fuse.conf mime.types selinux/
gcrypt/ mke2fs.conf services
```
* **-R**:若目录下有文件,则以下之文件亦皆依序列出
* **-h**:将显示出来的文件大小以合适的单位显示出来
##### 4.1.2 **实例**
* 查看当前目录下的文件
```shell
[root@localhost ~]# ls
```
* 查看根目录下的文件,查看/usr目录下的文件
```shell
[root@localhost ~]# ls /
[root@localhost ~]# ls /usr
```
* 查看当前目录下所有文件,包括隐藏文件
```shell
[root@localhost ~]# ls -a
```
* 查看当前目录下文件详情,包括隐藏文件
```shell
[root@localhost ~]# ls -lha
```
* 查看当前目录下的文件,并且显示出目录,文件,程序的区别
```shell
[root@localhost ~]# ls -F
anaconda-ks.cfg dirb/ dird/ file2 file4 ping*
dira/ dirc/ dire/ file1 file3 file5
# 可以看到普通文件只有文件名,可执行文件后面带*,文件夹后面带/
```
* 查看当前目录下的文件,如果有文件夹,那么将文件夹中的文件也显示出来
```shell
[root@localhost ~]# ls -FR
# dir这是一个目录在这个目录下的文件也全部显示出来
[root@localhost ~]# ls -FRl
# 显示详细的信息
```
##### 4.1.3 扩展知识
```shell
[root@localhost ~]# ls -ahl
总用量 24K
dr-xr-x---. 3 root root 139 4月 2 14:00 .
dr-xr-xr-x. 17 root root 224 6月 21 2020 ..
-rw-------. 1 root root 128 4月 2 09:37 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwxr-xr-x. 2 root root 32 4月 2 14:00 dir
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
-rw-r--r--. 1 root root 0 4月 2 14:00 test.txt
```
1. **第一列**共10位第1位表示文档类型`d`表示目录,`-`表示文件,`l`表示链接文件,`d`表示可随机存取的设备如U盘等`c`表示一次性读取设备如鼠标、键盘等。后9位依次对应三种身份所拥有的权限身份顺序为owner、group、others权限顺序为readable、writable、excutable。如`-r-xr-x---`的含义为**当前文档是一个文件,拥有者可读、可执行,同一个群组下的用户,可读、可写,其他人没有任何权限**
2. **第二列**表示链接数表示有多少个文件链接到inode号码。
3. **第三列**表示拥有者
4. **第四列**表示所属群组
5. **第五列**表示文档容量大小,单位字节
6. **第六列**表示文档最后修改时间,注意不是文档的创建时间哦
7. **第七列**表示文档名称。以点(.)开头的是隐藏文档
#### 4.2 cd
用于切换当前工作目录
```shell
cd [dirName]
```
##### 4.2.1 实例
* 跳转到`/usr/bin`目录下
```shell
[root@localhost ~]# cd /usr/bin
```
* 跳到自己的 home 目录
```shell
[root@localhost bin]# cd ~
```
* 跳到目前目录的上一层
```shell
[root@localhost ~]# cd ..
```
#### 4.3 pwd
显示工作目录
```shell
pwd [-LP]
```
* -L打印 $PWD 变量的值,如果它命名了当前的工作目录
* -P 打印当前的物理路径,不带有任何的符号链接
```shell
[root@localhost ~]# pwd
/root
```
#### 4.4 clear
用于清除屏幕
```shell
clear
```
#### 4.5 echo
用于字符串的输出
```shell
echo [-neE] 字符串
```
##### 4.5.1 选项
* **-n**不输出行尾的换行符
* **-e**:允许对下面列出的加反斜线转义的字符进行解释
* \\    反斜线
* \a    报警符(BEL)
* \b    退格符
* \c    禁止尾随的换行符
* \f    换页符
* \n    换行符
* \r    回车符
* \t    水平制表符
* \v    纵向制表符
* -E 禁止对在STRINGs中的那些序列进行解释
##### 4.5.2 实例
* 显示出`hello world`
```shell
[root@localhost ~]# echo "hello world"
```
* 用两行显示出`hello world`
```shell
[root@localhost ~]# echo -e "hello\nworld"
```
* 输出`hello world`的时候让系统发出警报音
```shell
[root@localhost ~]# echo -e "hello\aworld"
```
### 5. 系统命令
#### 5.1 poweroff
用于关闭计算器并切断电源
```shell
poweroff [-n] [-w] [-d] [-f] [-h]
```
##### 5.1.1 选项
* **-n**: 这个选项用于在关机时不执行文件系统的同步操作,即不调用 `sync()` 系统调用。通常,系统在关机时会自动同步所有挂载的文件系统,以确保所有挂起的磁盘写入操作都完成,从而避免数据丢失。使用 `-n` 参数可以跳过这个同步过程。
* **-w**: 仅记录关机信息到 `/var/log/wtmp` 文件中,但并不实际执行关机操作。
* **-d**: 不把记录写到 /var/log/wtmp 文件里
* **-f**:强制关机。此参数会立即停止所有进程并关闭系统,而不是正常关机流程。
#### 5.2 reboot
用来重新启动计算机
```shell
reboot [-n] [-w] [-d] [-f]
```
##### 5.2.1 选项
* **-n**: 这个选项用于在关机时不执行文件系统的同步操作,即不调用 `sync()` 系统调用。通常,系统在关机时会自动同步所有挂载的文件系统,以确保所有挂起的磁盘写入操作都完成,从而避免数据丢失。使用 `-n` 参数可以跳过这个同步过程。
* **-w**: 仅记录重启信息到 `/var/log/wtmp` 文件中,但并不实际执行重启操作。
* **-d**: 不把记录写到 /var/log/wtmp 档案里(-n 这个参数包含了 -d
* **-f**: 强迫重开机,不呼叫 shutdown 这个指令
#### 5.3 whoami
用于显示自身用户名称
```shell
[root@localhost ~]# whoami
root
```
### 6. 快捷键
| 快捷键 | 作用 |
| :----- | :----------------------- |
| ^C | 终止前台运行的程序 |
| ^D | 退出 等价exit |
| ^L | 清屏 |
| ^A | 光标移动到命令行的最前端 |
| ^E | 光标移动到命令行的后端 |
| ^U | 删除光标前所有字符 |
| ^K | 删除光标后所有字符 |
| ^R | 搜索历史命令,利用关键词 |
### 7. 帮助命令
#### 7.1 history
```shell
history [n]  n为数字列出最近的n条命令
```
##### 7.1.1 选项
* **-c**将目前shell中的所有history命令消除
* **-a**将目前新增的命令写入histfiles, 默认写入`~/.bash_history`
* **-r**将histfiles内容读入到目前shell的history记忆中
* **-w**将目前history记忆的内容写入到histfiles
##### 7.1.2 实例
* 将history的内容写入一个新的文件中
```shell
[root@localhost ~]# history -w histfiles.txt
```
* 情况所有的history记录注意并不清空`~/.bash_history`文件
```shell
[root@localhos t ~]# history -c
```
* 使用`!`执行历史命令。
* `! number`执行第几条命令
* `! command`从最近的命令查到以`command`开头的命令执行
* `!!`执行上一条
```shell
[root@localhost ~]# history
1 history
2 cat .bash_history
3 ping -c 3 baidu.com
4 history
[root@localhost ~]# !3
# 这里是执行第三条命令的意思
```
#### 7.2 help
显示命令的帮助信息
```shell
help [-dms] [内置命令]
```
##### 7.2.1 选项
* **-d**:输出每个主题的简短描述
* **-m**:以伪 man 手册的格式显示使用方法
* -s为每一个匹配 PATTERN 模式的主题仅显示一个用法
##### 7.2.2 实例
* 查看echo的帮助信息
```shell
[root@localhost ~]# help echo
```
#### 7.3 man
显示在线帮助手册页
```shell
man 需要帮助的命令或者文件
```
##### 7.3.1 快捷键
| 按键 | 用途 |
| :-------- | :--------------------------------- |
| 空格键 | 向下翻一页 |
| PaGe down | 向下翻一页 |
| PaGe up | 向上翻一页 |
| home | 直接前往首页 |
| end | 直接前往尾页 |
| / | 从上至下搜索某个关键词,如“/linux” |
| ? | 从下至上搜索某个关键词,如“?linux” |
| n | 定位到下一个搜索到的关键词 |
| N | 定位到上一个搜索到的关键词 |
| q | 退出帮助文档 |
##### 7.3.2 手册的结构
| 结构名称 | 代表意义 |
| :---------- | :----------------------- |
| NAME | 命令的名称 |
| SYNOPSIS | 参数的大致使用方法 |
| DESCRIPTION | 介绍说明 |
| EXAMPLES | 演示(附带简单说明) |
| OVERVIEW | 概述 |
| DEFAULTS | 默认的功能 |
| OPTIONS | 具体的可用选项(带介绍) |
| ENVIRONMENT | 环境变量 |
| FILES | 用到的文件 |
| SEE ALSO | 相关的资料 |
| HISTORY | 维护历史与联系方式 |
#### 7.4 alias
用于设置指令的别名
##### 7.4.1 实例
* 查看系统当前的别名
```shell
[root@localhost ~]# alias # 查看系统当前的别名
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 1241 8月 22 2018 anaconda-ks.cfg
drwxr-xr-x. 2 root root 19 8月 21 12:15 home
[root@xwz ~]# type -a ls # 查看命令类型
ls 是 `ls --color=auto' 的别名
ls 是 /usr/bin/ls
```
* 修改别名比如使用wl来查看IP地址相关信息
```shell
[root@localhost ~]# alias wl='ip address'
[root@localhost ~]# wl
```
* 为了让别名永久生效,可以讲修改别名的命令写入`bashrc`文件,这个文件中的命令会在每次登陆命令行的时候执行
```shell
[root@localhost ~]# echo "alias wl='ip address'" >> /etc/bashrc
```