1. Bash的特性
1. tab补全
#命令补全
[root@qls ~]# user #命令补全
useradd userdel usermod usernetctl users
[root@qls ~]# s
Display all 195 possibilities? (y or n) #以s开头的命令共有195个,是否显示 y 显示 n 不显示
#选项补全
yum install -y bash-completion #需要安装补全软件
[root@qls ~]# ls --
--all --indicator-style=
--almost-all --inode
--author --kibibytes
--block-size= --lcontext
[root@qls ~]# systemctl st
start status stop
#参数补全
文件或者目录的路径补全
[root@qls ~]# ls /etc/sysconfig/network-scripts/ifcfg-
ifcfg-eth0 ifcfg-lo
2. 命令行快捷键 主要通过xshell secureCRT 实现的
Ctrl键 + c #取消当前的操作 cancel
Ctrl键 + d #退出当前的登陆的用户
Ctrl键 + l #清除屏幕上的内容 clear
Ctrl键 + a #将光标移动到当前行的行首
Ctrl键 + e #将光标移动到当前行的行尾
Ctrl键 + u #将当前光标到行首的内容进行剪切
Ctrl键 + y #粘贴当前粘贴板上面的内容
Ctrl键 + k #将当前光标到行尾对的内容进行剪切
delete #从前往后删除一个字符
Ctrl键 + r #搜索最近一次包含某个命令的指令
Ctrl键 + s #锁屏 危险
Ctrl键 + q #解锁
Ctrl键 + ← #将当前光标向左移动一组字符串,以空格为分隔符
Ctrl键 + → #将当前光标向右移动一组字符串,以空格为分隔符
Ctrl键 + w #删除当前光标向前一组字符串,以空格为分隔符
以!开头的
!! #执行上一条所执行的指令
!ls #执行最近一次以ls开头的指令
Esc键 + . #获取上一条指令的最后的参数 或者内容
3. 历史记录
[root@qls ~]# history
1 ip a
2 ip a
3 logout
4 exit
5 man ls
[root@qls ~]# history | grep ip
1 ip a
2 ip a
选项:
[root@qls ~]# history -d 2 #删除历史ID为2的命令
[root@qls ~]# history -w #将当前的历史记录写入到默认的文件中 ~/.bash_history
[root@qls ~]# history -c #清空历史记录
[root@qls ~]# history
1 history
!4 #执行历史ID为4的指令
4. 别名
[root@qls ~]# 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'
#设置别名 临时生效,退出即失效
[root@qls ~]# alias test='ping baidu.com'
#查看是否设置成功
[root@qls ~]# 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 test='ping baidu.com'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
#测试别名是否生效
[root@qls ~]# test
#取消别名
[root@qls ~]# unalias test
[root@qls ~]# alias #查看别名是否取消
#让别名永久生效
把设置别名的命令写入到下面的文件中
[root@qls ~]# ls /etc/bashrc
/etc/bashrc
[root@qls ~]# ls ~/.bashrc
/root/.bashrc
[root@qls ~]# alias network='cat /etc/sysconfig/network-scripts/ifcfg-eth0'
[root@qls ~]# network #查看别名是否生效
#临时取消别名
#取消特殊字符的特殊含义 取消转义 撬棍 ,是临时的
[root@qls ~]#
etwork
-bash: network: command not found
#实现永久生效
[root@qls ~]# echo "alias network='cat /etc/sysconfig/network-scripts/ifcfg-eth0'" >>/etc/bashrc
#将其配置文件重新加载生效
[root@qls ~]# source /etc/bashrc
#退出重新登录测试,看看是否依然生效
#取消永久生效
[root@qls ~]# sed -i '/network/d' /etc/bashrc
#退出重新登录
2. 关机重启即注销命令
#更新时间
[root@qls ~]# yum install -y ntpdate
[root@qls ~]# ntpdate ntp.aliyun.com
#关机
[root@qls ~]# shutdown -h 10 #10分钟之后关机
[root@qls ~]# shutdown -h now #立刻关机
[root@qls ~]# shutdown -h 0 #立刻关机
[root@qls ~]# shutdown -h 11:00 #定时关机
[root@qls ~]# poweroff #立刻关机
[root@qls ~]# halt -p #立刻关机
[root@qls ~]# init 0 #立刻关机 通过系统的运行级别
#重启
[root@qls ~]# shutdown -r 10 #10分钟之后进行重启
[root@qls ~]# shutdown -r 0 #立刻重启
[root@qls ~]# shutdown -r now #立刻重启
[root@qls ~]# shutdown -r 11:00 #定时重启
[root@qls ~]# reboot #重启
#注销 退出当前登录的用户
[root@qls ~]# logout #退出当前登录的用户 只能退出登录式Shell,不能退出非登录式Shell
Ctrl键 + d #快捷键 退出当前登录的用户
[root@qls ~]# exit #退出当前登录的用户 既能退出登录式也能退出非登录式 主要用于脚本退出
[root@qls ~]# bash
[root@qls ~]# logout
bash: logout: not login shell: use `exit'
[root@qls ~]# exit
exit
3. 显示IP地址的命令
Centos7默认显示IP地址的命令
#获取所有网卡的IP地址
[root@qls ~]# ip a #ip a==ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#单独查看某个网卡的IP地址
[root@qls ~]# ip a s eth0 #ip a s eth0==ip address show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:eb:ea:8d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::3310:9d15:9ee4:43e8/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ifconfig是Centos6默认显示IP地址的命令
[root@qls ~]# yum install -y net-tools #安装ifconfig命令
#所有的网卡的IP地址信息
[root@qls ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet)
RX packets 669 bytes 367883 (359.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 376 bytes 48482 (47.3 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 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
#获取某一个网卡的信息
[root@qls ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.100 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::3310:9d15:9ee4:43e8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:eb:ea:8d txqueuelen 1000 (Ethernet)
RX packets 699 bytes 370373 (361.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 394 bytes 51180 (49.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
hostname
#显示所有的IP地址,会显示IPv6的地址
[root@qls ~]# hostname -i
fe80::3310:9d15:9ee4:43e8%eth0 10.0.0.100
#只显示IPv4地址
[root@qls ~]# hostname -I
10.0.0.100
#如何获取到公网地址
[root@qls ~]# curl ifconfig.me
139.226.13.64
[root@qls ~]# curl cip.cc
IP : 139.226.13.64
地址 : 中国 上海
运营商 : 联通
数据二 : 上海市 | 联通
数据三 : 中国上海上海 | 联通
URL : http://www.cip.cc/139.226.13.64
4. 总结
1. Bash特性
tab键补全
1. 命令补全
2. 选项补全 需要安装制定的软件包 bash-completion
3. 参数补全 路径补全
快捷键
历史记录 history
选项:
-d #删除制定ID的历史记录
-c #清除所有的历史记录
-w #将当前的历史记录保存到默认的文件中 ~/.bash_history
!num #执行ID对应的指令
别名 alias
设置别名
取消别名
临时取消别名
永久生效别名
系统中默认的别名
2. 关机重启及注销命令
关机
shutdown -h
Poweroff
init 0
halt -p
重启
shutdown -r
reboot
init 6
注销
logout #退出当前登录的用户 只能退出登录式shell
Ctrl键 + d #退出当前登录的用户 logout
exit #退出当前登录的用户 主要应用于脚本中 脚本退出使用这个指令
3. 显示IP地址的指令
ip
ip a ==== ip address
ip a s eth0 ===== ip address show eth0
ifconfig
ifconfig
ifconfig eth0
hostname
hostname -i
hostname -I