系统管理
目录
1.系统基础管理和维护
2.系统管理的常用命令
0.常用命令:
显示信息:echo
删除命令:rm -rf 文件名
创建文件:touch 文件名
创建目录:mkdir 名称
编辑文件:vim 文件名
查看当前路径:pwd
1.系统基础管理和维护。
1.1 tab补全
tab 一次;输入的内容能够唯一标识,直接跳出命令。
输入的内容不能唯一标识,再 tab,显示出所有匹配的命令
tab命令补全的原理:vi ,系统根据环境变量$PATH 去文件下寻找命令。
路径补全:
1.2 shell 命令操作
ctrl + a :跳到一行最前面
ctrl + e:跳到一行最后面
ctrl +k:删除光标后的内容
ctrl+u:删除光标前的内容
ctrl+l:清屏
ctrl+r:搜索历史命令。
ctrl+c:停止当前命令
Ctrl :退出
1.3 vim 操作
编辑模式:i:在当前光标下插入
O:上一行插入
o:下一行插入
命令行模式::q:退出
q!:强制退出
w:保存
w!:强制保存
wq:保存退出
x:保存退出
set nu:设行号
1.4 history
echo $PATH:显示PATH环境变量
! 10 ! +历史命令的列表行数
!$ 执行最后一次的历史命令
help history 查看帮助
history -w 把历史命令列表同步到历史命令文件中
echo $HISTFILE 查看历史命令文件位置。
(1)如何设置 历史命令的保存数量
输入
echo $HISTSIZE
vim /etc/profile
将最下面的HISTSIZE改为1
然后保存退出再重新执行文件输入:source /etc/profile
再输入history就只会出现10条记录
(2)如何设置历史命令的时间戳,和使用用户
输入:
vim /etc/profile
在最底下输入:
export HISTTIMEFORMAT="%F %T whoami
"
然后保存退出
再重新执行文件输入:source /etc/profile
再输入history就会出现时间戳和使用用户
1.5 alias
[root@localhost ~]# alias yang=“cat /etc/sysconfig/network-scripts/ifcfg-eth0” #定义别名
[root@localhost ~]# unalias net #取消别名
[root@localhost ~]# \net #跳过别名
让别名永久生效
输入:
[root@localhost ~]# vim /etc/bashrc
在最底下输入:
alias yang=“cat /etc/sysconfig/network-scripts/ifcfg-eth0”
然后
:wq
再输入
[root@localhost ~]# source /etc/bashrc
1.6 变量
环境变量:正对于当前的shell下的所有进程及子进程都生效
(1)变量的创建和删除
例如创建变量x
set x=10
echo $x
x=“123456” #当前shell进程,子进程。
echo $x
删除变量x
unset x
echo $x
拓展1
编辑文件test1.sh:
#!/bin/bash
who=whoami
#全局变量,作用于对当前文件
test(){
IP=‘192.168.254.125’ #局部变量,作用于一行代码,或者代码块
#ping $IP
echo $who
}
#test
ping $IP
然后执行文件:sh test.sh ,能ping通就表示成功
1.7 标准输入,输出
标准输出1 错误输出2
> filename 标准输出到文件
[root@localhost ~]# ls >file1
2> eername 错误输出到文件
[root@localhost ~]# mysql 2>err
>filename 2>eername 将标准输出到filename文件,错误输出到errname
[root@localhost ~]# ll anaconda-ks.cfg anaconda-ks1.cfg > ceu 2>err
将标准输出和错误输出放在同一个文件夹里
[root@localhost ~]# ll anaconda-ks.cfg anaconda-ks1.cfg &>file4
[root@localhost ~]# ll anaconda-ks.cfg anaconda-ks1.cfg >file5 2>&1
标准输入到文件file3
[root@localhost ~]# cat > file3 <<EOF
123
abc
EOF
拓展2
编辑一个脚本文件 :vim ping.sh
$?判断上一条命令执行是否成功,若成功,返回0;失败,返回非0;
#!/bin/bash
IP=‘192.168.42.145’
ping -c1 $IP &>/dev/null
if [ $? -eq 0 ]
then
echo ‘tong,’
fi
输入sh ping.sh 显示tong…
1.8特殊符号,通配符
“|”:把上个命令的执行结果交给了下一个命令继续处理
(管道就是将前一个命令的:输出作为后一个命令的:输入)
把ll命令里面的err文件过滤出来
[root@localhost ~]# ll |grep err
[root@localhost ~]# ip a |grep inet
“tee”把过滤的结果打印到文件中
"…"代表上级目录
[root@localhost test]# ll …/
"."当前目录
[root@localhost test]# ll ./
“;”多条命令的分割
[root@localhost test]# a=abc;echo $a
``把内容还原成命令
[root@localhost test]# a=ls
;echo $a
“*” 表示匹配所有
[root@localhost ~]# “?” 匹配单个任意符
"?"匹配单个任意符
[root@localhost ~]# ll te?
2. 系统管理的常用命令
2.1 ls
ls -a 显示所有的隐藏文件及目录,ls -A 显示影藏文件
ls -l ===> ll
ls : 显示是链接文件及链接路径,ls -L,显示的是链接路径下的真实文件
2.2 pwd
相对路径,绝对路径,pwd
用绝对路径,表示index.html ;/root/aaa/app/index.html
用相对路径,表示index.html ;app/index.html
pwd 显示的是当前路径,pwd -P 显示 链接文件的真实路径
[root@localhost ~]# cd ~ 切换到用户的家目录
[root@localhost var]# cd - 切换到上一次文件所在的路径。
[root@localhost mail]# cd …/ 切换到上曾目录
[root@localhost mail]# cd …/…/ 切换到上上层目录
2.3 data
时钟显示格式的拼接:
[root@localhost ~]# date "+%Y-%m-%d %H:%M:%S 明确每个参数的含义。
修改系统时间
[root@localhost ~]# date -s 17:54:30
显示时区:
[root@localhost ~]# date +%Z
拓展3
编写一个脚本文件:time.sh
输入
#!/bin/bash
time=date "+%H"
echo $time.log
保存退出,然后执行:sh time.sh
j结果是显示当前小时
2.4 wget
[root@localhost ~]# yum install wget 安装wget
Index of /centos/7.6.1810/extras/x86_64/Packages/
[root@localhost ~]# wget http://mirrors.sohu.com/centos/7.6.1810/extras/x86_64/Packages/centos-release-scl-2-3.el7.centos.noarch.rpm
下载搜狐上的CentOS7.6.1810,就可以连接外网
[root@localhost src]# wget -b -c http://mirrors.sohu.com/centos/7.6.1810/extras/x86_64/Packages/ansible-2.4.2.0-2.el7.noarch.rpm /usr/local/src/
[root@localhost src]# wget -P /usr/local/src/ http://mirrors.sohu.com/centos/7.6.1810/extras/x86_64/Packages/ansible-2.4.2.0-2.el7.noarch.rpm
-b 后台运行
-c端点续传
-P 指定下载路径
如果第一次安装不成功,则输入vi /etc/resolv.conf,再写入nameserver 192.168.1.2
#网关
2.5 uname
[root@localhost src]# uname -a 查看系统内核信息
[root@localhost src]# uname -r
2.6 free
[root@localhost src]# free
[root@localhost src]# free -m
[root@localhost src]# free -h
mem:内存
swap: 在磁盘上格式化的一个和内存格式相同的分区
buff:数据读的缓存空间
cache:数据写的缓存空间
reboot:重启系统