目录
1、常用命令回顾
1.1设置主机名
命令格式:
hostnamectl set-hostname 主机名称
示例:将默认的默认的主机名称(localhost)修改为 openEuler
#修改主机名称
hostnamectl set-hostname openEuler
#查看主机名称
hostnamectl hostname
#查看保存主机名的文件内容
cat /etc/hostname
1.2 nmcli 命令
修改虚拟机的IP地址
格式:
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
示例:将虚拟机的IP地址修改为150
步骤如下:
1)查看网卡名称
#几种方法如下
ifconfig
ip ad
inconfig ens33
2)设置IP地址为150
nmcli c modify ens33 ipv4.address 192.168.211.150/24
nmcli c modify ens33 ipv4.method manual
nmcli c modify ens33 ipv4.dns 114.114.114.114
nmcli c modify ens33 ipv4.gateway 192.168.211.2(网关)
nmcli c modify ens33 autoconnect yes
nmcli up ens33
如果要添加一个新的IP而不修改原来的,则执行以下命令:
nmcli c modify ens33 +ipv4.addresses 192.168.211.151/24
nmcli c up ens33
1.3 Iscpu
作用
显示虚拟机的CPU详细信息
1.4 重定向
在重定向中涉及到以下三个概述:
标准输入,它是用数字0表示,用于接受从终端输入的内容
标准输出,它是用数字1表示,用于将内存或文件中的内容输出到终端或文件
标准错误输出,它是用数字2表示,将执行命令所产生的错误信息进行输出,可以输入到终端文件
可能会用到的方式:
2>&1,表示:将标准输出和标准错误输出到同一个设备上
/dev/null 在Linux中表示一个黑洞
1.4.1 >
将内容输入到指定文件中,并且是以覆盖的方式来写人内容
[root@openEuler ~]# cat /etc/passwd | head -5
root:x:0:0:Super User:/root:/bin/bash
bin:x:1:1:bin:/bin:/usr/sbin/nologin
daemon:x:2:2:daemon:/sbin:/usr/sbin/nologin
adm:x:3:4:adm:/var/adm:/usr/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/sbin/nologin
[root@openEuler ~]# cat /etc/passwd | head -5 > passwd
[root@openEuler ~]# ls
anaconda-ks.cfg hehe.txt message.txt passwd pcqq test.txt
[root@openEuler ~]# cat passwd
root:x:0:0:Super User:/root:/bin/bash
bin:x:1:1:bin:/bin:/usr/sbin/nologin
daemon:x:2:2:daemon:/sbin:/usr/sbin/nologin
adm:x:3:4:adm:/var/adm:/usr/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/usr/sbin/nologin
[root@openEuler ~]# cat /etc/passwd | tail -5 > passwd
[root@openEuler ~]# cat passwd
cockpit-ws:x:988:985:User for cockpit web service:/nonexisting:/sbin/nologin
cockpit-wsinstance:x:987:984:User for cockpit-ws instances:/nonexisting:/sbin/nologin
chrony:x:986:983::/var/lib/chrony:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
[root@openEuler ~]#
1.4.2 >>
表示向文件的末尾追加指定的内容
[root@openEuler ~]# echo haha >> hehe.txt
[root@openEuler ~]# cat hehe.txt
hello world
haha
[root@openEuler ~]# ls >> hehe.txt
[root@openEuler ~]# cat hehe.txt
hello world
haha
anaconda-ks.cfg
hehe.txt
passwd
1.4.3 <
表示输入重定向
主要功能
将一个文件的内容作为命令的输入,使得我们能够将文件中的数据传递给某个命令,从而让命令以文件内容作为输入进行操作
格式
command < input_file
示例1:
假如有一个文件名为 hehe.txt 的文件,里面存储了一些文本数据,这时我们就可以使用 < 符号将文件的内容传递给 cat 命令,以查看文件的内容
[root@openEuler ~]# cat hehe.txt
hello world
haha
anaconda-ks.cfg
hehe.txt
passwd
[root@openEuler ~]# cat < hehe.txt
hello world
haha
anaconda-ks.cfg
hehe.txt
passwd
示例2:
统计行数
使用 < 将文件传递给 wc 命令,以便统计文件的行数、字数已经字符数
[root@openEuler ~]# wc -l hehe.txt
5 hehe.txt
[root@openEuler ~]# wc -l < hehe.txt
5
1.4.4 <<
将多行文本的内容输入给某个命令
格式
command << EOF
内容1
内容2
内容3
EOF
示例1:
[root@openEuler ~]# cat << EOF
> hello
> world
> good
> openEuler
> EOF
hello
world
good
openEuler
注意:
在上述的示例中,EOF 是自定义的分隔符,可以用引号将分隔符放在里面
示例2:
[root@openEuler ~]# cat << 'EOF'
> redhat
> openlab
> linux
> EOF
redhat
openlab
linux
如果希望将多行文本的内容输入到指定的文件中,则可以如下实现:
[root@openEuler ~]# cat << EOF > message.txt
> one
> two
> three
> EOF
[root@openEuler ~]# ll
total 16
-rw-------. 1 root root 881 Mar 16 20:10 anaconda-ks.cfg
-rw-r--r--. 1 root root 49 Mar 16 21:07 hehe.txt
-rw-r--r--. 1 root root 14 Mar 17 09:50 message.txt
-rw-r--r--. 1 root root 183 Mar 16 21:03 passwd
[root@openEuler ~]# cat message.txt
one
two
three
[root@openEuler ~]# cat << EOF >> message.txt
> hehe.txt
> message.txt
> EOF
[root@openEuler ~]# cat message.txt
one
two
three
hehe.txt
message.txt
1.4.5 <<<
将单行文件内容输入到某个命令中
格式
command <<< '单行文本内容'
示例:
[root@openEuler ~]# cat <<< 'this is text'
this is text
1.5 wget
用于下载网络资源
使用语法
Usage: wget [OPTION]... [URL]...
常用选项:
-V:查看wget的版本
-b:后台下载
-q:静默下载
-v:显示下载信息
-F:以HTML格式输入文件
-t,--tries=NUMBER:下载的重试次数
-O,--output-document=FILE:将下载的内容写入到文件中
-c,--continue:支持断点续传
-T, --timeout=SECONDS:下载的超时时间
-w, --wait=SECONDS:等待时间
--limit-rate=RATE:限制下载速度
示例1:
#查看版本
[root@openEuler ~]# wget -V
GNU Wget 1.21.4 built on linux-gnu.
示例2:
# 下载文件
[root@openEuler ~]# wget https://pm.myapp.com/invc/xfspeed/qqpcmgr/download/QQPCDownload320059.exe
示例3:
# 限制下载速度
[root@openEuler ~]# wget https://pm.myapp.com/invc/xfspeed/qqpcmgr/download/QQPCDownload320059.exe
--limit-rate=10K
示例4:
# 下载后指定文件名称
[root@openEuler ~]# wget https://pm.myapp.com/invc/xfspeed/qqpcmgr/download/QQPCDownload320059.exe -O qq.exe
示例5:
# 后台下载
[root@openEuler ~]# wget https://pm.myapp.com/invc/xfspeed/qqpcmgr/download/QQPCDownload320059.exe -O qq2.exe -b
1.6 watch
周期性的用来执行某命令,并且把某命令执行结果输出到屏幕
使用 watch 命令,可以周期性的监测并且输出某命令的执行结果到屏幕,提高工作效率
常用参数
参数 | 说明 |
-h | watch 帮助文档 |
-n | 设置 watch 时间间隔,默认为2s |
-d | 高亮显示变化的区域 |
-t | 不显示顶部的时间间隔信息 |
示例:
# 隔 4s 执行一次 ls 命令
[root@openEuler ~]# watch -n 4 ls
1.7 xargs
给其他命令传递参数的一个过滤器
也是组合多个命令的一个工具
擅长将标准输入的数据转换成命令行参数,xargs能处理管道或者 stdin 并且将其转换成待定命令的命令参数
格式
xargs [选项] [命令]
选项
-I | 用于指定替换字符串,将输入数据中的特定字符串替换为命令行参数 |
-n | 用于指定每次执行命令的参数个数 |
-t | 用于打印执行的命令 |
-p | 用于提示用户确认是否执行命令 |
-r | 当标准输入为空时,不执行命令 |
示例:
# 准备数据
[root@openEuler ~]# cat << EOF > test.txt
> a b c d e f g
> h i j k l m n
> o p q
> r s t
> u v w x y z
> EOF
[root@openEuler ~]#
[root@openEuler ~]# cat test.txt
a b c d e f g
h i j k l m n
o p q
r s t
u v w x y z
多行输入单行输出:
[root@openEuler ~]# cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z
使用 -n 进行多行输出, -n 选项多行输出:
[root@openEuler ~]# cat test.txt | xargs -n3
a b c
d e f
g h i
j k l
m n o
p q r
s t u
v w x
y z
使用 -d 分割输入
-d 可以自定义一个定界符:
[root@openEuler ~]# echo "nameXnameXnameXname" | xargs -dX
name name name name
结合 -n 选项使用:
[root@openEuler ~]# echo "nameXnameXnameXname" | xargs -dX -n1
name
name
name
name
结合 -I 选项
使用 -I 指定一个替换字符串 {} ,这个字符串 {} 会在 xargs 扩展时被替换,当 -I 与 xargs 结合使用的时候,每一个参数命令都会被执行一次
cat arg.txt | xargs -I {} ./sk.sh -p {} -l
-p aaa -l
-p bbb -l
-p ccc -l
复制目录下所有的 .txt 文件到当前目录下d1目录中:
[root@openEuler ~]# mkdir d1
[root@openEuler ~]# ll
total 24
-rw-------. 1 root root 881 Mar 16 20:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Mar 17 10:28 d1
-rw-r--r--. 1 root root 49 Mar 16 21:07 hehe.txt
-rw-r--r--. 1 root root 35 Mar 17 09:51 message.txt
-rw-r--r--. 1 root root 183 Mar 16 21:03 passwd
-rw-r--r--. 1 root root 54 Mar 17 10:21 test.txt
[root@openEuler ~]# ls *.txt
hehe.txt message.txt test.txt
[root@openEuler ~]# ls *.txt | xargs -I cp {} d1
xargs: {}: No such file or directory
[root@openEuler ~]# ls *.txt | xargs -n 1 -I {} cp {} d1/
[root@openEuler ~]# ll
total 24
-rw-------. 1 root root 881 Mar 16 20:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Mar 17 10:29 d1
-rw-r--r--. 1 root root 49 Mar 16 21:07 hehe.txt
-rw-r--r--. 1 root root 35 Mar 17 09:51 message.txt
-rw-r--r--. 1 root root 183 Mar 16 21:03 passwd
-rw-r--r--. 1 root root 54 Mar 17 10:21 test.txt
[root@openEuler ~]# tree d1
d1
├── hehe.txt
├── message.txt
└── test.txt
0 directories, 3 files
1.8 cron
是Linux系统中后台进程模式周期性执行命令或指定程序的服务软件名
Linux中,系统启动后 cron软件便会启动,对应的进程名称叫 crond,默认是定期(每分钟检查一次)检查系统中是否有需要执行的任务计划
如果有计划,则按照计划进行,类似于一个闹钟
crond 服务除了会在工作时查看 /var/spool/cron 文件夹下的定时任务文件以外,还会查看 /etc/cron.d 目录以及 /etc/anacrontab 下面的文件内容,里面存放着每天、每周、每月需要执行的系统任务
[root@openEuler ~]# ls /var/spool/cron/
[root@openEuler ~]# ls -l /etc/ | grep cron*
-rw-r--r--. 1 root root 541 Dec 28 13:43 anacrontab
-rw-------. 1 root root 0 Mar 16 20:17 cron.allow
drwx------. 2 root root 4096 Mar 16 20:07 cron.d
drwx------. 2 root root 4096 Mar 16 20:08 cron.daily
drwx------. 2 root root 4096 Mar 16 20:07 cron.hourly
drwx------. 2 root root 4096 Dec 16 23:57 cron.monthly
-rw-------. 1 root root 451 Dec 16 23:57 crontab
drwx------. 2 root root 4096 Dec 16 23:57 cron.weekly
系统定时任务配置文件 /etc/crontab
[root@openEuler ~]# vim /etc/crontab
# 文件内容如下
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
crontab
crontab 命令是被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似
参数 | 解释 | 示例 |
-I | list 查看定时任务 | crontab -I |
-e | edit 编辑定时任务,建议手动编辑 | crontab -e |
-i | 删除定时任务,提示用户确认删除,避免出错 | crontab -i |
-r | 删除定时任务,移除/var/spool/cron/username 文件 | crontab -r |
-user | 指定用户执行任务,root可以管理普通用户计划任务 | crontab -u redhat -I |
使用格式
* * * * *
分 时 日 月 周
0 * * * * 每小时执行,每小时的整点执行
1 2 * * 4 每周执行,每周四的凌晨2点1分执行
1 2 3 * * 每月执行,每月的3号的凌晨2点1分执行
1 2 3 4 * 每年执行,每年的4月3号的凌晨2点1分执行
1 2 * * 3,5 每周3和周5的2点1分执行
* 13,14 * * 6,0 每周六、周日下午1点和2点的每一分钟都执行
0 9-18 * * 1-5 周一到周五每天早上9点到下午6点的每一个整点
*/10 * * * * 每隔10分钟执行一次任务
注意:如果没法整除,定时任务则没意义,可以自定制脚本控制频率
使用示例:
1. 每分钟执行命令
* * * * * cmd
2. 每天凌晨3点半和12点半执行脚本
30 3,12 * * * cmd
3. 每隔6小时,相当于6,12,18,24点半执行脚本
30 */6 * * * cmd
30 6,12,18,24 * * * cmd
4. 30代表半点,8-18/2表示早上8点到下午18点之间每隔2小时执行脚本
30 8-18/2 * * * cmd
5. 每天晚上9点30重启nginx
30 21 * * * /opt/nginx/sbin/nginx --restart
6. 每月1和10号凌晨4点45执行脚本
45 4 1,10 * * cmd
7. 每周六和周日凌晨1点10分执行命令
10 1 * * 6,7 cmd
10 1 * * 6,0 cmd
8. 每天18点到23点之间,每隔30分钟执行一次
*/30 18-23 * * * cmd
0,30 18-23 * * * cmd
9. 每隔一小时执行一次
* */1 * * * cmd
10. 4月份的周一到周三的上午11点执行脚本
* 11 * 4 1-3 cmd
11. 每天早上7点到上午11点,每2小时运行cmd命令
* 7-11/2 * * * cmd
12. 每天6点执行脚本
0 6 * * * cmd
13. 每周六凌晨4点执行
* 4 * * 6 cmd
14. 每周六凌晨4点05执行
5 4 * * 6 cmd
15. 每天8:40执行
40 8 * * * cmd
16. 在每天10:31开始,每隔2小时重复一次
31 10/2 * * * cmd
17. 每周一到周五2:00执行
0 2 * * 1-5 cmd
18. 每周一到周五8:00,每周一到周五9:00执行
0 8,9 * * 1-5 cmd
19. 每天10:00,16:00执行
0 10,16 * * * cmd