002 bash的特性、命令行快捷键、历史记录、设置别名、关机重启及注销、显示IP地址的命令
bash的特性
table补全
1、点击1下table补全
2、点击2下table显示出全部的命令
3、选项补全:需要安装下述语句
yum install -y bash-completion
4、参数补全
命令行快捷键
主要是通过x-shell或者secureCRT实现的
- : 上一步
~ :
# 创建一个用户
[root@localhost ~]# useradd oldboy
# 切换用户
[root@localhost ~]# su - oldboy
ctrl+c #取消当前的操作
ctrl+d #退出当前的登录,相当于logout
ctrl+l #清屏命令=clear命令
ctrl+a #到当前编辑行的行首
ctrl+e #到当前编辑行的行末
ctrl+u #剪切光标前行所有的内容
ctrl+y #粘贴
ctrl+k #剪切光标后的所有内容
ctrl+r #搜索最近执行过的指令
ctrl+s #锁屏但是命令是有效的,你可以偷偷执行rm -f/,删库跑路
ctrl+q #解锁
ctrl+<-(左方向键) #移动一组字符串,以空格为分隔符
ctrl+w #删除当前光标向前一组字符串,以空格为分隔符
ctrl+z #杀死当前进程
以感叹号开头的:
!! #执行上一条的命令,用的少,还不如用上方向键
!ls #执行以最近ls开头的一组命令
以ESC开头的:
ESC+. #获取上一条指令的最后的参数
!$ 也是获取上一条命令最后一个参数
历史记录
history #执行历史命令
history | grep ip #通过结合管道,过滤出执行过的IP的命令
history -d 2 #删除历史ID为2的命令
history -w #将当前执行过的历史命令写入默认的文件中
history -c #清除所有的历史记录,当你进行密码操作的时候操作
>.bash_histroy #清空文件
ps:作为运维人员,离开座位的时候,就锁屏
!4 #执行历史ID为4的指令
设置别名
查看设置别名
[root@localhost ~]# 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@localhost ~]# alias net_test='ping baidu.com'
[root@localhost ~]# 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 net_test='ping baidu.com'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
测试别名
[root@localhost ~]# net_test
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=128 time=28.3 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=128 time=28.2 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=128 time=28.3 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=128 time=28.3 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=5 ttl=128 time=29.0 ms
取消别名
[root@localhost ~]# unalias net_test
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'
\net_test
#临时取消别名,\代表转义
别名永久生效
想要让别名永久生效,将其写入配置文件中去
[root@localhost ~]# ls /etc/bashrc #针对所有用户生效
/etc/bashrc
[root@localhost ~]# ls ~/.bashrc #针对当前用户生效
/root/.bashrc
[root@localhost ~]# echo "alias network='cat /etc/sysconfig/network-scripts/ifcfg-ens33'" >>/etc/bashrc
#将别名写入ens33配置文件中
[root@localhost ~]# source /etc/bashrc
#重新加载文件配置,让其生效
[root@localhost ~]# network
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=90dc83f7-329b-42ee-ab18-1bd99a4bc61e
DEVICE=ens33
ONBOOT=no
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
NAME=ens33
DEVICE=ens33
ONBOOT=yes
取消别名永久生效
[root@localhost ~]# sed -i '/network/d' /etc/bashrc
[root@localhost ~]# logout
#需要退出登录
[root@localhost ~]# network
-bash: network: command not found
修改主机名
# 方式一:
[root@local-work ~]# hostnamectl set-hostname local-work
# 需要重新登录或者执行一个解析器
# 方式二:
[root@local-work ~]# echo "string" > /etc/hostname
[root@local-work ~]# cat /etc/hostname
string
# 直接修改文件,不会立即生效,需要重启。
系统时间
[root@string ~]# date
Fri Mar 5 09:50:27 CST 2021
[root@string ~]# date +%Y-%m-%d
2021-03-05
# 格式化时间
[root@string ~]# # 09:58:30
[root@string ~]# date +%H:%M:%S
10:01:44
[root@string ~]# date +%Y-%m-%d_%H:%M:%S
2021-03-05_10:02:05
# 设置时间
[root@string ~]# date -s "2029-03-05 10:02:44"
Mon Mar 5 10:02:44 CST 2029
[root@string ~]# date +%Y-%m-%d_%H:%M:%S
2029-03-05_10:03:03
# 同步互联网时间
[root@string ~]# yum install -y ntpdate
[root@string ~]# ntpdate ntp.aliyun.com
# 系统硬件时钟
[root@string ~]# hwclock -r
Fri 05 Mar 2021 10:10:40 AM CST -0.271305 seconds
# 系统软件时钟
[root@string ~]# date
Mon Mar 5 10:03:30 CST 2029
# 获取硬件时间
[root@string ~]# hwclock -r
Fri 05 Mar 2021 10:13:19 AM CST -0.473926 seconds
# 将硬件时间同步到系统时间
[root@string ~]# hwclock -r
Fri 05 Mar 2021 10:13:19 AM CST -0.473926 seconds
[root@string ~]# date
Mon Mar 5 10:06:20 CST 2029
[root@string ~]# hwclock -s
[root@string ~]# date
Fri Mar 5 10:14:19 CST 2021
# 查看时区状态
[root@string ~]# timedatectl status
Local time: Fri 2021-03-05 10:26:39 CST
Universal time: Fri 2021-03-05 02:26:39 UTC
RTC time: Fri 2021-03-05 02:26:33
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
# 设置时区
[root@string ~]# timedatectl set-timezone Asia/Shanghai
关机重启及注销命令
同步时间:
yun install -y ntpdate
ntpdate ntp.aliyun.com
关机:
shutdown -h 10 #10是以分钟为节点的
shutdown -c #取消你的关机操作
立即关机
shutdown -h now
shutdown -h 0
#都表示立刻关机
shutdown -h 11:00 #定时关机
init 0 #立刻关机 通过系统的运行级别
poweroff #立即关机
halt -p #立刻关机,不加-p只关闭系统
重启的相关命令
shutdown -r 10 #10分钟后立刻重启
shutdown -r 0 #立即重启
shutdown -r now #立即重启
shutdown -r 11:00 #11:00重启
reboot #系统推荐的重启操作
注销的相关命令
logout #退出当前登录的用户 只能退出登录式shell,不能退出非登陆式shell
ctrl+d #快捷键 退出当前登录的用户
exit #退出当前登录的用户 能退出登录式shell,也能退出非登陆式shell,主要用于脚本退出
显示IP地址的命令
#获取所有网卡的ip地址
ip a #
ip address
#单独显示某块网卡的ip地址
ip a s eth0 #s=show的缩写
ifconfig #是Centos6默认的,获取所有
yum install -y net-tools #其他的需要使用的话需要安装
ifconfig eyh0 #获取一个
hostname -i #显示的IPV6
hostname -I #显示的IPV4
curl ifconfig.me
curl cip.cc #获取公网IP地址
#查看DNS信息
[root@test1 data]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 114.114.114.114
#查看dns解析
[root@test1 data]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#查看系统版本
[root@test1 data]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)