网站设计要点 优帮云,用树莓派做网站服务器好吗,沂水网站设计,郑州网站建设技术支持Linux常用命令的使用
Linux文件系统 和 Windows文件系统目录#xff1a; Windows是分C盘、D盘、E盘…的#xff0c;但是在Linux中是有一个最大的目录#xff0c;称之为根目录#xff0c;用 / 表示#xff0c;根目录下面会有很多子目录#xff0c;这些子目录其实可以理解…Linux常用命令的使用
Linux文件系统 和 Windows文件系统目录 Windows是分C盘、D盘、E盘…的但是在Linux中是有一个最大的目录称之为根目录用 / 表示根目录下面会有很多子目录这些子目录其实可以理解为windows中C盘、D盘、E盘。 不同的子目录具有不同的含义存放不同类型的文件信息。 bin 存放二进制可执行文件(ls,cat,mkdir等) boot 存放用于系统引导时使用的各种文件 dev 存放设备文件 etc 存放系统配置文件 home 存放所有用户文件的根目录 lib 存放跟文件系统中的程序运行所需要的共享库及内核模块 proc 虚拟文件系统存放当前内存的映射 usr 存放系统应用程序比较重要的目录/usr/local 管理员软件安装目录 var 存放运行时需要改变数据的文件 mnt 挂载目录 sbin 存储管理级别的二进制执行文件 root 超级用户主目录 opt 额外安装的可选应用程序包安装位置
文件查看
# pwd 列出当前目录的路径查看当前所在的路径。
# 注意~表示是用户家目录目前我们登陆的是root用户root用户的家目录是/root
[rootlocalhost ~]# pwd
/root# ls 列出当前目录下的所有文件
[rootlocalhost ~]# ls
anaconda-ks.cfg# ll(ls -l缩写) 列出当前目录下的文件带文件信息
[rootlocalhost ~]# ll
total 4
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
[rootlocalhost ~]# ls -l
total 4
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg# ll -a 列出当前目录下的所有文件包括隐藏文件
[rootlocalhost ~]# ll -a
total 28
dr-xr-x---. 2 root root 135 Mar 28 21:00 .
dr-xr-xr-x. 17 root root 224 Mar 28 20:58 ..
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-------. 1 root root 8 Mar 28 21:00 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2023 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2023 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2023 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2023 .cshrc
-rw-r--r--. 1 root root 129 Dec 29 2023 .tcshrc# ll --help 查看ls用法–help是一个帮助命令
[rootlocalhost ~]# ll --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.Mandatory arguments to long options are mandatory for short options too.-a, --all do not ignore entries starting with .
.............创建、重命名文件\文件夹
# touch filename 创建空文件
# 创建空文件hello.txt
[rootlocalhost ~]# touch hello.txt
[rootlocalhost ~]# ll
total 4
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt# mkdir 创建目录
# 创建目录abc
[rootlocalhost ~]# mkdir abc
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abc
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt# mkdir -p 目标目录存在也不报错
# 当我们在创建目录时不确定这个目录是否已存在的时候可以使用-p参数 就算目录已存在也不会报错如果不指定-p参数会报错会提示目录已存在
# 创建目录abc
[rootlocalhost ~]# mkdir -p abc
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abc
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
[rootlocalhost ~]# mkdir abc
mkdir: cannot create directory ‘abc’: File exists
# mv 重命名文件\文件夹
# 修改目录abc的名称为abx
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abc
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
[rootlocalhost ~]# mv abc abx
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
文件编辑
vi或vim文件编辑利器
# 编辑hello.txt文件不管这个文件存在与否都可以如果不存在最后在保存的时候会创建这个文件。
vi hello.txt
# 此时我们处于一个不可编辑的模式如果想编辑文件需要按键盘上的i键进入编辑模式。
# 文件编辑好了以后我们想要保存此时需要按键盘左上角的esc键退出编辑模式此时即进入了命令模式命令模式其实就是我们最开始进入的那个不可编辑的模式
# 在命令模式下按shift和: 最后输入wq 保存并退出
# 查找字符串在命令模式下输入/然后再输入你想要查询的字符串最后按回车键就可以进行查询了
# 查找某一行内容按shift和: 输入行号 回车
# 显示行号 按shift和: 输入 set nu
# 复制粘贴一行光标移动到需复制那一行内容上面连按yy既复制上一行了再按p粘贴
# 快速删除进入命令模式把光标定位到需删除的一行连按dd就可以删除当前行的内容清空当前行下的所有内容先连按999再连按dd
# 快速跳到文件首行在命令模式下按G可以快速将光标移动到最后一行
# 快速跳到文件末尾在命令模式下按gg即可快速跳转到第一行# 注意当修改一个文件修改到一半的时候把这命令行直接关闭了。
# 没有正确关闭文件所以产生了临时文件解决这个问题最直接最暴力的方式就是删除这个临时文件。
# 默认和这个原始文件在一个目录下面它是一个隐藏文件通过ll命令看不到这个隐藏文件的后缀名为.swp通过ll -a就可以看到了找到以后 使用rm删除掉即可
[rootlocalhost ~]# ll
total 8
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 78 Mar 29 18:02 hello.txt
[rootlocalhost ~]# ll -a
total 44
dr-xr-x---. 2 root root 174 Mar 29 18:16 .
dr-xr-xr-x. 17 root root 224 Mar 28 20:58 ..
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-------. 1 root root 188 Mar 29 18:14 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
-rw-r--r--. 1 root root 78 Mar 29 18:02 hello.txt
-rw-r--r--. 1 root root 12288 Mar 29 18:14 .hello.txt.swp
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
[rootlocalhost ~]# rm -rf .hello.txt.swp
grep查找sed编辑awk分析
[rootlocalhost ~]# cat hello.txt
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
abc# grep 常用于查找文件里符合条件的字符串
[rootlocalhost ~]# grep abc hello.txt
abc
# grep后面跟的这个字符串是可以支持正则表达式的
[rootlocalhost ~]# grep ^a hello.txt
abc
# 忽略大小写的查找
[rootlocalhost ~]# grep ABC hello.txt
[rootlocalhost ~]# grep -i ABC hello.txt
abc
# 查询出来对应字符串所在的行号
[rootlocalhost ~]# grep -i ABC -n hello.txt
7:abc
# 从所有进程中查找到java进程
[rootlocalhost ~]# ps -ef | grep java
root 2497 2247 0 22:34 pts/1 00:00:00 grep --colorauto java
# grep -v 再做一次过滤表示忽略包含指定字符串的数据。
[rootlocalhost ~]# ps -ef | grep java | grep -v grep[rootlocalhost ~]# cat hello.txt
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
abc
# 注意sed 默认不会直接修改源文件数据而是会将数据复制到缓冲区中修改也仅限于缓冲区中的数据最终把缓冲区内的数据输出到控制台
# 在指定文件的第2行下添加一行
[rootlocalhost ~]# sed 2a\haha hello.txt
hello world!
hello world!
haha
hello world!
hello world!
hello world!
hello world!
abc
# sed [第几行]a\[添加内容] 文件 a参数表示向指定行的下面添加数据
[rootlocalhost ~]# sed 1a\haha hello.txt
hello world!
haha
hello world!
hello world!
hello world!
hello world!
hello world!
abc
# sed ‘0a\haha’ hello.txt 此操作会报错行号是从1开始的
[rootlocalhost ~]# sed 0a\haha hello.txt
sed: -e expression #1, char 2: invalid usage of line address 0
# sed [第几行]i\[添加内容] 文件 i的意思表示在指定行的前面插入一行
[rootlocalhost ~]# sed 1i\haha hello.txt
haha
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
abc
# 在一个文件的最后一行的前面添加一行内容$ 它在这里表示是最后一行
[rootlocalhost ~]# sed $i\haha hello.txt
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
haha
abc
# 删除第7行内容sed [行号]d 文件d表示删除
[rootlocalhost ~]# sed 7d hello.txt
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!
# 删除最后一行
[rootlocalhost ~]# sed $d hello.txt
hello world!
hello world!
hello world!
hello world!
hello world!
hello world!# sed后面的参数格式为[行号]s/[需要替换的内容]/[要替换的新内容]/[flags表示指定要替换的字符串在这一行中出现第几次时才进行替换] s表示替换
# flags有多种用法1、flags可以表示为1~512之间的任意一个数字表示指定要替换的字符串在这一行中出现第几次时才进行替换
# 2、flags可以直接表示为g这样的意思就是对每一行数据中所有匹配到的内容全部进行替换
# 3、flags位置的值为空则只会在第一次匹配成功时做替换操作。
[rootlocalhost ~]# sed s/l/a/1 hello.txt
healo world!
healo world!
healo world!
healo world!
healo world!
healo world!
abc
[rootlocalhost ~]# sed s/l/a/2 hello.txt
helao world!
helao world!
helao world!
helao world!
helao world!
helao world!
abc
[rootlocalhost ~]# sed s/l/a/3 hello.txt
hello worad!
hello worad!
hello worad!
hello worad!
hello worad!
hello worad!
abc
[rootlocalhost ~]# sed s/l/a/g hello.txt
heaao worad!
heaao worad!
heaao worad!
heaao worad!
heaao worad!
heaao worad!
abc
[rootlocalhost ~]# sed s/l/a/ hello.txt
healo world!
healo world!
healo world!
healo world!
healo world!
healo world!
abc
# 注意上述sed命令的所有操作在执行之后都不会修改源文件中的内容如果需要修改源文件的话需要增加一个 -i 参数
[rootlocalhost ~]# sed -i 2s/l/a/g hello.txt
[rootlocalhost ~]# cat hello.txt
hello world!
heaao worad!
hello world!
hello world!
hello world!
hello world!
abc
# awk就是把文件逐行的读入以空白字符为默认分隔符将每行内容切片切开的部分再进行各种分析处理
# awk [option,指定文件中数据的字段分隔符] [programe,具体的处理逻辑] [file,操作的文件]
# awk在处理文本数据的时候它会自动给每行中的每个字段分配一个变量
# 变量从1开始
# $1表示是文本中的第1个数据字段,$2表示是文本中的第2个数据字段,以此类推。
# $0 它代表整个文本行的内容
[rootlocalhost ~]# cat hello.txt
hello world!
heaao worad!
hello world!
hello world!
hello world!
hello world!
abc
[rootlocalhost ~]# awk {print $1} hello.txt
hello
heaao
hello
hello
hello
hello
abc
[rootlocalhost ~]# awk {print $2} hello.txt
world!
worad!
world!
world!
world!
world!
[rootlocalhost ~]# awk {print $0} hello.txt
hello world!
heaao worad!
hello world!
hello world!
hello world!
hello world!
abc# 指定文件中分隔符操作某列字段
# 例如/etc/password 文件中的字段之间是使用:分割的
# 通过-F 参数 awk -F: ‘{print $1}’ /etc/passwd
[rootlocalhost ~]# awk -F: {print $1} /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix# 使用正则表达式进行过滤
[rootlocalhost ~]# cat hello.txt
hello world!
heaao worad!
hello world!
hello world!
hello world!
hello world!
abc
# awk ‘/[指定匹配字符]/ {print $1}’ hello.txt 这种写法表示对每次读取到的那一行数据进行匹配
[rootlocalhost ~]# awk /world/ {print $0} hello.txt
hello world!
hello world!
hello world!
hello world!
hello world!
[rootlocalhost ~]# awk /abc/ {print $0} hello.txt
abc
# awk ‘($1 ~ /world/) {print $1}’ hello.txt 在这里面可以通过$来指定具体是哪一列需要把具体的对比逻辑放到小括号里面
[rootlocalhost ~]# awk ($1 ~ /world/) {print $0} hello.txt
[rootlocalhost ~]# awk ($2 ~ /world/) {print $0} hello.txt
hello world!
hello world!
hello world!
hello world!
hello world!
[rootlocalhost ~]# awk ($2 ~ /wor[a-z]d/) {print $1} hello.txt
hello world!
heaao worad!
hello world!
hello world!
hello world!
hello world!
# 注意这里如果是不匹配的意思的话需要使用 !~文件内容统计
wc、uniq、sort、head文件内容统计相关命令
# wc统计字数相关信息
# -c是表示获取文件内容的字节数量
# -l表示是行数
# -L是获取最长的一行内容的长度
# -w 表示文件中单词的个数默认使用空白符切割[rootlocalhost ~]# wc --help
Usage: wc [OPTION]... [FILE]...or: wc [OPTION]... --files0-fromF
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. With no FILE, or when FILE is -,
read standard input. A word is a non-zero-length sequence of characters
delimited by white space.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.-c, --bytes print the byte counts-m, --chars print the character counts-l, --lines print the newline counts--files0-fromF read input from the files specified byNUL-terminated names in file F;If F is - then read names from standard input-L, --max-line-length print the length of the longest line-w, --words print the word counts--help display this help and exit--version output version information and exit[rootlocalhost ~]# wc -c hello.txt
78 hello.txt
[rootlocalhost ~]# wc -m hello.txt
78 hello.txt
[rootlocalhost ~]# wc -l hello.txt
6 hello.txt
[rootlocalhost ~]# wc -L hello.txt
12 hello.txt
[rootlocalhost ~]# wc -w hello.txt
12 hello.txtsort排序
# -n意思是按照数据的数值大小排序正序排序
[rootlocalhost ~]# sort -n num.txt
1
2
3
9
10# -nr意思是按照数据的数值大小排序倒序排序
[rootlocalhost ~]# sort -nr num.txt
10
9
3
2
1# 在-n的基础上增加-k 这个参数后面需要指定一个数字这个数字表示是文件中的第几列编号从1开始
[rootlocalhost ~]# sort -k 2 -n num2.txt
ax 1
bc 2
dd 7
aa 9
xc 15
uniq检查重复的行列
# -c在输出行的前面加上数据在文件中重复出现的次数
[rootlocalhost ~]# uniq -c hello.txt 6 hello world!# -u表示返回文件中不重复的行
[rootlocalhost ~]# uniq -u hello.txt
abc# uniq只能对连续在一起的重复内容进行去重
[rootlocalhost ~]# cat test.txt
hello
hello
abc
hello
hello
[rootlocalhost ~]# uniq test.txt
hello
abc
hello# |通过管道可以把前面一个命令的输出结果传递给后面一个命令
[rootlocalhost ~]# sort test.txt | uniq
abc
hello
head取前N条数据
# head表示获取前N条数据默认返回前10条后面可以通过指定数字来控制返回的数据条数
[rootlocalhost ~]# cat num.txt
3
2
9
10
1
[rootlocalhost ~]# head -3 num.txt
3
2
9
[rootlocalhost ~]# sort -nr num.txt | head -3
10
9
3链接文件
# linux有两种链接硬链接、符号(软)链接
# 软链接功能类似类似于windows的快捷方式主要用于节省磁盘空间
# 首先看硬链接硬链接相当于对原始文件的一个复制不能对目录使用硬链接。
# 命令ln hello.txt hlink
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
[rootlocalhost ~]# ln hello.txt hlink
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 2 root root 0 Mar 28 22:21 hello.txt
-rw-r--r--. 2 root root 0 Mar 28 22:21 hlink
# 再看符号(软)链接
# 如果想使用软连接需要添加-s相当于快捷方式不能删除原文件
# 命令ln -s hello.txt vlink
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 2 root root 0 Mar 28 22:21 hello.txt
-rw-r--r--. 2 root root 0 Mar 28 22:21 hlink
[rootlocalhost ~]# ln -s hello.txt vlink
[rootlocalhost ~]# ll
total 4
drwxr-xr-x. 2 root root 6 Mar 28 22:22 abx
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
-rw-r--r--. 2 root root 0 Mar 28 22:21 hello.txt
-rw-r--r--. 2 root root 0 Mar 28 22:21 hlink
lrwxrwxrwx. 1 root root 9 Mar 28 22:33 vlink - hello.txt
切换目录
# cd . 当前目录
# 一个.表示当前目录
[rootlocalhost ~]# pwd
/root
[rootlocalhost ~]# cd .
[rootlocalhost ~]# pwd
/root
# cd .. 去上一级目录
# 两个.表示上一级目录
[rootlocalhost ~]# pwd
/root
[rootlocalhost ~]# cd ..
[rootlocalhost /]# pwd
/
# cd / 去根目录
# 通过在cd后面指定目录可以切换到指定目录
[rootlocalhost /]# cd /
[rootlocalhost /]# pwd
/
[rootlocalhost ~]# cd /bin/
[rootlocalhost bin]# pwd
/bin
# cd ~ 去当前用户主(家)目录
[rootlocalhost bin]# cd ~
[rootlocalhost ~]# pwd
/root
# cd xxx/xxx 直接跳转到某个目录
[rootlocalhost ~]# cd abx/test/
[rootlocalhost test]# pwd
/root/abx/test
删除文件\文件夹(目录)
# rm命令可以删除文件或者目录也可以将某个目录及其下属的所有文件及其子目录均删除掉
# 对于链接文件只是删除整个链接文件而原有文件保持不变。
# 常见用法rm 删除文件但是会有提示确认对话输入y确认删除
[rootlocalhost test]# touch abc.txt
[rootlocalhost test]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 29 13:53 abc.txt
[rootlocalhost test]# rm abc.txt
rm: remove regular empty file ‘abc.txt’? y
[rootlocalhost test]# ll
total 0
# rm -r 删除目录需要确认
# 删除目录需要指定r参数否则会提示不能删除
# r是给rm加入递归(recursion)特性也就是目标为文件夹时删除文件夹下所有数据
# 使用rm -r 在删除目录的时候也会有提示确认对话输入y确认删除
[rootlocalhost abx]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 29 14:01 test
[rootlocalhost abx]# rm test
rm: cannot remove ‘test’: Is a directory
[rootlocalhost abx]# rm -r test
rm: remove directory ‘test’? y
[rootlocalhost abx]# ll
total 0
# rm -f 强制删除
# f给rm加入强制(force)特性也就是遇到删除时不需要询问即可直接删除
# 注意这个操作还是比较危险的建议慎用因为删除之后就找不到了
# Linux系统中没有回收站
[rootlocalhost abx]# touch a.txt
[rootlocalhost abx]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 29 14:03 a.txt
[rootlocalhost abx]# rm -f a.txt
[rootlocalhost abx]# ll
total 0
# rm -rf 递归删除目录及其文件
# Linux中最危险的操作最具破坏性
# rf参数可以强制递归删除任何数据并且没有任何提示慎用慎用慎用
[rootlocalhost ~]# ll
drwxr-xr-x. 2 root root 6 Mar 29 14:03 abx
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
[rootlocalhost ~]# mkdir -p abx/test/aaa
[rootlocalhost ~]# cd abx/test/aaa/
[rootlocalhost aaa]# touch a.txt
[rootlocalhost aaa]# cd ~
[rootlocalhost ~]# rm -rf abx
[rootlocalhost ~]# ll
-rw-------. 1 root root 1243 Mar 28 20:59 anaconda-ks.cfg
复制\粘贴\剪切
# 常见用法cp 复制粘贴文件
# 复制hello.txt文件复制后的文件名为hello-bak.txt
[rootlocalhost ~]# ll
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
[rootlocalhost ~]# cp hello.txt hello-bak.txt
[rootlocalhost ~]# ll
-rw-r--r--. 1 root root 0 Mar 29 14:20 hello-bak.txt
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
# cp -r 复制粘贴文件或目录复制目录需要指定r参数
[rootlocalhost ~]# mkdir abc
[rootlocalhost ~]# ll
drwxr-xr-x. 2 root root 6 Mar 29 14:21 abc
[rootlocalhost ~]# cp abc xyz 【错误用法复制目录必须指定-r参数】
cp: omitting directory ‘abc’
[rootlocalhost ~]# cp -r abc xyz
[rootlocalhost ~]# ll
drwxr-xr-x. 2 root root 6 Mar 29 14:21 abc
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz
# mv 移动剪切文件或目录
# 将目录xyz移动到目录abc下面
[rootlocalhost ~]# ll
drwxr-xr-x. 2 root root 6 Mar 29 14:21 abc
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz
[rootlocalhost ~]# ll abc/
total 0
[rootlocalhost ~]# mv xyz abc
[rootlocalhost ~]# ll abc/
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz
远程复制
# scp命令用于在网络中不同主机之间复制文件或目录。scp是有Security的文件copy基于ssh登录如果没有配置免密码登陆需要输入主机密码。
# 常见用法
# 从本地复制192.168.182.131到远程主机 192.168.182.130
# scp /root/hello.txt 192.168.182.130:/root
[rootlocalhost ~]# scp /root/hello.txt 192.168.182.130:/root/
The authenticity of host 192.168.182.130 (192.168.182.130) cant be established.
ECDSA key fingerprint is SHA256:uUG2QrWRlzXcwfv6GUot9DVs9ciFugZ7FhR89m2S00.
ECDSA key fingerprint is MD5:82:9d:01:51:06:a7:14:24:a9:16:3d:a1:5e:6d:0d:16.
Are you sure you want to continue connecting (yes/no)? yes 【第一次会提示此信息输入yes即可以后就不提示了】
Warning: Permanently added 192.168.182.130 (ECDSA) to the list of known hosts.
root192.168.182.130s password: 【在这里输入192.168.182.130机器的密码即可】
hello.txt 100% 0 0.0KB/s 00:00
# 提示
# 显示进度在scp后添加-v
# 复制目录在scp后添加-r
# 静默复制模式在scp后添加-q
# scp -rq /root/abc 192.168.182.130:/root
[rootlocalhost ~]# scp -rq /root/abc/ 192.168.182.130:/root/
root192.168.182.130s password:【在这里输入192.168.182.130机器的密码即可】
文件属性 黑带七段
第一段权限 第一个字符代表文件-、目录d链接l 其余字符每3个一组rwx读r、写w、执行x第一组文件所有者的权限是读、写和执行第二组与文件所有者同一组的用户的权限第三组不与文件所有者同组的其他用户的权限 也可用数字表示为r4w2x1如权限6可以表示为rw6 第二段目录/链接个数 对于目录文件表示它的第一级子目录的个数 注意此处的值要减2才等于该目录下的子目录的实际个数(目录下默认包含.和这两个目录) 对于其他文件默认是1第三段所属用户第四段所属组第五段文件大小字节第六段最后修改时间第七段文件\文件夹名称
chmod 分配权限
# 常见用法
# chmod ux xxx.txt 给当前所有者添加执行权限【x表示是执行权限】
# 针对hello.txt文件给当前用户添加执行权限
[rootlocalhost ~]# ll
-rw-r--r--. 1 root root 0 Mar 28 22:21 hello.txt
[rootlocalhost ~]# chmod ux hello.txt
[rootlocalhost ~]# ll
-rwxr--r--. 1 root root 0 Mar 28 22:21 hello.txt
# chmod 777 xxx.txt 添加rwxrwxrwx权限
# 给hello.txt添加777权限
[rootlocalhost ~]# chmod 777 hello.txt
[rootlocalhost ~]# ll
-rwxrwxrwx. 1 root root 0 Mar 28 22:21 hello.txt
# chmod -R 777 xxx 给指定目录递归添加rwxrwxrwx权限
# 给abc目录及其子目录中的所有内容添加777权限
[rootlocalhost ~]# ll
drwxr-xr-x. 3 root root 17 Mar 29 14:24 abc
[rootlocalhost ~]# cd abc/
[rootlocalhost abc]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 29 14:21 xyz
[rootlocalhost abc]# cd ..
[rootlocalhost ~]# chmod -R 777 abc
[rootlocalhost ~]# ll
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc
[rootlocalhost ~]# cd abc/
[rootlocalhost abc]# ll
total 0
drwxrwxrwx. 2 root root 6 Mar 29 14:21 xyz
内容查看
# cat 显示文本内容类似windows中的type(顺序输出)
[rootlocalhost ~]# cat anaconda-ks.cfg
#versionDEVEL
#System authorization information
auth --enableshadow --passalgosha512
......# cat -b 显示行号输出
[rootlocalhost ~]# cat -b anaconda-ks.cfg 1 #versionDEVEL2 # System authorization information3 auth --enableshadow --passalgosha5124 # Use CDROM installation media
........# 分屏显示 more
# 用一次显示一屏没有显示完时最后一行显示进度。回车显示下一行按b显示上一页空格显示下一页q退出。根据当前屏幕大小显示一屏内容
[rootlocalhost ~]# more anaconda-ks.cfg
#versionDEVEL
#System authorization information
auth --enableshadow --passalgosha512
#Use CDROM installation media
cdrom
#Use graphical install
graphical
#Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-usesda
#Keyboard layouts
keyboard --vckeymapus --xlayoutsus
#System language
lang en_US.UTF-8#Network information
network --bootprotodhcp --deviceens33 --ipv6auto --activate
network --hostnamelocalhost.localdomain#Root password
rootpw --iscrypted $6$sx2fOWF8AMiHgQTV$ExkpEX6Sq1EfZVHaP4RxfYls3B0o
dX2ouFfaTX2S0TttzWz7tX3L3cWRFeb1M4qfGUA2FGzrkylhlGfp4psze.
--More--(48%)
压缩、解压
常见用法压缩和解压 参数 -z 是否同时具有 gzip 的属性亦即是否需要用 gzip 压缩 -c 创建一个压缩文件的参数指令(create 的意思) -x 解开一个压缩文件的参数指令 -v 压缩的过程中显示文件 -f 使用档案名字这个参数是最后一个参数后面只能接档案名 注意特别注意在参数的下达中 c/x 仅能存在一个不可同时存在
# 压缩
# tar -zcvf 打包及压缩gzip方式
# 将abc目录的内容打包压缩为abc.tar.gz
[rootlocalhost ~]# ll
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc
[rootlocalhost ~]# tar -zcvf abc.tar.gz abc
abc/
abc/xyz/
[rootlocalhost ~]# ll
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc
-rw-r--r--. 1 root root 130 Mar 29 15:24 abc.tar.gz
# 解压
# tar -zxvf 解压gzip包
# 先把abc.tar.gz移动到test目录下再解压缩
[rootlocalhost ~]# ll
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc
-rw-r--r--. 1 root root 130 Mar 29 15:24 abc.tar.gz
[rootlocalhost ~]# mkdir test
[rootlocalhost ~]# cd test/
[rootlocalhost test]# mv ../abc.tar.gz .
[rootlocalhost test]# ll
total 4
-rw-r--r--. 1 root root 130 Mar 29 15:24 abc.tar.gz
[rootlocalhost test]# tar -zxvf abc.tar.gz
abc/
abc/xyz/
[rootlocalhost test]# ll
total 4
drwxrwxrwx. 3 root root 17 Mar 29 14:24 abc
-rw-r--r--. 1 root root 130 Mar 29 15:24 abc.tar.gz
输出及显示
echo将内容输出到设备类似java里面的system.out.println() 常见用法 echo “hello\t\t world” 不解析转义字符 echo -e “hello\t\t world” 解析转义字符 echo $PATH 输出环境变量 注意在打印变量信息的时候使用echo ${PATH} 也可以效果是一样的
[rootlocalhost ~]# echo hello\t\t world
hello\t\t world
[rootlocalhost ~]# echo -e hello\t\t world
hello world
[rootlocalhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[rootlocalhost ~]# echo ${PATH}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
时间命令
[rootlocalhost ~]# date --help
Usage: date [OPTION]... [FORMAT]or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.Mandatory arguments to long options are mandatory for short options too.-d, --dateSTRING display time described by STRING, not now-f, --fileDATEFILE like --date once for each line of DATEFILE
.......
FORMAT controls the output. Interpreted sequences are:%% a literal %%a locales abbreviated weekday name (e.g., Sun)%A locales full weekday name (e.g., Sunday)%b locales abbreviated month name (e.g., Jan)%B locales full month name (e.g., January)%c locales date and time (e.g., Thu Mar 3 23:05:25 2005)%C century; like %Y, except omit last two digits (e.g., 20)%d day of month (e.g., 01)%D date; same as %m/%d/%y%e day of month, space padded; same as %_d%F full date; same as %Y-%m-%d%g last two digits of year of ISO week number (see %G)%G year of ISO week number (see %V); normally useful only with %V%h same as %b%H hour (00..23)%I hour (01..12)%j day of year (001..366)%k hour, space padded ( 0..23); same as %_H%l hour, space padded ( 1..12); same as %_I%m month (01..12)%M minute (00..59)%n a newline%N nanoseconds (000000000..999999999)%p locales equivalent of either AM or PM; blank if not known%P like %p, but lower case%r locales 12-hour clock time (e.g., 11:11:04 PM)%R 24-hour hour and minute; same as %H:%M%s seconds since 1970-01-01 00:00:00 UTC%S second (00..60)%t a tab%T time; same as %H:%M:%S%u day of week (1..7); 1 is Monday%U week number of year, with Sunday as first day of week (00..53)%V ISO week number, with Monday as first day of week (01..53)%w day of week (0..6); 0 is Sunday%W week number of year, with Monday as first day of week (00..53)%x locales date representation (e.g., 12/31/99)%X locales time representation (e.g., 23:13:48)%y last two digits of year (00..99)%Y year%z hhmm numeric time zone (e.g., -0400)%:z hh:mm numeric time zone (e.g., -04:00)%::z hh:mm:ss numeric time zone (e.g., -04:00:00)%:::z numeric time zone with : to necessary precision (e.g., -04, 05:30)%Z alphabetic time zone abbreviation (e.g., EDT)# date命令默认获取系统当前时间
[rootlocalhost ~]# date
Sun Mar 29 20:48:15 CST 2023# 通过%Y%m%d这些参数可以对日期进行格式化
# date %Y-%m-%d %H:%M:%S
# 注意date后面的这些参数中间如果没有空格可以省略双引号。
[rootlocalhost ~]# date %Y-%m-%d %H:%M:%S
2026-03-29 21:06:45# %s表示获取自1970-01-01 00:00:00以来的秒数 当前时间的时间戳 date %s
[rootlocalhost ~]# date %s
1585487600# 虽然date命令没有直接支持获取毫秒数但是从秒换算为毫秒最直接粗暴的方式就是在秒数后面加3个0
[rootlocalhost ~]# date %s000
1585487796000# 获得指定时间
[rootlocalhost ~]# date --date2026-01-01 00:00:00
Wed Jan 1 00:00:00 CST 2026# 获得指定时间的时间戳
[rootlocalhost ~]# date --date2026-01-01 00:00:00 %s
1577808000# 获取昨天的日期
[rootlocalhost ~]# date --date1 days ago
Sat Mar 28 21:36:37 CST 2026
# 获取昨天的日期 只获取年月日
[rootlocalhost ~]# date --date1 days ago %Y-%m-%d
2026-03-28
# 2月份有多少天
[rootlocalhost ~]# date --date2026-03-01 1 days ago %d
29软件安装和卸载
第一种压缩包安装方式直接解压配置相应的环境变量即可使用 第二种在线安装使用yum yum集成了连接网络软件安装删除更新等功能yum在配置好repo后机器只要连网就能智能化安装软件使用yum 安装的好处在于可以自动安装软件需要的依赖包
# 安装
yum install -y 安装
# 升级
yum update 不跟则更新全部
# 查找和显示
yum info 显示包信息
yum list 不跟则显示已安装或可安装包
# 删除程序
yum remove
# 清除缓存
yum clean all 清除所有缓存包含文件、旧软件查看操作历史
history保留了最近执行的命令记录默认可以保留1000。 历史清单从0开始编号到最大值。 常见用法 history N 显示最近N条命令 history -c 清除所有的历史记录 history -w xxx.txt 保存历史记录到文本xxx.txt
[rootlocalhost ~]# history 10142 ll143 tar -zxvf abc.tar.gz 144 ll145 cd .146 ll147 echo hello\t\t world148 echo -e hello\t\t world149 env150 echo $PATH151 history 10
[rootlocalhost ~]# history -w his.txt
[rootlocalhost ~]# ll
-rw-------. 1 root root 1395 Mar 29 15:41 his.txt
[rootlocalhost ~]# more his.txt
ip addr
ls /
pwd
ls
ll
..........
磁盘使用情况
# 使用df命令查看硬盘使用情况
# 已使用1.2G还剩余16G
[rootlocalhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.5M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 1.2G 16G 7% /
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0查看内存使用情况
# free 查看内存和交换空间的使用情况
# 常见用法
# free -m显示内存单位为MB
# free -h根据值的大小显示易于识别的单位
[rootlocalhost ~]# free -mtotal used free shared buff/cache available
Mem: 1819 169 1509 9 140 1501
Swap: 2047 0 2047
[rootlocalhost ~]# free -htotal used free shared buff/cache available
Mem: 1.8G 169M 1.5G 9.5M 140M 1.5G
Swap: 2.0G 0B 2.0G
进程相关命令
ps显示进程信息netstat显示端口信息jps显示Java进程信息top动态监控进程信息nohup守护进程运行应用
# 显示系统内的所有进程
[rootlocalhost ~]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 20:46 ? 00:00:01 /usr/lib/systemd/systemd --sw
root 2 0 0 20:46 ? 00:00:00 [kthreadd]
root 4 2 0 20:46 ? 00:00:00 [kworker/0:0H]
........# 过滤出系统内的java进程信息
# 过滤出系统内的python进程信息
# 注意这里面返回的grep --colorauto这一行信息表示是grep这个命令本身
[rootlocalhost ~]# ps -ef | grep java
root 2239 1973 0 22:19 pts/0 00:00:00 grep --colorauto java
[rootlocalhost ~]# ps -ef | grep python
root 736 1 0 20:46 ? 00:00:00 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
root 1070 1 0 20:46 ? 00:00:00 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root 2241 1973 0 22:19 pts/0 00:00:00 grep --colorauto python# ps -ef |grep abc 查看含有abc的活动进程
# ps -ef |grep -v abc 查看不含abc的活动进程
# ps a 显示现行终端机下的所有程序包括其他用户的程序。
# ps -A 显示所有程序。
# ps c 列出程序时显示每个程序真正的指令名称而不包含路径参数或常驻服务的标示。
# ps -e 此参数的效果和指定A参数相同。
# ps e 列出程序时显示每个程序所使用的环境变量。
# ps f 用ASCII字符显示树状结构表达程序间的相互关系。
# ps -H 显示树状结构表示程序间的相互关系。
# ps -j显示进程归属的进程组id、会话id、父进程id
# ps -u显示进程的归属用户及内存使用情况
# ps x显示没有关联控制终端的进程# netstat也是显示进程相关信息的只不过可以比ps命令额外显示端口相关的信息
# 这个命令默认是没有安装的最方便的方式就是使用yum来在线安装 yum install -y net-tools
[rootlocalhost ~]# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1312/master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1071/sshd
tcp 0 52 192.168.182.132:22 192.168.182.1:51472 ESTABLISHED 1969/sshd: rootpts
tcp6 0 0 ::1:25 :::* LISTEN 1312/master
tcp6 0 0 :::22 :::* LISTEN 1071/sshd
......
# ssh服务的端口使用情况
[rootlocalhost ~]# netstat -anp | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1071/sshd
tcp 0 52 192.168.182.132:22 192.168.182.1:51472 ESTABLISHED 1969/sshd: rootpts
tcp6 0 0 :::22 :::* LISTEN 1071/sshd # jps显示当前正在运行的 Java 进程的进程 ID 和类名或 JAR 文件名。
# 用来查看基于HotSpot的JVM里面中所有具有访问权限的Java进程的具体状态, 包括进程ID进程启动的路径及启动参数等等类似ps命令不同的是ps是用来显示所有进程信息的而jps只显示Java进程准确的说jps是显示当前用户已启动的Java进程信息信息包括进程号和简短的进程command。
#注意jps是Java提供的命令只有在安装配置好Java环境之后才能使用
#使用jps时如果没有指定hostid它只会显示本地环境中所有的Java进程如果指定了hostid它就会显示指定hostid上面的java进程不过这需要远程服务上开启了jstatd服务。
# hostid指定了目标的服务器[protocol:][[//]hostname][:port][/servername]
jps [ options ] [ hostid ]
# -q 忽略输出的类名、Jar名以及传递给main方法的参数只输出pid。
# -m 输出传递给main方法的参数如果是内嵌的JVM则输出为null。
# -l 输出应用程序主类的完整包名或者是应用程序JAR文件的完整路径。
# -v 输出传给JVM的参数。
# -V 输出通过标记的文件传递给JVM的参数.hotspotrc文件或者是通过参数-XX:Flags指定的文件。
# -J 用于传递jvm选项到由javac调用的java加载器中例如“-J-Xms48m”将把启动内存设置为48M使用-J选项可以非常方便的向基于Java的开发的底层虚拟机应用程序传递参数。
在Linux系统中ps和jps是两个不同的命令用于查看不同类型的进程信息。 ps命令ps命令是一个通用的进程查看工具用于显示当前正在运行的所有进程的详细信息。它可以显示包括系统进程在内的所有进程并提供有关进程IDPID、父进程IDPPID、CPU使用率、内存使用量等信息。ps命令适用于查看系统中所有类型的进程不限于Java进程。 jps命令jps命令是Java特定的进程查看工具用于显示当前正在运行的Java进程的信息。它主要用于检查Java应用程序的运行状态和进程ID。jps命令只显示正在运行的Java进程并提供每个Java进程对应的进程IDPID和Java类名称。jps命令需要在安装了Java Development Kit (JDK) 的系统上才能使用。
top主要作用在于动态显示系统消耗资源最多的进程信息包含进程ID、内存占用、CPU占用等top和ps命令作用基本相同唯一的区别是top命令能够动态显示进程信息。
动态视图top 提供了一个实时动态更新的视图能够持续显示系统中当前正在运行的进程信息及其资源占用情况。系统性能监控除了显示进程信息外top 还可以显示系统的整体性能指标如平均负载uptimeload average、CPU使用率%CPU、内存使用量%MEM等。更新频率默认情况下top命令会定期通常是3秒钟刷新输出更新进程列表和系统状态。 top -d 2。
注意这里的CPU使用情况是总体CPU的使用情况如果是多核CPU想查看具体每个CPU的使用情况的话可以在监控模式中按键盘上的1就可以查看每一个CPU的情况了按q可以退出此监控模式。 第一行系统运行时间和平均负载 当前时间、系统已运行时间、当前登录用户的数量、最近5、10、15分钟内的平均负载
第二行任务 任务的总数、运行中(running)的任务、休眠(sleeping)中的任务、停止(stopped)的任务、僵尸状态(zombie)的任务
第三行cpu状态
字段释义ususer 运行(未调整优先级的) 用户进程的CPU时间sysystem: 运行内核进程的CPU时间niniced运行已调整优先级的用户进程的CPU时间ididle:空闲时间waIO wait: 用于等待IO完成的CPU时间hi处理硬件中断的CPU时间si处理软件中断的CPU时间st这个虚拟机被hypervisor偷去的CPU时间译注如果当前处于一个hypervisor下的vm实际上hypervisor也是要消耗一部分CPU处理时间的
第四行内存 全部可用内存、已使用内存、空闲内存、缓冲内存
第五行swap 全部、已使用、空闲和缓冲交换空间
第七行至N行各进程任务的的状态监控
字段释义PID进程ID进程的唯一标识符USER进程所有者的实际用户名PR进程的调度优先级。这个字段的一些值是’rt’。这意味这这些进程运行在实时态NI进程的nice值优先级。越小的值意味着越高的优先级。负值表示高优先级正值表示低优先级VIRTvirtual memory usage 虚拟内存,进程使用的虚拟内存。进程使用的虚拟内存总量单位kbVIRTSWAPRES1、进程“需要的”虚拟内存大小包括进程使用的库、代码、数据等2、假如进程申请100m的内存但实际只使用了10m那么它会增长100m而不是实际的使用量RESresident memory usage 常驻内存,驻留内存大小。驻留内存是任务使用的非交换物理内存大小。进程使用的、未被换出的物理内存大小单位kbRESCODEDATA1、进程当前使用的内存大小但不包括swap out2、包含其他进程的共享3、如果申请100m的内存实际使用10m它只增长10m与VIRT相反4、关于库占用内存的情况它只统计加载的库文件所占内存大小SHRshared memory 共享内存1、除了自身进程的共享内存也包括其他进程的共享内存2、虽然进程只使用了几个共享库的函数但它包含了整个共享库的大小3、计算某个进程所占的物理内存大小公式RES – SHR4、swap out后它将会降下来S这个是进程的状态。它有以下不同的值: D - 不可中断的睡眠态R – 运行态S – 睡眠态T – 被跟踪或已停止Z – 僵尸态%CPU自从上一次更新时到现在任务所使用的CPU时间百分比。%CPU显示的是进程占用一个核的百分比而不是整个cpuN核的百分比有时候可能大于100那是因为该进程启用了多线程占用了多个核心所以有时候我们看该值得时候会超过100%但不会超过总核数*100%MEM进程使用的可用物理内存百分比TIME任务启动后到现在所使用的全部CPU时间精确到百分之一秒COMMAND运行进程所使用的命令。进程名称命令名/命令行
# 停止某一个进程PID是进程号
kill PID
# 强制停止某一进程PID是进程号
kill -9 PID # nohup 是一个在类 Unix 操作系统如 Linux 和 macOS中广泛使用的命令全称是 no hang up即“不挂断”。它的主要目的是允许用户在终端断开连接或注销账户后仍然能够让指定的命令或程序在后台继续运行不受 SIGHUP挂起信号的影响。
nohup java -jar your-project.jar
# 如果你想要将输出重定向到一个日志文件可以这样做
nohup java -jar your-project.jar /tmp/wms.log 21 关机重启快捷命令
# 关机
shutdown -h now
# 重启
reboot -h now
# 退出当前登录状态
exit Linux基本配置修改
Linux静态ip设置Linux临时设置永久设置(hostname)配置Linux临时关闭永久关闭(防火墙)
临时关闭的特性是立刻生效重启失效 永久关闭的特性是重启生效永久有效
Linux静态ip设置
# 如果未设置静态ipLinux虚拟机每次重启之后都可能会重新获取到新的ip地址
[rootlocalhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPEEthernet
PROXY_METHODnone
BROWSER_ONLYno
BOOTPROTOdhcp
DEFROUTEyes
IPV4_FAILURE_FATALno
IPV6INITyes
IPV6_AUTOCONFyes
IPV6_DEFROUTEyes
IPV6_FAILURE_FATALno
IPV6_ADDR_GEN_MODEstable-privacy
NAMEens33
UUID9a0df9ec-a85f-40bd-9362-ebe134b7a100
DEVICEens33
ONBOOTyes
# 首先修改BOOTPROTO参数将之前的dhcp改为staticBOOTPROTOstatic
# 然后在文件末尾增加三行内容【注意我现在使用的是nat网络模式不同的网络模式在这里填写的ip信息是不一样的】
IPADDR192.168.182.100
GATEWAY192.168.182.2
DNS1192.168.182.2
# DNS1的值和GATEWAY的值一样即可。注意IPADDR的值192.168.182都是取自虚拟机中虚拟网络编辑器中子网地址的值最后的100是自定义的这个值可以取3~254之间的任意一个数值。 GATEWAY的值是取自虚拟网络编辑器中NAT设置里面的网关的值。
# /etc/sysconfig/network-scripts/ifcfg-ens33文件最终修改好的配置
[rootlocalhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPEEthernet
PROXY_METHODnone
BROWSER_ONLYno
BOOTPROTOstatic
DEFROUTEyes
IPV4_FAILURE_FATALno
IPV6INITyes
IPV6_AUTOCONFyes
IPV6_DEFROUTEyes
IPV6_FAILURE_FATALno
IPV6_ADDR_GEN_MODEstable-privacy
NAMEens33
UUID9a0df9ec-a85f-40bd-9362-ebe134b7a100
DEVICEens33
ONBOOTyes
IPADDR192.168.182.100
GATEWAY192.168.182.2
DNS1192.168.182.2# 重启网卡
[rootlocalhost ~]# service network restart
Restarting network (via systemctl): [ OK ]# 查看ip地址
# 注意每个机器的ip不能重复前三段是一样的第四段一定不能重复因为ip是一个虚拟机的唯一标识
[rootlocalhost ~]# ip addr
1: lo: LOOPBACK,UP,LOWER_UP mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens33: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:9c:86:11 brd ff:ff:ff:ff:ff:ffinet 192.168.182.100/24 brd 192.168.182.255 scope global noprefixroute ens33valid_lft forever preferred_lft foreverinet6 fe80::c8a8:4edb:db7b:af53/64 scope link noprefixroute valid_lft forever preferred_lft forever
Linux临时设置永久设置(hostname)配置
ip不好记所以针对每一台linux机器还有一个主机名hostname
# 通过hostname命令可以直接查看主机名这个是默认的主机名
[rootlocalhost ~]# hostname
localhost.localdomain# 临时设置使用hostname命令直接设置即可
# 这种方式设置的可以立刻生效机器重启之后就失效了。
[rootlocalhost ~]# hostname zsl01
[rootlocalhost ~]# hostname
zsl01# 永久设置直接修改/etc/hostname文件即可
[rootlocalhost ~]# vi /etc/hostname
zsl01
[rootlocalhost ~]# cat /etc/hostname
zsl01
# 重启后生效
[rootlocalhost ~]# reboot -h now
Linux临时关闭永久关闭(防火墙)
# 临时关闭防火墙
[rootlocalhost ~]# systemctl stop firewalld# 通过status确认当前防火墙的状态
[rootlocalhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1)# 永久关闭防火墙
[rootlocalhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.#通过这个list-unit-files来确认一下是否从开机启动项中关闭了
[rootlocalhost ~]# systemctl list-unit-files | grep firewalld
firewalld.service disabled#注意针对不同版本的centos系统关闭防火墙的命令是不一样的centos6和centos7关闭防火墙命令是不一样的
# centos6关闭防火墙
# centos6临时关闭
service iptables stop
# centos6永久关闭
chkconfig iptables off
shell基本语法
shell脚本以.sh结尾
# 创建一个专门放shell脚本的文件夹
[rootlocalhost ~]# mkdir shell
[rootlocalhost ~]# cd shell/
[rootlocalhost shell]# # shell脚本的第一行内容是 #!/bin/bash
# 在shell脚本中的第一行引入/bin/bash或者/bin/sh都是一样的
[rootlocalhost shell]# vi hello.sh
#!/bin/bash
# first command
echo hello world!# 执行shell脚本的标准写法 bash hello.sh
# 还有一种写法是sh hello.sh
[rootlocalhost shell]# bash hello.sh
hello world!# bash对应的是/bin目录下面的bash文件
[rootlocalhost shell]# ll /bin/bash
-rwxr-xr-x. 1 root root 964600 Aug 8 2019 /bin/bash
# sh是一个链接文件指向的也是/bin目录下面的bash文件
[rootlocalhost shell]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 Mar 28 20:54 /bin/sh - bash# 给脚本添加权限
[rootlocalhost shell]# chmod ux hello.sh
[rootlocalhost shell]# ll
total 4
-rwxr--r--. 1 root root 45 Apr 2 16:11 hello.sh# 添加完执行权限之后再执行的时候就可以使用简化形式./hello.sh
# 这里的.表示是当前目录表示在当前目录下执行这个脚本
[rootlocalhost shell]# ./hello.sh
hello world!
# 指定全路径也可以执行
[rootlocalhost shell]# /root/shell/hello.sh
hello world!
# 已经cd到这个文件所在的目录里面了按理说是可以找到那为什么会提示找不到呢
[rootlocalhost shell]# hello.sh
-bash: hello.sh: command not found
#主要原因是这样的因为在这里我们直接指定的文件名称前面没有带任何路径信息那么按照linux的查找规则它会到PATH这个环境变量中指定的路径里面查找这个时候PATH环境变量中都有哪些路径呢
[rootlocalhost shell]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin# 修改这个PATH环境变量
[rootlocalhost shell]# vi /etc/profile
........
.......
.......
export PATH.:$PATH
# 重新加载环境变量配置文件
[rootlocalhost shell]# source /etc/profile
# 再查看PATH变量的值
[rootlocalhost shell]# echo $PATH
.:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
# 再重新执行 hello.sh
[rootlocalhost shell]# hello.sh
hello world!# shell脚本的单步执行便于脚本调试
[rootlocalhost shell]# bash -x hello.sh echo hello world!
hello world!
# 号开头的内容表示是脚本中将要执行的命令下面的内容是执行的结果这样如果脚本中的命令比较多的话看起来是比较清晰的
shell中的变量
shell中的变量不需要声明初始化也不需要指定类型shell是一门弱类型的语言JAVA则是强类型的语言需要提前声明变量并且指定变量类型。shell中变量的命名要求只能使用数字、字母和下划线且不能以数字开头变量赋值是通过进行赋值在变量、等号和值之间不能出现空格执行之后提示-bash: name: command not found的都表示是错误的执行成功的话是没有任何输出的没有反馈就是最好的结果
[rootlocalhost shell]# namezs
[rootlocalhost shell]# name zs
-bash: name: command not found
[rootlocalhost shell]# name zs
-bash: zs: command not found
[rootlocalhost shell]# name2_zs
[rootlocalhost shell]# 2name_zs
-bash: 2name_zs: command not found
[rootlocalhost shell]# name2$zs
-bash: name2$zs: command not found# 打印变量的值通过echo命令
[rootlocalhost shell]# echo $name
zs
# 这两种形式都可以一个是完整写法一个是简化写法
[rootlocalhost shell]# echo ${name}
zs# 如果我们想在变量的结果后面直接无缝拼接其它字符串那就只能使用带花括号的形式
[rootlocalhost shell]# echo $namehehe
[rootlocalhost shell]# echo ${name}hehe
zshehe
# 如果带空格的话就无所谓了
[rootlocalhost shell]# echo $name hehe
zs hehe变量的分类
本地变量环境变量位置变量特殊变量
本地变量 本地变量的格式是VAR_NAMEVALUE这种变量一般用于在shell脚本中定义一些临时变量只对当前shell进程有效关闭shell进程之后就消失了对当前shell进程的子进程和其它shell进程无效。
注意开启一个shell的命令行窗口其实就是开启了一个shell进程克隆一个会话在这两个shell进程中执行echo $name 会发现在克隆的shell进程中获取不到name的值表示本地变量对其它shell进程无效
[rootlocalhost shell]# pstree
-bash: pstree: command not found
# 在线安装pstree安装 psmisc
[rootlocalhost shell]# yum install -y psmisc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.cn99.com* extras: mirrors.cn99.com* updates: mirrors.nju.edu.cn
base | 3.6 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
Resolving Dependencies
-- Running transaction check
--- Package psmisc.x86_64 0:22.20-16.el7 will be installed
-- Finished Dependency ResolutionDependencies ResolvedPackage Arch Version Repository SizeInstalling:psmisc x86_64 22.20-16.el7 base 141 kTransaction SummaryInstall 1 PackageTotal download size: 141 k
Installed size: 475 k
Downloading packages:
psmisc-22.20-16.el7.x86_64.rpm | 141 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transactionInstalling : psmisc-22.20-16.el7.x86_64 1/1 Verifying : psmisc-22.20-16.el7.x86_64 1/1 Installed:psmisc.x86_64 0:22.20-16.el7 Complete!# 再执行pstree
[rootlocalhost shell]# pstree
systemd─┬─NetworkManager───2*[{NetworkManager}]├─VGAuthService├─agetty├─anacron├─auditd───{auditd}├─crond├─dbus-daemon───{dbus-daemon}├─lvmetad├─master─┬─pickup│ └─qmgr├─polkitd───6*[{polkitd}]├─rsyslogd───2*[{rsyslogd}]├─sshd─┬─sshd───bash│ └─sshd───bash───pstree├─systemd-journal├─systemd-logind├─systemd-udevd├─tuned───4*[{tuned}]└─vmtoolsd───{vmtoolsd}# 进入这个shell的子进程中
[rootlocalhost shell]# bash
# 执行exit退出子shell进程
[rootlocalhost shell]# exit
exit
# 主要注意本地变量的生效范围如果我们在一个脚本中定义了一个本地变量那么这个本地变量就只在当前shell脚本中有效。环境变量 环境变量格式export VAR_NAMEVALUE 环境变量格式是在本地变量格式的基础上添加一个export参数 环境变量的这种格式主要用于设置临时环境变量当你关闭当前shell进程之后环境变量就消失了还有就是对子shell进程有效对其它shell进程无效
注意环境变量的生效范围和本地变量是不一样的环境变量对子shell进程是有效的。
# 设置临时环境变量
[rootlocalhost shell]# export age18
[rootlocalhost shell]# echo $age
18# 进入子进程环境变量对子shell进程是有效的
[rootlocalhost shell]# bash
[rootlocalhost shell]# echo $age
18
[rootlocalhost shell]# exit
exit# 设置永久环境变量添加到/etc/profile文件中这样可以保证对所有用户都生效
[rootlocalhost shell]# vi /etc/profile
...........
...........
...........
export age19# 重新加载
[rootlocalhost shell]# source /etc/profile
# 添加好了以后执行echo命令获取age的值
[rootlocalhost shell]# echo $age
19位置变量 动态给shell脚本传递一些参数这个时候就需要用到位置变量类似于$0 $1 2 这样的 2这样的 2这样的后面的数字理论上没有什么限制 它的格式是location.sh abc xyz 位置变量其实相当于java中main函数的args参数可以在shell脚本中动态获取外部参数这样就可以根据外部传的参数动态执行不同的业务逻辑了。
[rootlocalhost shell]# vi location.sh
#!/bin/bash
echo $0
echo $1
echo $2
echo $3# 动态传参
# $0的值是这个脚本的名称
# $1 是脚本后面的第一个参数
# $2是脚本后面的第二个参数
# $3为空是因为脚本后面就只有两个参数
# 理论上来说脚本后面有多少个参数在脚本中就可以通过$和角标获取对应参数的值。
# 多个参数中间使用空格分隔。
[rootlocalhost shell]# sh location.sh abc xyz
location.sh
abc
xyz特殊变量
$? 表示是上一条命令的返回状态码状态码在0~255之间 如果命令执行成功这个返回状态码是0如果失败则是在1~255之间不同的状态码代表着不同的错误信息也就是说正确的道路只有一条失败的道路有很多。
[rootlocalhost shell]# ll
total 8
-rwxr--r--. 1 root root 45 Apr 2 16:11 hello.sh
-rw-r--r--. 1 root root 44 Apr 3 16:23 location.sh
[rootlocalhost shell]# echo $?
0
[rootlocalhost shell]# lk
-bash: lk: command not found
[rootlocalhost shell]# echo $?
127# 状态码 描述
# 0 命令成功结束
# 1 通用未知错误
# 2 误用Shell命令
# 126 命令不可执行
# 127 没找到命令
# 128 无效退出参数
# 128x Linux信号x的严重错误
# 130 命令通过CtrlC控制码越界
# 255 退出码越界
$# 它表示的是shell脚本所有参数的个数
# $# 它表示的是shell脚本所有参数的个数
[rootlocalhost shell]# vi paramnum.sh
#!/bin/bash
echo $#
[rootlocalhost shell]# sh paramnum.sh a b c
3
[rootlocalhost shell]# sh paramnum.sh a b c d
4变量和引号的特殊使用
# 单引号单引号不解析变量
[rootlocalhost shell]# namejack
[rootlocalhost shell]# echo $name
$name# 双引号双引号解析变量
[rootlocalhost shell]# namejack
[rootlocalhost shell]# echo $name
jack# 反引号在键盘左上角esc下面的那个键在英文输入法模式下可以打出来
# 反引号是执行并引用命令的执行结果
[rootlocalhost shell]# namepwd
[rootlocalhost shell]# echo $name
/root/shell
# 反引号还有另一种写法$() 他们的效果一致
[rootlocalhost shell]# echo $($name)
/root/shell# 在变量的值外面套一层引号
# 动态拼接sql的时候会用到
[rootlocalhost shell]# echo $name
pwdshell中的循环和判断
for循环
for((i0;i10;i))
do
循环体...
done[rootlocalhost shell]# vi for1.sh
#!/bin/bash
for((i0;i10;i))
do
echo $i
done[rootlocalhost shell]# sh for1.sh
0
1
2
3
4
5
6
7
8
9for i in 1 2 3
do
循环体...
done[rootlocalhost shell]# vi for2.sh
#!/bin/bash
for i in 1 2 3
do
echo $i
done[rootlocalhost shell]# sh for2.sh
1
2
3while循环 while循环主要适用于循环次数未知或不便于使用for直接生成较大列表时
while 测试条件
do
循环体...
done测试条件 支持两种格式 test EXPR 或者 [ EXPR ] 第二种形式里面中括号和表达式之间的空格不能少这个EXPR表达式里面写的就是具体的比较逻辑。
整型测试-gt(大于)、-lt(小于)、-ge(大于等于)、-le(小于等于)、-eq(等于)、-ne(不等于) 针对整型数据需要使用-gt、-lt这样的写法而不是大于号或小于号。
字符串数据如果判断两个字符串相等使用号这里的号不是赋值的意思不等于就使用!字符串测试(等于)、!(不等于)
[rootlocalhost shell]# vi while1.sh
#!/bin/bash
while test 2 -gt 1
do
echo yes
sleep 1
done[rootlocalhost shell]# vi while1.sh
#!/bin/bash
while test 2 -lt 1
do
echo yes
sleep 1
done
[rootlocalhost shell]# sh while1.sh
#执行脚本按ctrlc可强制退出程序# 中括号和里面的表达式之间一定要有空格否则就报错
[rootlocalhost shell]# vi while2.sh
#!/bin/bash
while [ 2 -gt 1 ]
do
echo yes
sleep 1
done
[rootlocalhost shell]# sh while2.sh
yes
yes
yes
...if判断 单分支 双分支 多分支
单分支
if 测试条件
then选择分支
fi# 动态获取一个参数在测试条件中动态判断
[rootlocalhost shell]# vi if1.sh
#!/bin/bash
flag$1
if [ $flag -eq 1 ]
then
echo one
fi
# 如果脚本后面没有传参数的话执行程序会抱错程序不够健壮
[rootlocalhost shell]# sh if1.sh
if1.sh: line 3: [: -eq: unary operator expected# 设计先判断脚本后面参数的个数如果参数个数不够直接退出就行在这里使用exit可以退出程序并且可以在程序后面返回一个状态码这个状态码其实就是我们之前使用$?获取到的状态码如果这个程序不传任何参数就会执行exit 100结束程序并且返回状态码100
[rootlocalhost shell]# vi if1.sh
#!/bin/bash
if [ $# -lt 1 ]
then
echo not found param
exit 100
fiflag$1
if [ $flag -eq 1 ]
then
echo one
fi
[rootlocalhost shell]# sh if1.sh
not found param
[rootlocalhost shell]# echo $?
100双分支
if 测试条件
then选择分支1
else选择分支2
fi
[rootlocalhost shell]# vi if2.sh
#!/bin/bash
if [ $# -lt 1 ]
thenecho not found paramexit 100
fiflag$1
if [ $flag -eq 1 ]
thenecho one
elseecho not support
fi多分支
if 测试条件1
then选择分支1
elif 测试条件2
then选择分支2...
else选择分支n
fi[rootlocalhost shell]# vi if3.sh
#!/bin/bash
if [ $# -lt 1 ]
thenecho not found paramexit 100
fiflag$1
if [ $flag -eq 1 ]
thenecho one
elif [ $flag -eq 2 ]
thenecho two
elif [ $flag -eq 3 ]
thenecho three
elseecho not support
fi注意如果在windows中使用notepad开发shell脚本需要将此参数设置为UNIX。 否则在windows中开发的脚本直接上传到linux中执行会报错。
shell扩展
# 后台执行在脚本后面添加一个即可
[rootlocalhost shell]# sh while2.sh
[1]2228# 当我们把这个窗口关闭以后会发现之前放到后台运行的shell脚本也停止了我们是希望这个脚本能够一直在后台运行的
[rootlocalhost ~]# ps -ef|grep while2
root 2325 2306 0 20:48 pts/2 00:00:00 grep --colorauto while2# 原理就是默认情况下当我们关闭shell窗口时shell窗口会向之前通过它启动的所有shell
# 脚本发送停止信号当我们加上nohup之后就会阻断这个停止信号的发送所以已经放到后台的shell脚本就不会停止了。
# 保证关闭shell窗口而不影响放到后台的shell脚本执行
[rootlocalhost shell]# nohup sh while2.sh
[1]2326
nohup: ignoring input and appending output to ‘nohup.out’
# 注意使用nohup之后脚本输出的信息默认都会存储到当前目录下的一个nohup.out日志文件中后期想要排查脚本的执行情况的话就可以看这个日志文件。# 停止这个shell脚本使用kill
[rootlocalhost shell]# ps -ef|grep while2
root 2326 2306 0 20:51 pts/2 00:00:00 sh while2.sh
root 3515 2306 0 21:11 pts/2 00:00:00 grep --colorauto while2
[rootlocalhost shell]# kill 2326
[rootlocalhost shell]# ps -ef|grep while2
root 3525 2306 0 21:11 pts/2 00:00:00 grep --colorauto while2标准输出表示是命令或者程序输出的正常信息标准错误输出表示是命令或者程序输出的错误信息重定向
# 将输出内容重定向至文本
# 重复执行此命令 会覆盖掉之前的内容
# 标准输出 使用参数1
# 标准错误输出 使用使用2
[rootlocalhost shell]# ll 1 a.txt
[rootlocalhost shell]# more a.txt
total 52
-rw-r--r--. 1 root root 0 Apr 3 21:39 a.txt
-rw-r--r--. 1 root root 48 Apr 3 17:32 for1.sh
-rw-r--r--. 1 root root 43 Apr 3 17:40 for2.sh
-rwxr--r--. 1 root root 45 Apr 2 16:11 hello.sh
-rw-r--r--. 1 root root 121 Apr 3 18:30 if1.sh
-rw-r--r--. 1 root root 147 Apr 3 18:30 if2.sh
-rw-r--r--. 1 root root 227 Apr 3 18:34 if3.sh
-rw-r--r--. 1 root root 44 Apr 3 16:23 location.sh
-rw-------. 1 root root 4692 Apr 3 21:11 nohup.out
-rw-r--r--. 1 root root 20 Apr 3 16:48 paramnum.sh
-rw-r--r--. 1 root root 56 Apr 3 17:59 while1.sh
-rw-r--r--. 1 root root 55 Apr 3 18:01 while2.sh
-rw-r--r--. 1 root root 61 Apr 3 18:03 while3.sh
# 标准错误输出 使用使用2
[rootlocalhost shell]# lk 2 b.txt
[rootlocalhost shell]# more b.txt
-bash: lk: command not found# 追加的话需要使用
[rootlocalhost shell]# ll 1 a.txt
[rootlocalhost shell]# more a.txt
total 52
-rw-r--r--. 1 root root 0 Apr 3 21:39 a.txt
-rw-r--r--. 1 root root 48 Apr 3 17:32 for1.sh
-rw-r--r--. 1 root root 43 Apr 3 17:40 for2.sh
-rwxr--r--. 1 root root 45 Apr 2 16:11 hello.sh
-rw-r--r--. 1 root root 121 Apr 3 18:30 if1.sh
-rw-r--r--. 1 root root 147 Apr 3 18:30 if2.sh
-rw-r--r--. 1 root root 227 Apr 3 18:34 if3.sh
-rw-r--r--. 1 root root 44 Apr 3 16:23 location.sh
-rw-------. 1 root root 4692 Apr 3 21:11 nohup.out
-rw-r--r--. 1 root root 20 Apr 3 16:48 paramnum.sh
-rw-r--r--. 1 root root 56 Apr 3 17:59 while1.sh
-rw-r--r--. 1 root root 55 Apr 3 18:01 while2.sh
-rw-r--r--. 1 root root 61 Apr 3 18:03 while3.sh
total 56
-rw-r--r--. 1 root root 671 Apr 3 21:39 a.txt
-rw-r--r--. 1 root root 48 Apr 3 17:32 for1.sh
-rw-r--r--. 1 root root 43 Apr 3 17:40 for2.sh
-rwxr--r--. 1 root root 45 Apr 2 16:11 hello.sh
-rw-r--r--. 1 root root 121 Apr 3 18:30 if1.sh
-rw-r--r--. 1 root root 147 Apr 3 18:30 if2.sh
-rw-r--r--. 1 root root 227 Apr 3 18:34 if3.sh
-rw-r--r--. 1 root root 44 Apr 3 16:23 location.sh
-rw-------. 1 root root 4692 Apr 3 21:11 nohup.out
-rw-r--r--. 1 root root 20 Apr 3 16:48 paramnum.sh
-rw-r--r--. 1 root root 56 Apr 3 17:59 while1.sh
-rw-r--r--. 1 root root 55 Apr 3 18:01 while2.sh
-rw-r--r--. 1 root root 61 Apr 3 18:03 while3.sh
# nohup和 可以让程序一直在后台运行
# /dev/null 是linux中的黑洞任何数据扔进去都找不到了
# /dev/null 把标准输出重定向到黑洞中表示脚本的输出信息不需要存储
# 21 表示是把标准错误输出重定向到标准输出中
nohup hello.sh /dev/null 21 # 最终这条命令的意思就是把脚本放在后台一直运行并且把脚本的所有输出都扔到黑洞里面定时器crontab
针对周期性需要被执行的命令完全可以选择使用定时器定时调度执行。 crontab类似于java中的timer定时器它可以作用于周期性被执行的命令
crontab的格式是这样的* * * * * user-name command
*分钟(0-59)
*小时(0-23)
*一个月中的第几天(1-31)
*月份(1-12)
*星期几(0-6) (星期天为0)
user-name用户名用哪个用户执行
command具体需要指定的命令这条配置需要添加到crontab服务对应的文件中在配置之前需要先确认crontab的服务是否正常
查看crontab服务状态systemctl status crond# 查看crontab的服务状态
[rootlocalhost shell]# systemctl status crond
● crond.service - Command SchedulerLoaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)Active: active (running) since Fri 2020-04-03 22:16:18 CST; 1min 53s agoMain PID: 3583 (crond)CGroup: /system.slice/crond.service└─3583 /usr/sbin/crond -nApr 03 22:16:18 localhost systemd[1]: Started Command Scheduler.
Apr 03 22:16:18 localhost crond[3583]: (CRON) INFO (RANDOM_DELAY will be sc...)
Apr 03 22:16:18 localhost crond[3583]: (CRON) INFO (running with inotify su...)
Apr 03 22:16:18 localhost crond[3583]: (CRON) INFO (reboot jobs will be ru...)
Hint: Some lines were ellipsized, use -l to show in full.# 启动crontab的服务
systemctl start crond
# 停止crontab的服务
systemctl stop crond# crontab服务对应的配置文件了/etc/crontab
[rootlocalhost shell]# vi /etc/crontab SHELL/bin/bash
PATH/sbin:/bin:/usr/sbin:/usr/bin
MAILTOroot# 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) (Sunday0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# 每隔1分钟打印一次当前时间时间格式为年月日 时分秒
[rootlocalhost shell]# vi showTime.sh
#!/bin/bash
showTimedate %Y-%m-%d %H:%M:%S
echo $showTime
# 然后在/etc/crontab文件中配置
* * * * * root sh /root/shell/showTime.sh
# 把脚本执行的结果重定向到showTime.log文件中需要使用追加重定向
* * * * * root sh /root/shell/showTime.sh /root/shell/showTime.log
# 可以使用tail -f 监控
[rootlocalhost shell]# tail -f /root/shell/showTime.log
2026-04-06 21:14:01
2026-04-06 21:15:01
2026-04-06 21:16:01
# crontab的日志在/var/log/cron文件中使用tail -f命令实时监控
[rootlocalhost shell]# tail -f /var/log/cron
.........
Apr 6 21:14:01 localhost CROND[1577]: (root) CMD (sh /root/shell/showTime.sh /root/shell/showTime.log)
Apr 6 21:15:01 localhost CROND[1584]: (root) CMD (sh /root/shell/showTime.sh /root/shell/showTime.log)
Apr 6 21:16:01 localhost CROND[1591]: (root) CMD (sh /root/shell/showTime.sh /root/shell/showTime.log)
Apr 6 21:17:01 localhost CROND[1597]: (root) CMD (sh /root/shell/showTime.sh /root/shell/showTime.log)
Apr 6 21:18:01 localhost CROND[1603]: (root) CMD (sh /root/shell/showTime.sh /root/shell/showTime.log)# 设置任务每7分钟执行一次
# 注意如果设置的7分钟执行一次那么就会在每个小时的第0、7、14、21、28…分钟执行而不是根据你配置好的时候往后推这里的间隔时间是7分钟7分钟无法被60整除它最后会在第56分钟执行一次下一次执行就是下一个小时的0分开始执行了所以针对这种除不尽的到下一小时就开始重新计算了不累计。
......