高级Linux运维工程师养成记-Linux基础入门教程
高级Linux运维工程师养成记-Linux基础入门
管理员如果想要管理Linux,首先需要使用账号登录系统
===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
=============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
在Linux系统安装完成后,至少有两种用户,一种是管理员账号,账户名默认是root,id是0,如何辨别用户是管理员?即id为0,而不是根据用户名判断。
查看当前用户的ID,管理员用户默认是0
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# id -u
0
一种是普通人员账号,例如之前增加的guanglei就是普通用户,普通账户从1000开始编号。
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
guanglei@ubuntu20:~$ id -u
1000
因为root账号权限过大,在生产环境中一般都是使用普通用户登录系统,然后切换到root用户来完成日常运维任务。
在Linux运维学习阶段推荐使用root用户操作Linux,因为可能在执行某些名命令时普通用户没有权限执行。
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
一台服务器可以提供多个终端来登录后使用系统,每个终端有不同的类型 。
如果想要查看当前终端的编号,可以使用tty命令查看,虚拟机中CentOS8可以使用Ctrl+Alt+F(1-6)来切换终端,其中Ctrl+Alt+F2切换的是图形终端,其他的都是字符终端,这种切换只是临时切换,如果想要在图形终端和字符终端切换,可以使用init 命令切换模式,在切换之前可以使用runlevel查看当前系统的运行级别,3 表示字符模式,此时不会开启图形界面,5表示图形模式,图形模式占用内存,生产环境的服务器一般不安装图形,可用使用free -h
命令 查看内存情况。
可以使用init3命令从图形界面模式切换到字符界面模式
init 3切换成字符模式。
init 5 切换成图形模式。
有些程序(例如Firefox)在字符模式下无法使用,只能在图形界面使用。
/dev/tty4 表示虚拟终端设备文件
/dev/pts/0表示第一个伪终端设备文件,终端的编号从0开始,然后依次递增,xshell上开启的终端也是伪终端。
而在CentOS8.2系统中打开的终端也是伪终端的一种
伪终端
不同的终端类型(虚拟机终端和伪终端)可以区分登录系统的方式。
查看当前用户登录的终端类型
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[guanglei@centos8 ~]$ tty
/dev/pts/0
who命令可以查看当前系统登录的终端
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
==========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
如果想要使用系统,就必须使用系统的账号来登录系统,目前运维工程师一般都是通过远程连接工具(例如xshell或者是SecureCRT、mobaxterm)来远程登录服务器。这里使用XShell案例介绍如何远程登录CentOS8.2。
在登录之前需要知道服务器的ip地址
首先在CentOS8中打开终端
CentOS8终端
然后使用命令ifconfig ens33
查看网卡的ip信息
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[guanglei@centos8 ~]$ ifconfig ens33
ens33: 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::dab5:70e0:b6a:c724 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:de:3d:19 txqueuelen 1000 (Ethernet)
RX packets 118 bytes 22018 (21.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 71 bytes 11176 (10.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看输出的inet 10.0.100也就是意味着ip地址是10.0.0.100
然后使用远程终端登录CentOS8
如果你的主机是windows10,可以按windows键然后输入cmd
回车后打开命令提示符
然后输入ssh [email protected]
和root的密码,就可以登录服务器
Windows10内置了ssh远程连接工具
登录服务器
但是日常运维时推荐使用XShell来远程登录Linux
xshell远程连接服务器
首先新建会话
然后设置会话的名称、主机以及端口并点击连接
ssh端口默认是22端口
输入登录的用户名root
输入root账号的密码
登录成功
在运维环境中通常都是同时连接不同环境的多台服务器,xshell的会话支持复制功能,我们只需要复制一个会话,然后修改对应的账号密码,可以同时登录多台服务器。
其中10.0.0.100是CentOS8.2
10.0.0.102是CentOS7.8
10.0.0.101是Ubuntu18.04
需要注意的是目前Ubuntu18.04只能使用普通用户登录
XShell自定义会话属性
设置每秒自动重连
键盘设置
外观设置
会话总览
==========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
交互式接口:启动终端后,在终端上附加的一个交互式应用程序
在使用Linux时,系统提供了两种交互式程序:图形化界面(Graphic User Interface)和基于命令行(Command Line Interface)的字符界面,在运维场景中通常是使用命令行界面完成日常任务。
Linux的主流图形化界面主要有GNOME,KDE和XFCE三种界面,CentOS8默认安装的就是GNOME。
Linux的命令行界面主要是由Shell来执行用户输入的命令。
命令行方式相对于图形界面 绝大多数场景的效率高于图形化界面。
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
Shell是Linux系统的用户界面,提供了用户与内核进行交互的一种接口,它接受用户在字符界面终端上输入的命令并提交给内核去执行,Shell也被称为Linux的命令解释器。
Shell也是一种高级程序设计语言,支持选择结构、分支结构、条件判断,可以编写Shell脚本实现自动化运维。
Shell的本质就是一个程序,如果想要查看当前系统使用的是哪种Shell,可以使用命令echo $SHELL查看
Ubuntu20.04 shell
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
guanglei@ubuntu20:~$ echo $SHELL
/bin/bash
CentOS7 shell
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```
[root@centos7 ~]# echo $SHELL
/bin/bash
CentOS8 shell
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo $SHELL
/bin/bash
目前CentOS8,CentOS7和Ubuntu20.04都使用bash shell来作为默认的shell
除了bash shell以外,系统还内置了其他的shell,可以通过cat /etc/shells
查看系统内置的shell
CentOS8内置的shell
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
CentOS7内置的shell
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos7 ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
Ubuntu Server 20.04内置的shell
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
guanglei@ubuntu20:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux
/usr/bin/screen
Shell本身就是一个程序,如果想要切换成别的Shell,只需要输入完整路径就可完成切换。
==========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
当使用账号登录Linux后,终端上会显示指定格式的命令提示符
CentOS8命令提示符
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]#
命令提示符的含义说明
root表示当前登录的账号
centos8表示主机名,不同的操作系统版本设置成了不同的主机名,用于区分系统版本。
~表示当前所在的目录,这里指的是当前用户的家目录,如果切换目录,则会替换成切换后的目录
\# 表示当前登录的用户是root用户,如果是普通用户登录,显示的是$
命令提示符显示的格式由环境变量PS1决定的,可以使用echo $PS1
命令来查看
环境变量PS1默认值查看
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo $PS1
[\u@\h \W]\$
如果想要改变命令提示符的显示方式,只需要修改PS1即可
31-37表示字体颜色
41-47 表示背景颜色
1 表示高亮
5表示闪烁
命令提示符定制方案1
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# PS1="\[\e[1;5;41;33m\][\u@\h \W]\\$\[\e[0m\]"
命令提示符定制方案2
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# PS1="\[\e[1;32m\][\[\e[0m\]\t \[\e[1;33m\]\u\[\e[36m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"
修改命令提示符
不过这种方式只会临时生效,一旦重新登录则恢复默认的命令提示符。我们可以使用echo结合重定向来将命令提示符的格式写入文件,让其永久生效。
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo 'PS1="\[\e[1;32m\][\[\e[0m\]\t \[\e[1;33m\]\u\[\e[36m\]@\h\[\e[1;31m\] \W\[\e[1;32m\]]\[\e[0m\]\\$"' >/etc/profile.d/env.sh
写入文件后需要使用logout退出,然后XShell在1秒钟后自动重连服务器
如果不想使用,则使用rm -rf删除该文件即可
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[18:25:27 root@centos8 ~]#rm -rf /etc/profile.d/env.sh
删除文件后重新登录系统,系统的命令提示符又使用了默认格式的命令提示符。
企业运维场景通常会区分测试环境和生产环境,可以给不同的环境设置不同的命令提示符来避免误操作。
命令提示符的格式符说明
- \e 控制符\033
- \u 当前用户
- \h 主机名简称
- \H 主机名
- \w 当前工作目录
- \W 当前工作目录基名
- \t 24小时时间格式
- \T 12小时时间格式
- ! 命令历史数
- \# 开机后命令历史数
=================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
当用户在终端上输入命令并回车后,后台运行的Shell会查找该命令所对应的可执行程序或者代码,并分析后提交给内核分配资源将其运行起来。
Shell中可以执行的命令分为内置命令和外置命令两种
内部命令是由Shell自带的(例如pwd,cd,echo都是内置命令),当系统启动时加载到内存。
外部命令是在文件系统路径下有对应的可执行程序的文件(通常在PATH环境变量对应的目录下)。
我们可以通过type命令来查看命令的类型
pwd就是shell内置的命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# type pwd
pwd is a shell builtin
cd也是shell内置的命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# type cd
cd is a shell builtin
type也是内置命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# type type
type is a shell builtin
hostname就是外置命令,其对应可执行程序的路径是/usr/bin/hostname
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# type hostname
hostname is /usr/bin/hostname
cat也是外置命令,其对应的可执行程序的路径是/hsr/bin/cat
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# type cat
cat is /usr/bin/cat
由于Linux上可用的Shell有很多种,因此不同Shell提供了不同的内部命令。例如csh中就没有内置type命令
在执行命令如果内部命令和外部命令同名时优先执行内部命令。
例如echo命令既是内部命令,又是外部命令。可以使用type -a echo
查看
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# type -a echo
echo is a shell builtin
echo is /usr/bin/echo
如果想要优先使用外部命令,可以使用enable命令来禁用内部命令,当输入enable时,该命令会列出所有可以禁用的内部命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable compopt
enable continue
enable declare
enable dirs
enable disown
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable fg
enable getopts
enable hash
enable help
enable history
enable jobs
enable kill
enable let
enable local
enable logout
enable mapfile
enable popd
enable printf
enable pushd
enable pwd
enable read
enable readarray
enable readonly
enable return
enable set
enable shift
enable shopt
enable source
enable suspend
enable test
enable times
enable trap
enable true
enable type
enable typeset
enable ulimit
enable umask
enable unalias
enable unset
enable wait
如果想要禁用指定的命令,可以使用enable -n command实现,例如禁用echo
一旦禁用了echo,再次使用type echo查看命令的类型时显示的是外部命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# enable -n echo
[root@centos8 ~]# type echo
echo is /usr/bin/echo
此时再次使用echo执行的是外部命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo $SHELL
/bin/bash
如果想要再次启用内部命令echo,直接使用enable echo即可
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# enable echo
[root@centos8 ~]# type -a echo
echo is a shell builtin
echo is /usr/bin/echo
在日常运维中绝大多数任务都是使用外部命令实现的,日常常用的外部命令大概有几百个。。。
Linux系统的外部命令存储在PATH变量的路径下,可以使用echo $PATH
查看PATH对应的路径。
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
外部命令可以使用whereis查看对应执行程序的路径以及帮助信息
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz /usr/share/man/man1p/cat.1p.gz
也可以使用which 查看外部命令的执行路径
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# which cat
/usr/bin/cat
当首次执行外部命令,从PATH的路径下查找外部命令,然后放入缓存中,后续再次执行外部命令时,首先从缓存中查找。
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# hostname
centos8.ittimeline.net
[root@centos8 ~]# hash
hits command
1 /usr/bin/hostname
1 /usr/bin/cat
1 /usr/bin/whereis
2 /usr/bin/ls
2 /usr/bin/clear
[root@centos8 ~]# hostname
centos8.ittimeline.net
[root@centos8 ~]# hash
hits command
2 /usr/bin/hostname
1 /usr/bin/cat
1 /usr/bin/whereis
2 /usr/bin/ls
2 /usr/bin/clear
查看内存中缓存了哪些外部命令,可以使用hash命令查看
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# hash
hits command
2 /usr/bin/lsblk
1 /usr/bin/echo
1 /usr/bin/ls
3 /usr/bin/clear
当缓存中的路径失效或者错误时可以清除内存中的外部命令,可以使用hash -d command
实现,除此以外可以使用hash -r
可以清除内存中全部的外部命令。
删除缓存命令的路径
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```hell
[root@centos8 ~]#hash
hits command
3 /usr/bin/pstree
6 /usr/bin/clear
[18:28:10 root@centos8 ~]#hash -d clear
[18:28:22 root@centos8 ~]#hash
hits command
3 /usr/bin/pstree
===================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
别名类似于小名,日常运维中有些常用的命令,例如free -h 命令,其功能是用于查看内存
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# free -h
total used free shared buff/cache available
Mem: 7.6Gi 851Mi 6.1Gi 10Mi 723Mi 6.5Gi
Swap: 4.0Gi 0B 4.0Gi
如果想要更快捷的达到同样的功能,可以使用alias来定义别名,其使用方式是alias 别名='命令'
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# alias free='free -h'
[root@centos8 ~]# free
total used free shared buff/cache available
Mem: 7.6Gi 879Mi 6.0Gi 10Mi 723Mi 6.5Gi
Swap: 4.0Gi 0B 4.0Gi
不过alias增加的命令也是临时性的,如果想要永久生效,可以保存在.bashrc文件中
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo "alias free='free -h' " >> .bashrc
修改完成后还需要执行. .bashrc
让修改的文件立即生效
如果想要删除别名,可以使用unalias命令实现,此命令只会删除内存中配置的别名,如果free在.bashrc中配置,那么下次登录系统时,别名依然会生效。
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# unalias free
[root@centos8 ~]# free
total used free shared buff/cache available
Mem: 7972884 879116 6348600 11136 745168 6814948
Swap: 4194300 0 4194300
如果别名和命令重名,而且想要临时使用原始命令,不使用别名,可以在命令前面加,例如\free,或者使用一对单引号,例如'free'。
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# \free
total used free shared buff/cache available
Mem: 7972884 880104 6347348 11136 745432 6813828
Swap: 4194300 0 4194300
定义别名osversion实现快速查看操作系统的版本
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo "alias osversion='cat /etc/redhat-release'" >> .bashrc
[root@centos8 ~]# . .bashrc
[root@centos8 ~]# osversion
CentOS Linux release 8.2.2004 (Core)
查看.bashrc文件内容
.bashrc文件只会影响当前登录的用户,其他用户不会生效。
定义别名cdnet快速切换到/etc/sysconfig/network-scripts/ 目录下
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# echo "alias cdnet='cd /etc/sysconfig/network-scripts'" >> .bashrc
[root@centos8 ~]# . .bashrc
[root@centos8 ~]# cdnet
[root@centos8 network-scripts]#
在执行命令时别名、内部命令和外部命令的顺序从高到低依次是别名>内部命令>外部命令缓存>外部命令
别名、内部命令、外部命令缓存都是存储在内存中。
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
Linux命令由命令[选项][参数]三部分组成,选项和参数都是可选的
- 命令是外部命令或者内部命令
- 选项用于启用或者关闭命令的某个或者某些功能
选项有短选项和长选项两种,其中短选项由 -和字符组成,例如 ls -l,id -u,长选项由--和单词组成,例如ls --help,长选项相对于短选项更加直观,还有一种BSD风格的短选项,即不带-,例如ps aux。 - 参数是命令作用的对象,例如文件名、用户名等等。例如hostname ittimeline.net ,ittimeline.net就是修改的主机名
需要注意的是
- 命令的多个选项以及参数和命令之间使用空白字符分隔
- 多个命令可以使用;符号隔开
- 一个命令可以使用\分成多行,命令太长时使用,例如编译Nginx
- Ctrl+c:强制退出程序(命令)
- Ctrl+d:正常退出程序(命令)
不带选项和参数的ls
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg test.txt
带选项的ls -a
ls -a 用于显示以.开头的隐藏文件
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .cache .cshrc .esd_auth .tcshrc .viminfo
.. .bash_history .bash_profile .bashrc.save .config .dbus initial-setup-ks.cfg test.txt .Xauthority
带多个选项的ls -al
ls -al 显示所有文件的完整信息
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# ls -al
total 56
dr-xr-x---. 5 root root 4096 Jul 27 10:18 .
dr-xr-xr-x. 18 root root 236 Jul 26 12:48 ..
-rw-------. 1 root root 1602 Jul 26 12:56 anaconda-ks.cfg
-rw-------. 1 root root 2652 Jul 27 10:18 .bash_history
-rw-r--r--. 1 root root 18 May 11 2019 .bash_logout
-rw-r--r--. 1 root root 176 May 11 2019 .bash_profile
-rw-r--r--. 1 root root 289 Jul 27 10:18 .bashrc
-rw-r--r--. 1 root root 243 Jul 27 05:38 .bashrc.save
drwx------. 3 root root 19 Jul 26 12:57 .cache
drwx------. 4 root root 31 Jul 26 15:42 .config
-rw-r--r--. 1 root root 100 May 11 2019 .cshrc
drwx------. 3 root root 25 Jul 26 12:57 .dbus
-rw-------. 1 root root 16 Jul 26 15:42 .esd_auth
-rw-r--r--. 1 root root 1712 Jul 26 13:01 initial-setup-ks.cfg
-rw-r--r--. 1 root root 129 May 11 2019 .tcshrc
-rw-r--r--. 1 root root 6 Jul 27 06:04 test.txt
-rw-------. 1 root root 2945 Jul 27 05:46 .viminfo
-rw-------. 1 root root 136 Jul 27 10:18 .Xauthority
带长选项的ls
ls --help 显示命令的帮助信息
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# ls --help
带选项和参数的ls
ls -l initial-setup-ks.cfg 查看指定文件的完整信息
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# ls -l initial-setup-ks.cfg
-rw-r--r--. 1 root root 1712 Jul 26 13:01 initial-setup-ks.cfg
带选项和参数的id
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# id -u guanglei
1000
一行执行多个命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# hostname;uname -r;who
centos8.ittimeline.net
4.18.0-193.el8.x86_64
root pts/0 2020-07-27 10:18 (10.0.0.1)
guanglei tty2 2020-07-27 04:50 (tty2)
root pts/1 2020-07-27 05:46 (10.0.0.1)
一条命令放在多行执行
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos8 ~]# wh\
> o\
> ami\
>
root
=============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
tab键通常用于外部命令以及路径的补全,在补全外部命令时bash会根据PATH环境变量定义的路径,自左而右在每个路径搜寻以给定命令命名的文件,第一次找到的命令就是要执行的命令。
如果用户给定的字符串只有唯一一条命令,则直接补全,否则再次Tab会给出列表。
在补全路径时把用户给出的字符串当作路径开头,并在其指定的上级目录下搜索以指定的字符串开头的文件名,如果唯一则补全路径,否则再次Tab给出列表。
=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
当输入命令时,系统会将命令存储在内存中,如果想要查看之前执行过的命令,只需要按向上、向下的的方向键。
除了存储在内存中,还会存在用户家目录的.bash\_history文件中。
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos7 ~]# cat .bash_history
uname -a
uptime
clear
screen
yum install -y screen
clear
screen
ps aux
ps aux|grep ping
screen -ls
screen -r
clear
screen -ls
screen -s help
screen -ls
clear
screen -S help
scren -S help
clear
screen -S help
clear
使用history命令可以查看输入的历史命令
<pre data-processed="true" style="line-height: initial !important; word-wrap: break-word; tab-size: 4; white-space: pre-wrap; font-family: monospace; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;">```shell
[root@centos7 ~]# history
1 uname -a
2 uptime
3 clear
4 screen
5 yum install -y screen
6 clear
7 screen
8 ps aux
9 ps aux|grep ping
10 screen -ls
11 screen -r
12 clear
13 screen -ls
14 screen -s help
15 screen -ls
16 clear
17 screen -S help
18 scren -S help
19 clear
20 screen -S help
21 clear
22 whereis kernel-doc
23 clear
24 echo \a
25 echo -e \a
26 echo -e "\a"
27 sleep3; echo -e "\a"
28 sleep 3; echo -e "\a"
29 echo -e '[\e[32m OK \e[0m]'
30 echo {1..10}
31 echo {1..10..2}
32 echo {20..10..2}
33 echo {a..z}
34 echo {1..10}{a..z}
35 echo {1..10}{a..c}
36 clear
37 echo `echo $PATH`
38 touch /data/`date +%F`.log
39 ls /data
40 clear
41 history