点击上方蓝色字体,关注本公众号,了解学习更多相关知识!
“Linux基本操作”
01
—
Linux终端及shell介绍
1、认识Linux终端
终端仿真器:
GNOME桌面:GHOME Terminal
KDE桌面:Konsole Terminal
远程链接工具:有很多,可根据个人喜好自行选择:
https://note.youdao.com/s/4FK9p8eW
示例1:通过tty命令查看当前的虚拟终端
[root@demo ~]# tty/dev/pts/0
注:在桌面版Terminal中,常用快捷键:
shift+ctrl+N:快速打开一个终端
ctrl+shift+“+”:终端字体放大
ctrl+“-”:终端字体缩小
示例2:不同虚拟终端之间通讯
同时打开两个终端,在第一个终端中执行:
[root@demo ~]# tty/dev/pts/0[root@demo ~]# echo demo1 > dev/pts/1
查看第二个终端的输出:

示例3:对所有终端广播消息:该系统于5分钟后关机
[root@demo ~]# tty/dev/pts/0[root@demo ~]# shutdown +5Shutdown scheduled for Sun 2023-04-30 22:44:56 CST, use 'shutdown -c' to cancel.[root@demo ~]#Broadcast message from root@demo (Sun 2023-04-30 22:39:56 CST):The system is going down for power-off at Sun 2023-04-30 22:44:56 CST!
如下,可以看到在第二个终端中有收到关机的消息
[root@demo ~]# tty/dev/pts/1Broadcast message from root@demo (Sun 2023-04-30 22:39:56 CST):The system is going down for power-off at Sun 2023-04-30 22:44:56 CST!
取消关机:
[root@demo ~]# shutdown -cBroadcast message from root@demo (Sun 2023-04-30 22:41:28 CST):The system shutdown has been cancelled at Sun 2023-04-30 22:42:28 CST!
或者使用命令:wall,广播命令,通知所有终端
[root@demo ~]# wall "This is a demo system" # 终端:/dev/pts/0Broadcast message from root@demo (pts/0) (Sun Apr 30 22:44:28 2023):This is a demo system[root@demo ~]# # 终端:/dev/pts/1Broadcast message from root@demo (pts/0) (Sun Apr 30 22:44:28 2023):This is a demo system
2、认识Shell
Shell俗称壳,是指“为使用者提供操作界面”的命令解释器软件,它接收到用户输入的命令,然后调用相应的应用程序。
Shell同时又是一种程序设计语言。作为命令语言,它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高级语言中才具有的控制结构,包括循环和分支。
Shell根据类型分为图形界面和命令行式:
图形化界面shell:(Graphical User Interface shell 即 GUI shell),就是带桌面窗口化的一种展示界面。如微软的Windows系列,Linux Shell中的X Window Manager,或者GNOME、KDE等。
命令行式shell:(Command Line Interface shell ,即CLI shell),以centos 7为例,查看系统支持的shell如下:
[root@demo ~]# cat etc/shells/bin/sh # 由Steve Bourne开发,是Bourne Shell的缩写,sh是Unix标准默认的shell。/bin/bash # 由Brian Fox和Chet Ramey共同完成,是BourneAgain Shell的缩写,也是Linux标准默认的shell。/sbin/nologin # 配置该解释器的用户,不允许登录当前的Linux系统。/bin/tcsh # 是csh的增强版,加入了命令补全功能,提供了更加强大的语法支持。/bin/csh # 由以William Joy为代表的共计47位作者编成,共有52个内部命令。该shell其实是指向/bin/tcsh这样的一个shell,也就是说,csh其实就是tcsh。# 还有其他一些解释器,如ash、ksh、zsh等,这里就不一一介绍了。
Shell根据模式的不同分为交互式和非交互式:
交互式shell:该模式是shell等待用户的输入,并且执行用户提交的命令。由于是需要与用户进行交互而命名。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、退出。当用户退出后,shell也就终止了。
非交互式shell:该模式下,shell不与用户进行交互,而是读取用户编写存放在文件中的命令,来执行它们。当它读到文件的结尾,shell也就终止了。
3、Shell命令
根据来源不同分为内部命令和外部命令:
内部命令:是构建在shell内部,由当前shell本身来执行,所以执行效率高。
外部命令:是一个独立的文件(可以是二进制或者脚本等),执行时通过shell进程建立子进程后,加载外部文件,加载完成后才会执行,所以执行效率相对较低。
使用type命令来区分内部命令和外部命令:
[root@demo ~]# type pwdpwd is a shell builtin # 内部命令:pwd是shell内嵌[root@demo ~]# type catcat is hashed (/usr/bin/cat) # 外部命令:指向一个文件
4、Shell提示符
在Shell中,提示符有两种,分别是“#” 号与“$”号,区别如下:
[root@demo ~]# # “#”号表示是root用户登录,系统管理员账号登录[root@demo ~]# su - cloud # 切换到cloud普通用户登录[cloud@demo ~]$ # “$”号表示是普通用户登录# 上面登录提示符前面“[]”中的内容代表的意思如下:[root @ demo ~ ]#[用户名---@---主机名---当前工作目录(~表示的是当前用户的家目录)]管理员root身份登录
02
—
基本命令
在Linux中使用一个命令,语法格式为:“命令 【选项】 【参数】”
命令:要执行的命令,如cd、pwd、ls、head等
选项:要执行的命令支持提供一些操作,常以短格式“-”、长格式“--”实现
参数:命令作用的对象
注:命令、选项、参数之间要用空格进行分隔,且字母严格区分大小写。
1、ls命令
ls命令是用于显示目录中的文件列表,list的缩写,语法格式为 “ls [选项]... [文件]...”。
当选项、文件为空时,则表示查看当前目录下的内容
常用的选项:
-l:列出文件的详细信息,如文件的类型、权限、链接数、所有者、所属组、大小、日期、文件名
[root@demo ~]# ls -ltotal 84712-rw-------. 1 root root 1493 Feb 4 2022 anaconda-ks.cfgdrwxr-xr-x. 2 root root 6 Feb 4 2022 Desktop
-a:列出目录下所有的文件,包括以“.”开头的隐藏文件
[root@demo ~]# ls -a. anaconda-ks.cfg .bash_logout .bashrc .config .dbus Documents Pictures Public Templates.. .bash_history .bash_profile .cache .cshrc Desktop Downloads initial-setup-ks.cfg Music
-d:列出目录本身,而不查看目录里面的内容
[root@demo ~]# ls -ld root/dr-xr-x---. 16 root root 4096 May 1 12:43 root/
-t:按修改时间排序,最新的优先,一般搭配-l使用,便于观看
[root@demo ~]# ls -tDownloads Desktop Documents Music Pictures Public Templates Videos initial-setup-ks.cfg anaconda-ks.cfg[root@demo ~]# ls -lttotal 8drwxr-xr-x. 2 root root 97 Mar 3 2022 Downloadsdrwxr-xr-x. 2 root root 6 Feb 4 2022 Desktopdrwxr-xr-x. 2 root root 6 Feb 4 2022 Documentsdrwxr-xr-x. 2 root root 6 Feb 4 2022 Musicdrwxr-xr-x. 2 root root 6 Feb 4 2022 Picturesdrwxr-xr-x. 2 root root 6 Feb 4 2022 Publicdrwxr-xr-x. 2 root root 6 Feb 4 2022 Templatesdrwxr-xr-x. 2 root root 6 Feb 4 2022 Videos-rw-r--r--. 1 root root 1541 Feb 4 2022 initial-setup-ks.cfg-rw-------. 1 root root 1493 Feb 4 2022 anaconda-ks.cfg
-s:以块为单位打印每个文件的分配大小
[root@demo ~]# ls -stotal 84 anaconda-ks.cfg 0 Desktop 0 Documents 0 Downloads 4 initial-setup-ks.cfg 0 Music 0 Pictures 0 Public 0 Templates 0 Videos
拓展:
使用ls -l命令打印文件的内容解析:
上表中第一个字符文件类型的表示分类及相对应颜色:
命令+参数的简写方式,即ls -l和ll执行结果是一样的
下面的返回值说明ll是ls -l的别名
[root@demo ~]# type llll is aliased to `ls -l --color=auto'
2、alias命令
alias命令是用来定义别名的,给经常使用而有很长的路径或文件以别名的方式快速访问。
[root@demo ~]# alias ve32="vim etc/sysconfig/network-scripts/ifcfg-ens32" # 定义别名[root@demo ~]# ve32[root@demo ~]# 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 ve32='vim etc/sysconfig/network-scripts/ifcfg-ens32'alias which='alias | usr/bin/which --tty-only --read-alias --show-dot --show-tilde'[root@demo ~]# unalias ve32 # 删除别名
设置别名永久生效:
当前用户:将定义别名的整条命令写入到~/.bashrc文件中。
所有用户,全局使用:将定义别名的整条命令写入到/etc/bashrc文件中。
3、cd命令
cd命令的作用是切换目录,更改当前工作目录,语法格式为 “cd [选项] [目录]”。
[root@demo sysconfig]# cd etc/sysconfig/network-scripts/ # 切换到/etc/sysconfig/network-scripts/[root@demo network-scripts]# pwd/etc/sysconfig/network-scripts[root@demo network-scripts]# cd # 表示返回到家目录,后面不加值时[root@demo ~]# pwd/root[root@demo network-scripts]# cd ~ # 等同于cd命令[root@demo ~]# pwd/root[root@demo network-scripts]# cd .. # 表示返回到上级目录,也就是父目录[root@demo sysconfig]# pwd/etc/sysconfig[root@demo sysconfig]# cd . # 表示进入当前目录,即目录不会发生变更[root@demo sysconfig]# cd root # 进入root目录[root@demo ~]# pwd/root[root@demo ~]# cd - # 表示返回进入到这个目录之前所在的目录/etc/sysconfig
4、history命令
history命令是用来查看历史记录的,语法格式为 “history [选项] [参数]”。
常用的参数及选项:
-c:清空当前用户的历史记录
-n:显示最近n行的历史记录
-d offset:删除offset位置的历史记录
-a:将当前bash会话开始以来输入的命令行,附加到当前用的历史文件中
更多参数请使用man history查看
拓展:快速查找历史命令的技巧
使用键盘中的上下方向键
!+offset(数字):执行历史命令中的第N条命令
!+字符串:执行历史命令中最近一次以该字符串开头的命令
ctrl+r→输入某条命令的关键字→找出对应的命令,按回车即可执行或按右方向键进行修改
5、Linux快捷键
ctrl + c:终止当前运行的命令(前台)
ctrl + d:退出当前会话,等同于exit
ctrl + l:清楚当前屏幕,等同于clear
ctrl + r:搜索历史命令
!$:引用上一条命令的最后一个参数,等同于alt+“.”(有些系统不支持)
03
—
系统时间命令
在Linux系统中的时间分为硬件时钟和系统时钟。
硬件时钟:是指保存在主板上的时钟,也就是通过BIOS设定的时钟
系统时钟:是指保存在内核(kernel)中的时钟,是系统中相关指令与函数默认读取的时钟
当Linux系统启动时,系统时钟会读取硬件时钟进行同步,之后独立运行
1、hwclock命令
hwclock命令:用来访问硬件时钟的工具,语法格式为 “hwclock [函数] [选项] ... ”。
常用参数:
-r, --show # 读取硬件时钟的时间并在屏幕上打印。
-s, --hctosys # 根据硬件时钟设置系统时间
-w, --systohc # 设置硬件时钟为当前系统时间
--set --date=date_string # 设置硬件时钟为指定的日期
更多参数请使用man hwclock查看
[root@demo ~]# hwclock -r # 读取硬件时间Mon 01 May 2023 02:30:13 PM CST -0.036614 seconds[root@demo ~]# hwclock --set --date="2023-05-02 13:00:00" # 设置硬件时钟为2023年5月2日 13:00分[root@demo ~]# hwclock -rTue 02 May 2023 01:00:05 PM CST -0.241095 seconds[root@demo ~]# hwclock -w # 设置硬件时钟为当前系统时间[root@demo ~]# hwclock -rMon 01 May 2023 02:31:21 PM CST -0.661782 seconds
2、data命令
date命令:用来打印或设置系统日期和时间。
语法格式:
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
常用参数:
-d, --date=STRING:显示指定的时间
-r, --reference=FILE:显示文件的最后修改时间
-s, --set=STRING:设置为指定的时间
-u, --utc, --universal:打印或设置协调世界时(UTC)
常用的时间控制输出的格式,见下表:
[root@demo ~]# date "+%F" # 以完整日期格式输出2023-05-01[root@demo ~]# date "+%Y%m%d" # 以日期组合方式输出20230501[root@demo ~]# date "+%Y-%m-%d %H:%M:%S" # 在年月日之间添加想要的字符或不添加2023-05-01 14:52:43[root@demo ~]# date "+%Y/%m/%d %H:%M:%S"2023/05/01 14:53:01[root@demo ~]# date "+%Y%m%d %H:%M:%S"20230501 14:53:05[root@demo ~]# date -s "2023-05-02 13:00:00" # 设置系统时间为2023年5月2日 13:00分Tue May 2 13:00:00 CST 2023
3、时区
UTC (Universal Time Coordinated):世界标准时间
GMT (Greenwich Mean Time):格林尼治时间
CST (China standard Time):中国标准时间
4、拓展:time命令
time命令:用来计算一个命令的运行时间,语法格式为 “time [选项] 命令 [参数...]”。
更多使用方法及参数,请查看man time。
[root@demo ~]# time ls -l etc/total 1452real 0m0.006s # 实际使用时间user 0m0.000s # 用户状态使用的时间sys 0m0.005s # 内核状态使用的时间
04
—
帮助命令
1、man命令
man命令,本地保存的命令参考手册,基本使用语法格式为 “man 命令”。
[root@demo ~]# man man# 查看man命令的帮助文档,查看时支持上下翻页(page up、page down)、搜索(输入斜线)、退出(q)
2、help命令
help命令是查看某个命令的使用帮助,语法格式为“命令 -h或--help”
注:有些命令是不支持短格式-h的。如find命令
[root@demo ~]# find -hfind: unknown predicate `-h'[root@demo ~]# find --helpUsage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
05
—
常用的开关机命令
1、shutdown命令
shutdown命令:关机,语法格式为:“shutdown [选项...] [时间] [留言...]”
shutdown:不加参数,立即关闭操作系统
shutdown -H:立即关闭设备硬件电源,等同于halt命令
shutdown -P:立即关闭操作系统,等同于poweroff命令
shutdown -r:计划于1分钟后重新启动操作系统,等同于reboot命令
shutdown -r 时间:计划于某个时间重新启动操作系统
shutdown -h now:立即关闭操作系统,默认情况下等同于poweroff
shutdown -h 时间:指定该时间后关闭操作系统
shutdown -c:取消指定时间关闭操作系统的任务
[root@demo ~]# shutdown -h +10Shutdown scheduled for Mon 2023-05-01 15:32:22 CST, use 'shutdown -c' to cancel.[root@demo ~]#Broadcast message from root@demo (Mon 2023-05-01 15:22:22 CST):The system is going down for power-off at Mon 2023-05-01 15:32:22 CST![root@demo ~]# shutdown -h 23:30Shutdown scheduled for Mon 2023-05-01 23:30:00 CST, use 'shutdown -c' to cancel.[root@demo ~]# shutdown -r 22:22Shutdown scheduled for Mon 2023-05-01 22:22:00 CST, use 'shutdown -c' to cancel.[root@demo ~]# shutdown -rShutdown scheduled for Mon 2023-05-01 15:24:19 CST, use 'shutdown -c' to cancel.Broadcast message from root@demo (Mon 2023-05-01 15:23:19 CST):The system is going down for reboot at Mon 2023-05-01 15:24:19 CST![root@demo ~]# shutdown -cBroadcast message from root@demo (Mon 2023-05-01 15:23:24 CST):The system shutdown has been cancelled at Mon 2023-05-01 15:24:24 CST![root@demo ~]# shutdown -h now
06
—
系统启动级别
1、系统的7个启动级别
init是Linux系统操作中不可缺少的程序之一,是由内核启动的用户级进程。
用途:切换系统的运行级别,语法格式:“init 0-6”。
分别代表的含义如下:
0:系统停机或关机,默认运行级别不能设置为0,否则系统将不能正常启动
1:单用户模式,仅root用户登录维护,而且仅限本地登录,无法远程访问
2:多用户模式,此模式下不支持NFS和网络功能
3:完全多用户模式:标准使用的运行级别,有NFS和网络功能,登录后进入命令行界面
4:安全模式:系统保留模式,应急使用
5:图形化模式:即登录后进入图形界面(GUI)模式
6:重启模式:默认运行级别不能设置为6,否则系统将不断重启
[root@demo ~]# init 0 # 关机[root@demo ~]# init 3 # 切换至3级别命令行操作界面[root@demo ~]# init 5 # 切换至5界别图形化操作界面[root@demo ~]# init 6 # 重启设备
2、默认运行级别设置
CentOS 7不在使用/etc/inittab文件进行默认的启动界别配置,而是用sysvinit的运行级别更为自用的target替代。
第3运行级别:multi-user.target
第5运行级别:graphical.target
[root@demo ~]# systemctl get-default # 查看当前的运行级别graphical.target[root@demo ~]# systemctl set-default multi-user.target # 设置默认启动级别为第3运行级别Removed symlink /etc/systemd/system/default.target.Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.[root@demo ~]# systemctl set-default graphical.target # 设置默认启动级别为第5运行级别Removed symlink /etc/systemd/system/default.target.Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.[root@demo ~]# runlevel # 表示系统启动从3级别切换到5级别运行3 5
欢迎点击上方链接,关注本公众号!
谢谢您,点击👇“赞”和“在看”!




