liunx 基础命令

xshell 快捷键

Ctrl+l 清屏

shift+tab 切换终段

ctrl+c 取消当前命令执行

. 代表当前目录

Ctrl+a 光标移动至行首

Ctrl+e 光标移动至行尾

“*‘ 代表任意所有字符

Ctrl+u 清空至行首

Ctrl+w 删除一个单词

 

图形界面

Ctrl+ shift+“+” 调整图形化终端界面变大

Ctrl+ shift+“-” 调整图形化终端界面变小

命令提示符含义

[root@localhost~]# root 表明当前登录账户,localhost 表明主机名 ,~ 为当前所在目录(实例为家目录),#表明当前用所拥有的权限(# 为管理员权限,$ 为普通用户权限)

命令一般格式

命令字 [- 选项...] [参数...](.. 表示可以有多个,[] 表示可选)

选项

短选项 类似于 ls 命令 -a -d -l(单个字符),可以合并使用(-la)

长选项 --help(单词),长选项通常不能合并使用

参数:命令的执行对象,文件 / 程序 / 目录等

 

liunx 基础快捷键

pwd 显示当前所在目录
[root@localhost ~]# pwd
/root
exit 退出系统
ls 查看当前目录下的文件内容和详细信息 ls [选项]
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面
  • -a 显示当前目录下的所有内容

[root@localhost ~]# ls -a
.               .bash_logout   .cache   .dbus         initial-setup-ks.cfg .Xauthority 视频 下载
..               .bash_profile .config .esd_auth     .local               公共         图片 音乐
anaconda-ks.cfg .bashrc       .cshrc   .ICEauthority .tcshrc               模板         文档 桌面
  • -l 以长目录显示目录下的内容及详细属性

[root@localhost ~]# ls -l
总用量 8
-rw-------. 1 root root 1744 8 月   3 10:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 1792 8 月   3 10:09 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 8月   3 10:17 公共
drwxr-xr-x. 2 root root    6 8月   3 10:17 模板
drwxr-xr-x. 2 root root    6 8月   3 10:17 视频
drwxr-xr-x. 2 root root    6 8月   3 10:17 图片
drwxr-xr-x. 2 root root    6 8月   3 10:17 文档
drwxr-xr-x. 2 root root    6 8月   3 10:17 下载
drwxr-xr-x. 2 root root    6 8月   3 10:17 音乐
drwxr-xr-x. 2 root root    6 8月   3 10:17 桌面
  • -h 人性化的显示目录内容的大小(KB、MB、GB)

[root@localhost ~]# ls -hl
总用量 8.0K
-rw-------. 1 root root 1.8K 8 月   3 10:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 1.8K 8 月   3 10:09 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 8月   3 10:17 公共
drwxr-xr-x. 2 root root    6 8月   3 10:17 模板
drwxr-xr-x. 2 root root    6 8月   3 10:17 视频
drwxr-xr-x. 2 root root    6 8月   3 10:17 图片
drwxr-xr-x. 2 root root    6 8月   3 10:17 文档
drwxr-xr-x. 2 root root    6 8月   3 10:17 下载
drwxr-xr-x. 2 root root    6 8月   3 10:17 音乐
drwxr-xr-x. 2 root root    6 8月   3 10:17 桌面
  • -d 仅显示目录本身而不显示目录下的内容

[root@localhost ~]# ls -dl
dr-xr-x---. 14 root root 4096 8月   4 00:32 .
  • -i 查看 inode 号(类似于文件的身份证号)

[root@localhost ~]# ls -i
33574991 anaconda-ks.cfg       33575039 公共  33696768 视频  52077823 文档   1420781 音乐
33574999 initial-setup-ks.cfg  17837892 模板  17837893 图片   1420780 下载  52077822 桌面
  • -R 显示当前目录下的文件夹及其中的文件

[root@localhost ~]# ls -R
.:
anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面
hello.txt 公共 视频 文档 音乐
./ 公共:
1
./ 公共 /1:
./ 模板:
./ 视频:
./��片:
./ 文档:
./ 下载:
./ 音乐:
./ 桌面:
stat 查看文件的元数据信息 stat 文件
[root@localhost ~]# stat hello.txt
文件:"hello.txt"
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:33696798 硬链接:1
权限:(0644/-rw-r--r--) Uid:(0/ root) Gid:(0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-08-04 00:32:03.573102766 +0800
最近更改:2022-08-04 00:32:03.573102766 +0800
最近改动:2022-08-04 00:32:03.573102766 +0800
创建时间:-
mkdir 创建目录 mkdir [选项] 目录名
// 在当前所在目录创建目录
[root@localhost ~]# mkdir rhce
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
hello.txt rhce 模板 图片 下载 桌面
[root@localhost ~]#
  • -p 递归创建多个目录

[root@localhost /]# mkdir -p /home/rhce
[root@localhost /]# cd /home/
[root@localhost home]# ls
rhce visitor
[root@localhost home]#
  • mkdir {目录名,目录名....} 一次创建多个目录

[root@localhost ~]# mkdir {e1,2}
[root@localhost ~]# ls
2 e1 initial-setup-ks.cfg 模板 图片 下载 桌面
anaconda-ks.cfg hello.txt 公共 视频 文档 音乐
[root@localhost ~]#
touch 创建新的空白文件 touch 文件名
[root@localhost ~]# touch test
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
hello.txt test 模板 图片 下载 桌面
[root@localhost ~]#
cp 用于复制文件或者目录,并且可以在复制时修改文件名 (目标目录如果不存在就是修改文件名,存在就复制) cp [选项] 源文件或者目录 目标目录
[root@localhost ~]# cp test /opt/
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
rh test
[root@localhost opt]#
  • -r 复制目录 (可同时复制多个目录)

[root@localhost ~]# cp test /opt/
cp: 略过目录"test"
[root@localhost ~]# cp -r test /opt/
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
rh test
[root@localhost opt]#
  • -p 保留源文件属性不变(如修改时间,归属关系,权限)

[root@localhost ~]# cp anaconda-ks.cfg /opt/
[root@localhost ~]# ll /opt/anaconda-ks.cfg
-rw-------. 1 root root 1744 84 19:22 /opt/anaconda-ks.cfg
[root@localhost ~]# ll anaconda-ks.cfg
-rw-------. 1 root root 1744 83 10:06 anaconda-ks.cfg
[root@localhost ~]# cp anaconda-ks.cfg test/
[root@localhost ~]# rm -rf test/anaconda-ks.cfg
[root@localhost ~]# cp -p anaconda-ks.cfg test/
[root@localhost ~]# ll test/anaconda-ks.cfg
-rw-------. 1 root root 1744 83 10:06 test/anaconda-ks.cfg
[root@localhost ~]#
mv 移动目录和文件也可修改文件名 (可同时移动多个) mv [选项] 源文件... 目标路径
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
hello.txt test 模板 图片 下载 桌面
[root@localhost ~]# mv test/ /opt/
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面
hello.txt 公共 视频 文档 音乐
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
anaconda-ks.cfg test
[root@localhost opt]#
cat 查看文件内容 cat [选项] 文件名
[root@localhost ~]# cat flag
hello
world
  • -n 查看文件内容的时候显示行号

[root@localhost ~]# cat -n flag
1 hello
2 world
[root@localhost ~]#
less 对文件输出的内容进行分页显示的工具,常用于显示内容量较大的文件 less [选项] 文件名
  • -n 以行号显示文件内容

[root@localhost ~]# less -n /etc/services
  • 使用技巧

键盘上下键翻页

pgdn 向上翻一页

pgup 向下翻一页

G 跳转到文件的最后一行

gg 跳转到文件的行首

/ [字符串] 搜索指定字符串(n 从上往下搜索,N 从下往上搜索)

q 退出

head 查看文件开头内容 head [选项] 文件名
  • -n [行数] 显示指定行数的内容(不加选项 -n 默认显示 10 行)

[root@localhost ~]# head -n5 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
[root@localhost ~]#
tail 查看文件末尾的信息 tail [选项] 文件名
  • -n [行数] 显示指定行数的内容(不加选项 -n 默认显示 10 行)

[root@localhost ~]# tail -n 5 /etc/services
com-bardac-dw 48556/tcp # com-bardac-dw
com-bardac-dw 48556/udp # com-bardac-dw
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
matahari 49000/tcp # Matahari Broker
[root@localhost ~]#
  • -f 动态显示(通常用来查看日志或者实时更新的文件)

rm 用来删除文件或者目录 rm [选项] 文件(目录)
  • -r 删除目录

  • -f 强制删除(不在询问是否删除)

软连接和硬连接(类似于 Windows 中的快捷方式)
  • 软连接:软连接可以跨分区,可以对目录文件进行连接,源文件删除之后,连接文件不可用

  • 软连接格式: ln -s 源文件路径 目标路径

  • 连接文件颜色为浅蓝色

  • 创建连接一定要写绝对路径

[root@localhost ~]# ln -s /root/hello.txt /opt/
[root@localhost ~]# ls /opt/
anaconda-ks.cfg hello.txt test
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
anaconda-ks.cfg hello.txt test
  • 硬连接特点:硬连接不可以跨分区,不可以对目标文件进行连接,源文件删除之后,连接文件仍然可用 ( 感觉像复制,但不能复制目录,不过此链接可以实现同步更新)

  • 硬连接格式:ln 源文件路径 目标文件路径

[root@localhost ~]# ln /root/flag /opt/
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面
flag 公共 视频 文档 音乐
[root@localhost ~]# ll /opt/
总用量 8
-rw-------. 1 root root 1744 84 19:22 anaconda-ks.cfg
-rw-r--r--. 2 root root 12 84 21:44 flag
[root@localhost ~]#
which 用来搜索某一命令的路径 which 命令
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]#
type 用来查看命令属于内部命令还是外部命令 type 命令
[root@localhost ~]# type hash
hash 是 shell 内嵌
hash -r 清除哈希表的缓存
内部命令与外部命令
  • 什么是命令:用来实现某一功能的指令或程序

  • 命令的执行依赖于解释器

    • 用户——解释器(shell 外壳)——内核

 

查看 mac 地址

```bash
显示所有 ip 的 mac 地址
[root@localhost ~]# arp -a
gateway (192.168.75.1) at 00:50:56:e0:88:85 [ether] on ens33
? (192.168.75.2) at 00:50:56:c0:00:08 [ether] on ens33
[root@localhost ~]#

显示指定 ip 的 mac 地址
[root@localhost ~]# arp -a 192.168.75.1
gateway (192.168.75.1) at 00:50:56:e0:88:85 [ether] on ens33
[root@localhost ~]#

```

远程连接虚拟机

```bash
C:\Users\Administrator>ssh root@192.168.75.24
root@192.168.75.24's password:
Last failed login: Mon Jul 31 03:57:49 CST 2023 from 192.168.75.2 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Jul 31 03:46:11 2023 from 192.168.75.2
[root@localhost ~]#
```