1、基本操作命令

命令提示符

[root@zbx-agent01 ~]

root:当前登录的用户

zbx-agent01:机器名称

~:当前工作目录

#显示当前登录的用户
[root@zbx-agent01 ~]# whoami
root
#显示机器名称
[root@zbx-agent01 zabbix]# hostname
zbx-agent01
#显示当前工作目录
[root@zbx-agent01 ~]# pwd
/root

系统操作

[root@zbx-agent01 ~]# logout

路径操作

#到制定路径
[root@zbx-agent01 ~]# cd 路径
#到当前路径
[root@zbx-agent01 ~]# cd .
#到上一级目录
[root@zbx-agent01 ~]# cd ..
#来回切换路径
[root@zbx-agent01 ~]# cd -
#到用户目录
[root@zbx-agent01 /]# cd ~

linux目录结构

/dev 存放抽象硬件、U盘等

/lib 存放系统库文件

/sbin 存放特权级二进制文件、如ls cd mv命令

/bin 存放二进制文件(可执行命令)

/var 存放经常变化的文件、如/var/log

/home 普通用户的家目录

/root 特权用户的家目录

/etc 存放配置文件目录、各种运维软件的配置文件

/boot 存放内核和启动文件

/usr 存放安装程序(软件默认目录)

/opt 大型软件存放目录(非强制)

/mnt 文件挂载目录

几个重要的配置文件

/etc/sysconfig/network-scripts 下存放网卡配置文件

/etc/resolv.conf 设置DNS域名解析服务器地址

/etc/hostname 设置主机名称

/etc/hosts

/etc/motd 设置登录后的文字

/etc/os-release

/proc

/var/log

/var/log/messages

2、文件操作命令

ls命令

#参数
-a    显示所有文件,包含隐藏文件
-l    显示所有文件和简要信息
-h    显示文件的字节大小
--full-time    显示详细的时间信息
-t    根据最近的时间进行排序
-F    将文件、文件夹、软链接、可执行文件用符号的方式标注
-d    显示文件夹本身信息
-i    显示文件的id号

#ls [路径]
#路径可选,不写默认当前路径
#显示所有文件
[root@zbx-agent01 ~]# ls
anaconda-ks.cfg           node_exporter-1.3.1.linux-amd64.tar.gz  package.json       zabbix.sh
install.sh                node-v16.14.2-linux-x64.tar.xz          package-lock.json
install_zabbix_v0.9.2.sh  openssl-1.0.2f.tar.gz  

#ls -a [路径]
#路径可选,不写默认当前路径
#显示所有文件(包括隐藏文件)
[root@zbx-agent01 ~]# ls -a
.                .cache                                  .npm                   .rnd
..               .cshrc                                  openssl-1.0.2f.tar.gz  .tcshrc
anaconda-ks.cfg  .halo                                   package.json           .viminfo
.bash_history    install.sh                              package-lock.json      zabbix.sh
.bash_logout     install_zabbix_v0.9.2.sh                .pip
.bash_profile    node_exporter-1.3.1.linux-amd64.tar.gz  .pki
.bashrc          node-v16.14.2-linux-x64.tar.xz          PS.TXT

#ls -l [路径]
#路径可选,不写默认当前路径
#显示所有文件及详细信息
[root@zbx-agent01 ~]# ls -l
总用量 35480
-rw-------. 1 root root     1233 3月   5 18:44 anaconda-ks.cfg
-rw-r--r--. 1 root root    27051 3月  29 15:05 install.sh
-rw-r--r--  1 root root    40908 5月   7 12:16 install_zabbix_v0.9.2.sh
-rw-r--r--  1 root root  9033415 5月   8 14:54 node_exporter-1.3.1.linux-amd64.tar.gz
-rw-r--r--. 1 root root 21941244 3月  20 17:44 node-v16.14.2-linux-x64.tar.xz
-rw-r--r--. 1 root root  5258384 4月   1 20:36 openssl-1.0.2f.tar.gz
-rw-r--r--. 1 root root        3 3月  25 19:37 package.json
-rw-r--r--. 1 root root       83 3月  25 19:37 package-lock.json
-rw-r--r--. 1 root root       21 12月 30 2011 PS.TXT
-rw-r--r--  1 root root     5988 5月   7 11:48 zabbix.sh

#ls -lh [路径]
#路径可选,不写默认当前路径
#显示所有文件及详细信息,显示明确的文件大小
[root@zbx-agent01 ~]# ls -lh
总用量 35M
-rw-------. 1 root root 1.3K 3月   5 18:44 anaconda-ks.cfg
-rw-r--r--. 1 root root  27K 3月  29 15:05 install.sh
-rw-r--r--  1 root root  40K 5月   7 12:16 install_zabbix_v0.9.2.sh
-rw-r--r--  1 root root 8.7M 5月   8 14:54 node_exporter-1.3.1.linux-amd64.tar.gz
-rw-r--r--. 1 root root  21M 3月  20 17:44 node-v16.14.2-linux-x64.tar.xz
-rw-r--r--. 1 root root 5.1M 4月   1 20:36 openssl-1.0.2f.tar.gz
-rw-r--r--. 1 root root    3 3月  25 19:37 package.json
-rw-r--r--. 1 root root   83 3月  25 19:37 package-lock.json
-rw-r--r--. 1 root root   21 12月 30 2011 PS.TXT
-rw-r--r--  1 root root 5.9K 5月   7 11:48 zabbix.sh

#ls -lh --full-time[路径]
#路径可选,不写默认当前路径
#显示所有文件及详细信息,显示明确的文件大小和最近修改时间
[root@zbx-agent01 ~]# ls -lh --full-time
总用量 35M
-rw-------. 1 root root 1.3K 2022-03-05 18:44:48.493000000 +0800 anaconda-ks.cfg
-rw-r--r--. 1 root root  27K 2022-03-29 15:05:42.000000000 +0800 install.sh
-rw-r--r--  1 root root  40K 2022-05-07 12:16:48.511246023 +0800 install_zabbix_v0.9.2.sh
-rw-r--r--  1 root root 8.7M 2022-05-08 14:54:10.000000000 +0800 node_exporter-1.3.1.linux-amd64.tar.gz
-rw-r--r--. 1 root root  21M 2022-03-20 17:44:19.000000000 +0800 node-v16.14.2-linux-x64.tar.xz
-rw-r--r--. 1 root root 5.1M 2022-04-01 20:36:01.000000000 +0800 openssl-1.0.2f.tar.gz
-rw-r--r--. 1 root root    3 2022-03-25 19:37:19.734760587 +0800 package.json
-rw-r--r--. 1 root root   83 2022-03-25 19:37:19.734760587 +0800 package-lock.json
-rw-r--r--. 1 root root   21 2011-12-30 15:45:12.000000000 +0800 PS.TXT
-rw-r--r--  1 root root 5.9K 2022-05-07 11:48:11.000000000 +0800 zabbix.sh

#ls -lt --full-time[路径]
#路径可选,不写默认当前路径
#显示所有文件及详细信息,显示明确的文件大小和最近修改时间
[root@zbx-agent01 ~]# ls -lt --full-time
总用量 35480
-rw-r--r--. 1 root root       21 2022-05-23 16:19:46.118233426 +0800 PS.TXT
-rw-r--r--  1 root root  9033415 2022-05-08 14:54:10.000000000 +0800 node_exporter-1.3.1.linux-amd64.tar.gz
-rw-r--r--  1 root root    40908 2022-05-07 12:16:48.511246023 +0800 install_zabbix_v0.9.2.sh
-rw-r--r--  1 root root     5988 2022-05-07 11:48:11.000000000 +0800 zabbix.sh
-rw-r--r--. 1 root root  5258384 2022-04-01 20:36:01.000000000 +0800 openssl-1.0.2f.tar.gz
-rw-r--r--. 1 root root    27051 2022-03-29 15:05:42.000000000 +0800 install.sh
-rw-r--r--. 1 root root        3 2022-03-25 19:37:19.734760587 +0800 package.json
-rw-r--r--. 1 root root       83 2022-03-25 19:37:19.734760587 +0800 package-lock.json
-rw-r--r--. 1 root root 21941244 2022-03-20 17:44:19.000000000 +0800 node-v16.14.2-linux-x64.tar.xz
-rw-------. 1 root root     1233 2022-03-05 18:44:48.493000000 +0800 anaconda-ks.cfg

mkdir命令

#同时创建多个文件夹
[root@zbx-agent01 test]# mkdir {test1,test2,test3}

#同时删除多个文件夹
[root@zbx-agent01 test]# rmdir {test1,test2,test3}

#递归创建目录
[root@zbx-agent01 test]# mkdir -p test1/test11/test111

#同时创建多个目录
[root@zbx-agent01 test]# mkdir test{1..50}

touch命令

1、创建普通文件

普通文件:权限首字母是-
-rw-r--r-- 1 root root 0 5月  24 15:27 test

2、对于已存在的文件会修改访问时间为当前时间

#创建新普通文件test
[root@zbx-agent01 test]# touch test

#修改test文件的时间
[root@zbx-agent01 test]# touch -t 202205220606 test

cp命令

用法: cp [选项] 源文件(可多个) 目标路径或目标路径加新文件名

[root@zbx-agent01 test]# touch test
[root@zbx-agent01 test]# cp test test1
[root@zbx-agent01 test]# ls
test  test1
[root@zbx-agent01 test]# mkdir pkg
[root@zbx-agent01 test]# ls
pkg  test  test1
[root@zbx-agent01 test]# cp test ./pkg/
[root@zbx-agent01 test]# ls ./pkg/
test

#递归式复制文件可以复制整个文件夹 cp -r
[root@zbx-agent01 test]# cp -r pkg pkg2

#通过cp命令拷贝后的文件内容是一样的,但是属性不一样,可以通过-t参数保持一致
[root@zbx-agent01 test]# cp test test2
[root@zbx-agent01 test]# ls -l
总用量 0
drwxr-xr-x 2 root root 18 5月  24 15:32 pkg
drwxr-xr-x 2 root root 18 5月  24 15:37 pkg2
-rw-r--r-- 1 root root  0 5月  24 15:31 test
-rw-r--r-- 1 root root  0 5月  24 15:31 test1
-rw-r--r-- 1 root root  0 5月  24 15:41 test2

#拷贝软链接 cp -d
[root@zbx-agent01 test]# cp -d test-link test-link2

#拷贝覆盖提示 cp -i
#但是直接用cp命令也会提示,这是因为linux系统中创建了别名
[root@zbx-agent01 test]# 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'

mv命令

1、 mv [选项] 源文件(可多个) 目标路径或目标路径加文件名

2、改名

3、mv也有别名mv -i,覆盖前询问

4、强制覆盖已有的文件,加上-f参数

rm命令

1、rm 文件名,默认执行rm -i

2、一次性删除多个文件

3、删除文件夹,用rm -r命令,递归删除文件夹及文件夹下的内容

4、如果文件夹为空,用rm -d命令

5、强制删除文件,用rm -f命令

6、强制删除文件及文件夹,用rm -rf命令,千万注意不要误删!

#强制删除当前目录下的所有文件及文件夹
[root@zbx-agent01 test]# rm -rf ./*
.    当前目录
/    根目录
./    当前目录下的某个文件

帮助命令

1、man 命令

2、命令 --help

3、info 命令

重启命令

1、shutdown -r 0 立即重启

2、shutdown -r 10 过10分钟后重启

3、shutdown -r now 立即重启

4、reboot

关机命令

1、shutdown -h 0 立即关机

2、shutdown -h 10 过10分钟后关机

3、shutdown -h now 立即关机

常用快捷键

ctrl+c 取消当前操作

ctrl+l 清屏

ctrl+d 退出当前用户

ctrl+a 光标移到行首

ctrl+e 光标移到行尾

ctrl+u 删除光标到行首的内容

linux环境变量

[root@zbx-agent01 test]# echo $PATH
/usr/node_modules/hexo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/git/bin:/root/bin

当输入命令后,系统会从环境变量中设置的目录中查找

#查找ls命令的位置
[root@zbx-agent01 test]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls

vi命令

命令行模式下快捷键

w 上一个单词

b 下一个单词

0 行首

$ 行尾

g 文章第一行

G 文章最后一行

H 屏幕的第一行

L 屏幕的最后一行

M 屏幕的中间一行

命令行模式下输入/ 可进行自上而下查找

命令行模式下输入?可进行自下而上查找

命令行模式下输入yy 可复制当前行,输入p进行黏贴

命令行模式下输入数字+yy 可复制当前行开始的多行,输入p进行黏贴

命令行模式下输入dd进行整行删除

命令行模式下输入D删除当前行光标后的内容

命令行模式下输入x删除当前光标后的一个字母

命令行模式下输入X删除当前光标前的一个字母

命令行模式下输入u撤销操作

命令行模式下输入C删除当前行光标后的内容,并进入编辑模式

交换文件

cat命令

显示文件内容

[root@zbx-agent01 ~]# cat hello2
helloworld!
totong helloworld!
#显示行号
[root@zbx-agent01 ~]# cat hello2 -n
     1  helloworld!
     2  totong helloworld!

more和less命令

分屏显示文件内容

1、按下enter下一行

2、按下空格滚到到下一屏

3、按下=显示当前行号

4、按下q退出

head和tail命令

head命令显示文件的前n行

[root@zbx-agent01 ~]# head -5 hello
helloworld!
fefefef
fefefe
fefef
e
#如果不加-n参数,将默认显示10行

head -c n 文件名 将按字节数输出

tail命令显示文件的后n行

[root@zbx-agent01 ~]# tail -5 hello
3
3
3
33
4
#如果不加-n参数,将默认显示10行

tail -f 文件名:实时监测文件内容

tail -F 文件名:实时监测文件内容,文件可以是还没有创建的文件

重定向符号

用法1:读取文件中的内容写入到另一个文件中去

#使用“>”重定向符号将内容“覆盖”到新的文件中去
[root@zbx-agent01 ~]# cat hello
helloworld!
[root@zbx-agent01 ~]# cat hello > ./hello2
[root@zbx-agent01 ~]# ls
anaconda-ks.cfg  install.sh                              node-v16.14.2-linux-x64.tar.xz  package-lock.json  zabbix.sh
hello            install_zabbix_v0.9.2.sh                openssl-1.0.2f.tar.gz           PS.TXT
hello2           node_exporter-1.3.1.linux-amd64.tar.gz  package.json                    test
[root@zbx-agent01 ~]# cat hello2
helloworld!

#使用“>>”重定向符号将内容“追加”到新的文件中去
[root@zbx-agent01 ~]# cat hello >> ./hello2
[root@zbx-agent01 ~]# cat hello2
helloworld!
helloworld!

#“<”的使用
#“<<”的使用

管道符号

利用管道符号“|”将左侧命令的的输出内容作为右侧命令的输入,实现过滤的作用

[root@zbx-agent01 ~]# cat hello | grep hello
helloworld!

3、网络操作命令

mii-tool命令

[root@zbx-agent01 ~]# mii-tool
No interface specified
usage: mii-tool [-VvRrwl] [-A media,... | -F media] [-p addr] <interface ...>
       -V, --version               display version information
       -v, --verbose               more verbose output
       -R, --reset                 reset MII to poweron state
       -r, --restart               restart autonegotiation
       -w, --watch                 monitor for link status changes
       -l, --log                   with -w, write events to syslog
       -A, --advertise=media,...   advertise only specified media
       -F, --force=media           force specified media technology
       -p, --phy=addr              set PHY (MII address) to report
media: 1000baseTx-HD, 1000baseTx-FD,
       100baseT4, 100baseTx-FD, 100baseTx-HD,
       10baseT-FD, 10baseT-HD,
       (to advertise both HD and FD) 1000baseTx, 100baseTx, 10baseT

ethtool命令

[root@zbx-agent01 ~]# ethtool enp0s3
Settings for enp0s3:
        Supported ports: [ TP ]
        Supported link modes:   10baseT/Half 10baseT/Full 
                                100baseT/Half 100baseT/Full 
                                1000baseT/Full 
        Supported pause frame use: No
        Supports auto-negotiation: Yes
        Supported FEC modes: Not reported
        Advertised link modes:  10baseT/Half 10baseT/Full 
                                100baseT/Half 100baseT/Full 
                                1000baseT/Full 
        Advertised pause frame use: No
        Advertised auto-negotiation: Yes
        Advertised FEC modes: Not reported
        Speed: 1000Mb/s
        Duplex: Full
        Port: Twisted Pair
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: on
        MDI-X: off (auto)
        Supports Wake-on: umbg
        Wake-on: d
        Current message level: 0x00000007 (7)
                               drv probe link
        Link detected: yes

查看MAC地址

[root@zbx-agent01 ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 08:00:27:39:4c:b5 brd ff:ff:ff:ff:ff:ff
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 02:42:56:93:97:6c brd ff:ff:ff:ff:ff:ff
4: br-46c51a28061c: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default 
    link/ether 02:42:31:82:af:06 brd ff:ff:ff:ff:ff:ff
[root@zbx-agent01 ~]# ifconfig
br-46c51a28061c: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.0.1  netmask 255.255.0.0  broadcast 192.168.255.255
        ether 02:42:31:82:af:06  txqueuelen 0  (Ethernet)
        RX packets 14  bytes 1232 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1232 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:56:93:97:6c  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.17  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe39:4cb5  prefixlen 64  scopeid 0x20<link>
        inet6 2409:8a1e:6e77:c6b0:a00:27ff:fe39:4cb5  prefixlen 64  scopeid 0x0<global>
        ether 08:00:27:39:4c:b5  txqueuelen 1000  (Ethernet)
        RX packets 34623  bytes 40400808 (38.5 MiB)
        RX errors 0  dropped 53  overruns 0  frame 0
        TX packets 9087  bytes 837273 (817.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 14  bytes 1232 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1232 (1.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ARP缓存

[root@zbx-agent01 ~]# arp -a
? (192.168.2.1) at f8:2f:65:b4:0c:32 [ether] on enp0s3
? (192.168.1.1) at ac:5a:ee:b4:b4:98 [ether] on enp0s3
? (192.168.1.15) at <incomplete> on enp0s3
? (192.168.1.12) at 00:21:6b:e7:c4:26 [ether] on enp0s3

[root@zbx-agent01 ~]# ip neigh
192.168.2.1 dev enp0s3 lladdr f8:2f:65:b4:0c:32 STALE
192.168.1.1 dev enp0s3 lladdr ac:5a:ee:b4:b4:98 STALE
192.168.1.15 dev enp0s3  FAILED
192.168.1.12 dev enp0s3 lladdr 00:21:6b:e7:c4:26 DELAY
fe80::1 dev enp0s3 lladdr ac:5a:ee:b4:b4:98 router STALE

[root@zbx-agent01 ~]# arp -d 192.168.1.15
[root@zbx-agent01 ~]# arp -s IP地址 MAC地址
#ARP欺骗,如果局域网中的主机中毒,大量发送ARP欺骗广播包,将自己伪造成为网关,那么局域网的其他机器就认为中毒的机器是网关,导致上不了网
#解决1:arp -s 静态绑定
#解决2:arp防火墙
#解决3:清除中毒的机器

4、ctl相关命令

hostnamectl

[root@zbx-agent01 ~]# hostnamectl
   Static hostname: zbx-agent01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 8c0def24413e47a3b41538e6690ead57
           Boot ID: 89675d91af39408cb099dcfce9428e7e
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64
[root@zbx-agent01 ~]# hostname
zbx-agent01
[root@zbx-agent01 ~]# uname -n
zbx-agent01
#主机名文件位置/etc/hostname
[root@zbx-agent01 ~]# cat /etc/hostname
zbx-agent01

#修改主机名
1、直接修改文件
2、hostnamectl set-hostname 新名字

localectl

[root@zbx-agent01 ~]# localectl
   System Locale: LANG=zh_CN.UTF-8
       VC Keymap: cn
      X11 Layout: cn
[root@zbx-agent01 ~]# cat /etc/locale.conf
LANG="zh_CN.UTF-8"
最后修改:2022 年 06 月 01 日
如果觉得我的文章对你有用,请随意赞赏