vi/vim文本编辑器

  • Vim是从 vi 发展出来的一个文本编辑器,vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正确性
  • vi/vim 共分为三种模式:命令模式、输入模式、底线命令模式(末行模式)
    • 命令模式:刚刚启动 vi/vim,便进入了命令模式
    • 输入模式:在命令模式下按 a/i/o 就进入了输入模式
    • ESC,退出输入模式,切换到命令模式
    • 底线命令模式:在命令模式下按下:(英文冒号)就进入了底线命令模式
  • 命令格式:vim 文件名
    • 若目标文件不存在,则新创建文件并编辑
    • 若目标文件以存在,则打开文件并编辑
  • 命令模式:刚刚启动 vi/vim,便进入了命令模式
    • i 切换到输入模式,在当前光标所在字符前插入
    • a 切换到输入模式,在当前光标所在字符后插入
    • o 切换到输入模式,在当前光标所在行下插入新行
    • : 切换到底线命令模式,以在最底一行输入命令
    • x 在命令模式下删除当前光标所在的单字符
    • dd 删除一整行内容,配合数字可删除指定范围内的行
    • C 删除当前光标及光标后所有内容并进入输入模式
    • u 恢复上一次修改内容,一次恢复一个操作,可多次恢复,直到恢复本次操作初始状态为止
    • $ 将光标移动至行尾
    • 0(零) 将光标移动至行首
    • gg 跳转至文件第一行
    • G 跳转至文件最后一行
    • yy 复制当前行,配合数字可以同时复制多行
    • p 粘贴当前光标所在行下
    • /关键字 搜索文件内关键字,n从上向下快速定位关键字,N从下向上快速定位关键字
  • 底线命令模式可以输入单个或多个字符的命令,可用的命令非常多。
    • :w 保存
    • :q 退出
    • :wq 保存并退出
    • :q! 强制退出不保存
    • :wq! 强制保存并退出
    • :set nu 以行号形式显示文件内容
    • :set nonu 取消行号显示
    • :行号 快速跳转到指定行
    • :r 读入另一个文件的数据 , 文件内容填加到光标的下一行
  1. [root@test ~]# vim /etc/services

修改网卡IP地址

  • 网卡配置文件地址: /etc/sysconfig/network-scripts/网卡名
  • ifconfig #用于显示和设置网卡的参数
  • systemctl restart network #重启网络
  • ifup 网卡名 #启动该网卡设备
  • ifdown 网卡名 #禁用该网卡设备
  1. #修改IP地址
  2. [root@test ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
  3. TYPE="Ethernet"
  4. PROXY_METHOD="none"
  5. BROWSER_ONLY="no"
  6. BOOTPROTO="none"
  7. DEFROUTE="yes"
  8. IPV4_FAILURE_FATAL="no"
  9. IPV6INIT="yes"
  10. IPV6_AUTOCONF="yes"
  11. IPV6_DEFROUTE="yes"
  12. IPV6_FAILURE_FATAL="no"
  13. IPV6_ADDR_GEN_MODE="stable-privacy"
  14. NAME="ens32"
  15. UUID="16085f4c-f690-4058-b29e-d55c73387026"
  16. DEVICE="ens32"
  17. ONBOOT="yes"
  18. IPADDR="192.168.0.60" #修改IP地址
  19. PREFIX="24"
  20. GATEWAY="192.168.0.254"
  21. DNS1="114.114.114.114"
  22. IPV6_PRIVACY="no"
  23. ~
  24. #重启网络(IP地址发生改变,当前终端会断开)
  25. [root@test ~]# systemctl restart network
  26. [c:\~]$ ssh 192.168.0.60
  27. #启动该网卡
  28. [root@test ~]# ifup ens32
  29. #查看所有网卡信息
  30. [root@test ~]# ip a

使用命令修改网卡IP地址

nmcli connection modify 网卡名 ipv4.method manual ipv4.addresses Ip地址/掩码 connection.autoconnect yes

解释:

nmcli connection modify(修改)

网卡名 ipv4.method(配置ipv4地址方法)

manual (手动配置)

ipv4.addresses(ipv4地址)

Ip地址/掩码 connection.autoconnect yes(开机自动连接)

  • 激活网卡:nmcli connection up 网卡名
  • 关闭网卡:nmcli connection down 网卡名
  • 重启网卡:nmcli connection reload 网卡名
  1. #使用命令修改网卡IPV地址
  2. [root@test ~]# nmcli connection modify ens32 ipv4.method manual ipv4.addresses 192.168.0.50/24 connection.autoconnect yes
  3. #激活网卡
  4. [root@test ~]# nmcli connection up ens32
  5. [c:\~]$ ssh 192.168.0.50

host命令

  • host用于将一个域名解析到一个IP地址
  1. [root@test ~]# host www.baidu.com
  2. www.baidu.com has address 110.242.68.3
  3. www.baidu.com has address 110.242.68.4
  4. www.baidu.com is an alias for www.a.shifen.com.
  5. www.baidu.com is an alias for www.a.shifen.com.

nslookup命令

  • nslookup用于查询域名解析是否正常,在网络故障时用来诊断网络问题
  1. [root@test ~]# nslookup www.baidu.com
  2. Server: 114.114.114.114
  3. Address: 114.114.114.114#53
  4. Non-authoritative answer:
  5. Name: www.baidu.com
  6. Address: 110.242.68.4
  7. Name: www.baidu.com
  8. Address: 110.242.68.3

alias别名管理命令

  • alias命令用于设置命令别名,用户可以使用alias自定义命令别名来简化命令的复杂度
  • .bashrc 文件存放命令别名
  • 命令格式:aliasi [别名]=[命令] #注意事项:等号(=)前后不能有空格
  • unalias 别名 #取消别名
  1. #定义别名
  2. [root@test ~]# alias lsnet='ls /etc/sysconfig/network-scripts/'
  3. [root@test ~]# lsnet
  4. [root@test ~]# alias myls='ls -ldh'
  5. [root@test ~]# myls /opt
  6. #查看当前系统可用命令别名
  7. [root@test ~]# alias
  8. alias cp='cp -i'
  9. alias egrep='egrep --color=auto'
  10. alias fgrep='fgrep --color=auto'
  11. alias grep='grep --color=auto'
  12. alias l.='ls -d .* --color=auto'
  13. alias ll='ls -l --color=auto'
  14. alias ls='ls --color=auto'
  15. alias lsnet='ls /etc/sysconfig/network-scripts/'
  16. alias mv='mv -i'
  17. alias myls='ls -ldh'
  18. alias rm='rm -i'
  19. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  20. #两条命令效果相同
  21. [root@test ~]# ls -l hello
  22. -rw-r--r--. 1 root root 426 3 28 15:00 hello
  23. [root@test ~]# ll hello
  24. -rw-r--r--. 1 root root 426 3 28 15:00 hello
  25. [root@test ~]# which ls
  26. alias ls='ls --color=auto'
  27. /usr/sbin/ls
  28. [root@test ~]# /usr/sbin/ls
  29. [root@test ~]# ls
  30. #取消本次命令的别名功能“\”
  31. [root@test ~]# \ls
  32. #取消命令别名
  33. [root@test ~]# unalias myls
  34. [root@test ~]# myls
  35. bash: myls: 未找到命令...
  36. #定义别名不要跟系统命令发生冲突
  37. [root@test ~]# alias ls=hostname
  38. [root@test ~]# ls
  39. test
  40. #取消命令别名
  41. [root@test ~]# unalias ls
  42. [root@test ~]# alias
  43. #重新定义别名
  44. [root@test ~]# alias ls='ls --color=auto'
  45. [root@test ~]# ls

history 管理历史命令

  • history命令用于显示历史记录和执行过的命令,登录shell时会读取~./bash_history历史文件中记录下的命令,当退出或登出shell时,会自动保存到历史命令文件,该命令单独使用时,仅显示历史命令
  • 历史命令默认只能存储1000条,可以通过/etc/profile文件修改
  • 命令格式:history [-选项] [参数]
  • 常用选项:
    • -a 追加本次新执行的命令至历史命令文件中
    • -d 删除历史命令中指定的命令
    • -c 清空历史命令列表
  • 快捷操作:
    • !# 调用命令历史中第N条命令
    • !string 调用命令历史中以strind开头的命令
    • !! 重复执行上一条命令
  1. #获取命令帮助
  2. [root@test ~]# help history
  3. #查看历史命令
  4. [root@test ~]# history
  5. #查看记录历史命令文件
  6. [root@test ~]# cat .bash_history
  7. #将历史命令同步至历史命令配置文件中
  8. [root@test ~]# history -a
  9. [root@test ~]# cat .bash_history
  10. #删除历史命令中655条命令历史
  11. [root@test ~]# history -d 655
  12. [root@test ~]# history -d 637
  13. #清空缓存中所有历史命令
  14. [root@test ~]# history -c
  15. [root@test ~]# history
  16. 1 history
  17. #删除历史命令配置文件(该文件删除后系统会再次自动创建)
  18. [root@test ~]# rm -rf .bash_history
  19. #快速调用历史命令中第1条
  20. [root@test ~]# !1
  21. [root@test ~]# !3
  22. #调用历史命令中以cat开头的命令(只调用最近使用的cat历史命令)
  23. [root@test ~]# !cat
  24. #重复执行上一条命令
  25. [root@test ~]# !!
  26. #历史命令默认只能记录1000条,可以通过/etc/profile文件修改
  27. [root@test ~]# vim /etc/profile
  28. ...
  29. 46 HISTSIZE=100

date日期时间管理命令用

  • date命令用于显示或设置系统日期与时间
  • 命令格式:date [-选项] [+格式符] #查看系统日期时间
  • date [-选项] [MMDDhhmm[[CC]YY][.ss]] #设置日期时间
  • 常用选项:-s 设置日期时间
  • 格式符:
    • +%Y 年份
    • +%B 月份
    • +%d 日
    • +%H 时
    • +%M 分
    • +%S 秒
    • +%F 年-月-日
    • +%X 时:分:秒
    • 安装网络时间:yum install -y ntpdate,,,,,,,,ntpdate time.nist.gov
  1. #显示系统日期与时间
  2. [root@test ~]# date
  3. 2021 03 28 星期日 17:08:34 CST
  4. #只显示年分
  5. [root@test ~]# date +%Y
  6. 2021
  7. #只显示月份
  8. [root@test ~]# date +%B
  9. 三月
  10. #只显示几号
  11. [root@test ~]# date +%d
  12. 28
  13. #只显示小时
  14. [root@test ~]# date +%H
  15. 17
  16. #只显示分钟
  17. [root@test ~]# date +%M
  18. 10
  19. #只显示秒
  20. [root@test ~]# date +%S
  21. 24
  22. #显示年月日
  23. [root@test ~]# date +%F
  24. 2021-03-28
  25. #显示时分秒
  26. [root@test ~]# date +%X
  27. 171210
  28. #显示年月日时分秒
  29. [root@test ~]# date +%F%X
  30. 2021-03-28171239
  31. #可以自定义分隔符“-”
  32. [root@test ~]# date +%F-%X
  33. 2021-03-28-171338
  34. [root@test ~]# date +%F:%X
  35. 2021-03-28:171355
  36. #修改系统年月日
  37. [root@test ~]# date -s 2020-03-28
  38. 2020 03 28 星期六 00:00:00 CST
  39. #修改系统时分秒
  40. [root@test ~]# date -s 17:16:00
  41. 2020 03 28 星期六 17:16:00 CST
  42. #修改年月日时分秒
  43. [root@test ~]# date -s '2021-03-28 17:17:00'
  44. 2021 03 28 星期日 17:17:00 CST
  45. #解释:
  46. ''单引号:引用整体,屏蔽特殊符号的功能
  47. ""双引号:引用整体,不会屏蔽特殊符号的功能
  48. #Linux的两种时钟
  49. 系统时钟:内核通过CPU的工作频率去计算的时间
  50. 硬件时钟:
  51. #显示硬件时间
  52. [root@test ~]# clock
  53. 20210328 星期日 172342 -0.945549
  54. #显示并同步系统与硬件时钟
  55. [root@test ~]# man hwclock
  56. -s:把系统时间设置成与硬件时间相同
  57. -w:把硬件时间设置成与系统时间相同
  58. [root@test ~]# hwclock -w
  59. [root@test ~]# date
  60. 2021 03 28 星期日 17:27:18 CST
  61. #cal显示日历
  62. [root@test ~]# cal
  63. 三月 2021
  64. 1 2 3 4 5 6
  65. 7 8 9 10 11 12 13
  66. 14 15 16 17 18 19 20
  67. 21 22 23 24 25 26 27
  68. 28 29 30 31
  69. #显示指定的全年月份
  70. [root@test ~]# cal 2021

wc统计命令

  • wc 用于统计文件的字节数、行数,并将统计的结果输出到屏幕
  • 命令格式:wc [-选项] 文件名
  • 常用选项:
    • -c #统计字节数
    • -l #统计行数
  1. [root@test ~]# wc /etc/passwd
  2. 43 87 2259 /etc/passwd
  3. 行数 单词 字节 文件名
  4. #统计文件字节数
  5. [root@test ~]# wc -c /etc/passwd
  6. 2259 /etc/passwd
  7. #统计文件行数
  8. [root@test ~]# wc -l /etc/passwd
  9. 43 /etc/passwd
  10. [root@test ~]# wc -l /etc/fstab
  11. 11 /etc/fstab

管道符

  • 管道符“|”:将命令的输出结果交给另外一条命令作为参数继续处理
  1. [root@test ~]# head -10 /etc/passwd |tail -5
  2. [root@test ~]# head -10 /etc/passwd |tail -5 |wc -l
  3. 5
  4. root@test ~]# cat -n /etc/passwd |head -10|tail -5
  5. 6 sync:x:5:0:sync:/sbin:/bin/sync
  6. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
  7. 8 halt:x:7:0:halt:/sbin:/sbin/halt
  8. 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
  9. 10 operator:x:11:0:operator:/root:/sbin/nologin
  10. [root@test ~]# ifconfig ens32 |head -2
  11. ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  12. inet 192.168.0.50 netmask 255.255.255.0 broadcast 192.168.0.255

重定向操作

  • 重定向操作:将前面命令的输出结果,写入到其他的文本文件中
  • 重定向的表示符号
    • > #重定向输出(覆盖)
    • >> #重定向输出(追加)
    • < #输入重定向(覆盖)
    • << #输入重定向(追加)
    • > 只收集正确的输出结果
    • 2> 只收集错误的输出结果
    • &> 正确错误都收集
  1. #将命令的输出结果以覆盖的方式重定向到文件中,(>附带创建文件功能)
  2. [root@test ~]# ifconfig ens32 |head -2 > /opt/ens32.bak
  3. ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  4. inet 192.168.0.50 netmask 255.255.255.0 broadcast 192.168.0.255
  5. [root@test ~]# cat /etc/hostname > /opt/ens32.bak
  6. [root@test ~]# cat /opt/ens32.bak
  7. test
  8. [root@test ~]# free -h > /opt/free.bak
  9. [root@test ~]# cat /opt/free.bak
  10. total used free shared buff/cache available
  11. Mem: 972M 414M 123M 15M 435M 336M
  12. Swap: 2.0G 0B 2.0G
  13. #将命令的输出结果以追加的方式重定向到文件中
  14. [root@test ~]# cat /etc/hostname >> /opt/free.bak
  15. [root@test ~]# cat /opt/free.bak
  16. #“>”只收集正确的输出结果,不收集错误的输出结果
  17. [root@test ~]# ls xxooooxx > /opt/xx.txt
  18. ls: 无法访问xxooooxx: 没有那个文件或目录
  19. #“2>”只收集错误的输出结果,不收集正确的输出结果
  20. [root@test ~]# ls xxooooxx 2> /opt/xx.txt
  21. [root@test ~]# cat /opt/xx.txt
  22. ls: 无法访问xxooooxx: 没有那个文件或目录
  23. #“2>”以覆盖的方式将输出结果重定向到文件中
  24. [root@test ~]# cat /etc/abc 2> /opt/ens32.bak
  25. [root@test ~]# cat /opt/ens32.bak
  26. cat: /etc/abc: 没有那个文件或目录
  27. #“2>>”以追加的方式将输出结果重定向到文件中
  28. [root@test ~]# ls /etc/abcd 2>> /opt/ens32.bak
  29. [root@test ~]# cat /opt/ens32.bak
  30. cat: /etc/abc: 没有那个文件或目录
  31. ls: 无法访问/etc/abcd: 没有那个文件或目录
  32. #“&>”以覆盖的方式将正确输出与错误输出重定向到文件中
  33. [root@test ~]# lscat &> /opt/abc.txt
  34. [root@test ~]# cat /opt/abc.txt
  35. [root@test ~]# ls /etc/passwd &> /opt/pass.bak
  36. [root@test ~]# cat /opt/pass.bak
  37. [root@test ~]# free -h &> /opt/pass.bak
  38. [root@test ~]# cat /opt/pass.bak
  39. #“&>”以追加的方式将正确输出与错误输出重定向到文件中
  40. [root@test ~]# ifconfig ens32 | head -2 &>> /opt/pass.bak
  41. [root@test ~]# cat /opt/pass.bak
  42. #以覆盖方式将正确输出与错误输出重定向到不同文件中
  43. [root@test ~]# ll -d /root/ bcd >a.txt 2>b.txt
  44. [root@test ~]# cat a.txt
  45. dr-xr-x---. 24 root root 4096 3 28 18:07 /root/
  46. [root@test ~]# cat b.txt
  47. ls: 无法访问bcd: 没有那个文件或目录

echo命令与sleep命令

  • echo命令用于输出指定的字符串和变量
  • 命令格式:echo [-选项] [参数]
  1. [root@test ~]# echo $PATH
  2. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  3. [root@test ~]# echo xxoo
  4. xxoo
  5. [root@test ~]# echo abc
  6. abc
  7. [root@test ~]# echo 男人好难
  8. 男人好难
  9. [root@test ~]# echo 123
  10. 123
  11. [root@test ~]# cat /etc/hostname
  12. test
  13. [root@test ~]# echo localhost > /etc/hostname
  14. [root@test ~]# cat /etc/hostname
  15. localhost
  • sleep命令可以用来将目前动作延迟一段时间
  • 命令格式:sleep 时间
  • 常用选项: s 秒 m 分钟 h 小时 d 日
  1. [root@test ~]# sleep 3