*** Linux基本命令操作***
- Linux终端介绍、SHELL提示符、Bash Shell
- 基本命令的使用:ls、pwd、cd、history
- 查看系统和BIOS硬件时间
- Linux如何获取帮助
- 开关机命令及7个启动级别
第一节
1、如何登录系统?
- 图形界面登录
- 文本界面登录
2、虚拟终端
控制台(/dev/tty,/dev/console)
当我们访问RHEL系统的控制台,就可以使用6个虚拟终端来打开6个独立的登录会话,但是默认情况下只激活一个虚拟终端,当切换到未使用的终端时,其它终端提示符会动态启动,虚拟终端由/etc/systemd目录中的logind.conf文件定义,它定义了可被激活的最大虚拟终端数。虚拟终端与设备文件/dev/tty关联,,当你在控制台上登录时,默认使用的是tty1。我们可以使用Ctrl+Alt+[F1—F6] 组合键进行虚拟终端之间的切换,我们就可以切换到tty2、tty3等上面去。tty1–tty6等称为****虚拟终端****,而tty0则是当前所使用虚拟终端的一个别名,系统所产生的信息会发送到该终端上(这时也叫控制台终端)
远程连接终端工具:Xshell Crt 后期会教你安装
例1:通过tty命令看到当前所属的虚拟终端*
[root@test ~]# tty
/dev/pts/0
注:
ctrl+shift +t 在一个终端中打开多个标签
标签之间的切换用alt+n(数字,第一个标签用alt+1)
终端字体放大: shift+ctrl+加号;终端字体缩小:ctrl+减号
例2:不同虚拟终端之间进行通信
例3:对所有的终端进行广播(提示操作系统在10分钟后关机)
或
[root@test ~]# wall "The System will be shutdown in 10 min"
Broadcast message from root@test (pts/0) (Sun Apr 26 04:55:23 2020):
The System will be shutdown in 10 min
第二节
1、认识一下shell
Shell俗称壳,是一个为用户提供使用界面的软件(命令解释器),它类似于WIN中的cmd.exe,用户,shell,操作系统三者间的关系如下图:
它提供了用户与内核进行交互操作的一种接口,它接收用户输入的命令并把它送入内核去执行
内部命令:在系统启动时就调入内存,是常驻内存的,所以执行效率高
外部命令:是系统软件的功能,用户需要时才从硬盘中读入内存
如何区分内外命令?
需要使用一个命令:type
语法: type 要检测的命令
[root@test ~]# type cat
cat is /usr/bin/cat
[root@test ~]# type pwd
pwd is a shell builtin
[root@test ~]# LANG=zh_CN.UTF8
[root@test ~]# type pwd
pwd 是 shell 内嵌
[root@test ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
同时,shell还是一个编程语言。Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果
2、普通用户与管理员用户的区别
怎么区分?根据提示符
这个是root用户登录的
这个是alice普通用户登录的
上面各位置对应内容代表的意思如下:
**[alice@test ~]” 表示的是普通用户
[root@test etc]# “#” 表示管理员用户
[用户名@主机名 用户当前所在的位置(~表示当前用户的家目录)]
在Linux中,只有一个管理员账号就是root
3、常见的shell解释器
[root@test ~]# cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
还有另外两个
ksh zsh
在Linux中执行命令,格式如下:
在LINUX中使用一个命令,命令格式如下:
命令 [选项] [参数]
命令:由小写字母构成,表示相应功能的英文单词或单词的缩写,具体执行的命令,比如pwd,head,
选项:会影响到命令的一些形为操作,通常以- 或--实现
参数:命令作用的对象
说明:方括号内的内容是可选项,它们之间用空格隔开
第三节
1、基本命令--ls
作用:查看当前目录下有哪些文件(list)
语法:ls 目录/文件
- 命令后面不加任何选项,输出的是当前目录下的内容
root@test ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
- 添加选项 -l(小写字母L),表示列出文件的详细信息
[root@test ~]# ls -l
total 8
-rw-------. 1 root root 2123 Apr 26 00:52 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Apr 26 00:57 Desktop
drwxr-xr-x. 2 root root 6 Apr 26 00:57 Documents
drwxr-xr-x. 2 root root 6 Apr 26 00:57 Downloads
-rw-r--r--. 1 root root 2171 Apr 26 00:56 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Apr 26 00:57 Music
drwxr-xr-x. 3 root root 24 Apr 26 01:03 Pictures
drwxr-xr-x. 2 root root 6 Apr 26 00:57 Public
drwxr-xr-x. 2 root root 6 Apr 26 00:57 Templates
drwxr-xr-x. 2 root root 6 Apr 26 00:57 Videos
第一个字符表示文件类型,下面列出了几种文件类型:
d:目录文件
l:链接文件
b:块设备文件
c:字符设备文件
p:管道文件
-: 表示普通文件
为什么不同的文件 显示的颜色不一样呢?
在Linux中用不同的颜色代表了不同的文件类型
- 添加选项 -a 列出目录下的所有文件,包括以点开头的隐藏文件
[root@test ~]# ls -a
. .bash_profile .dbus .ICEauthority Public
.. .bashrc Desktop initial-setup-ks.cfg .tcshrc
anaconda-ks.cfg .cache Documents .local Templates
.bash_history .config Downloads Music Videos
.bash_logout .cshrc .esd_auth Pictures .viminfo
- 添加选项 -d 查看目录的(看不到目录里面内容)
[root@test ~]# ls -d /etc
/etc
[root@test ~]# ls -l -d /etc
drwxr-xr-x. 139 root root 8192 Apr 26 03:04 /etc
[root@test ~]# ls -ld /etc
drwxr-xr-x. 139 root root 8192 Apr 26 03:04 /etc
- 添加选项 -S 以文件的大小进行排序
- ll 是ls -l 的别名
[root@test ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 2262 Apr 26 00:51 /etc/passwd
[root@test ~]# ll /etc/passwd
-rw-r--r--. 1 root root 2262 Apr 26 00:51 /etc/passwd
[root@test ~]# type ll
ll is aliased to `ls -l --color=auto'
添加别名
[root@test ~]# ls -l /etc/sysconfig/network-scripts/ifcfg-ens33
-rw-r--r--. 1 root root 360 Apr 26 00:51 /etc/sysconfig/network-scripts/ifcfg-ens33
[root@test ~]# alias wangwu="ls -l /etc/sysconfig/network-scripts/ifcfg-ens33"
[root@test ~]# wangwu
-rw-r--r--. 1 root root 360 Apr 26 00:51 /etc/sysconfig/network-scripts/ifcfg-ens33
删除别名
[root@test ~]# unalias wangwu
[root@test ~]# wangwu
bash: wangwu: command not found...
别名的永久生效,是需要写入配置文件的
如果需要系统上的所有人都能使用,写入文件/etc/bashrc
如果只是让当前用户使用这个别名,写入文件~/.bashrc
2、基本命令-cd
作用:切换目录(change directory)
语法:cd 目录
说明:直接输入cd表示回到当前用户的宿主(家)目录
[root@test ~]# cd /etc/sysconfig/network-scripts/
[root@test network-scripts]# cd # 后面什么也不加直接返回当前用户家目录
[root@test ~]#
有两个特殊的符号:
.. 两个点 # 表示上一级目录 . 一个点 # 表示当前目录
[root@test network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@test network-scripts]# cd ..
[root@test sysconfig]# pwd
/etc/sysconfig
[root@test sysconfig]# cd .
[root@test sysconfig]# pwd
/etc/sysconfig
[root@test sysconfig]# cd network-scripts/
[root@test network-scripts]# cd ../../
[root@test etc]# pwd
/etc
[root@test etc]# cd - # 跟一个减号,表示返回切换前的那个目录
/etc/sysconfig/network-scripts
3、基本命令-history
作用:查看命令历史记录
4个快速查找Linux历史命令的技巧:
方法1: 键盘上的方向键中上下键 方法2: ctrl+r -》输入某条命令的关键字-》找出来对应的命令,如果要直接执行,就回车,如果要修改,就按左右方向键
方法3: !数字 # 执行历史命令中第N条命令
方法4:!字符串 # 搜索历史命令中最近一个以xxxx字符开头的命令,例如!vim
4、Linux下快捷键
ctrl+a 将光标快速放到当前行的行首
ctrl+e 将光标快速放到当前行的行尾
ctrl+u 将光标所处位置到前面的内容全部删除
ctrl+k 将光标所处位置到后面的内容全部删除
都是用Ctrl+下面的单词, ^表示Ctrl
^C
终止前台运行的程序 , 如:ping g.cn 后,想停止按下Ctrl+C ^D
退出 等价exit ^L
清屏与clear功能一样
^R
搜索历史命令,可以利用好关键词 !$ 引用上一个命令的最后一个参数,等价于ESC+.
[root@test ~]# ls -l /etc/hosts
-rw-r--r--. 1 root root 158 Jun 7 2013 /etc/hosts
[root@test ~]# cat !$ # 直接调用上一条命令的参数,作为本条命令的参数来使用
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@test ~]# !! # 执行上一条命令
cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@test ~]# ls -l /etc/hosts
-rw-r--r--. 1 root root 158 Jun 7 2013 /etc/hosts
[root@test ~]# !!
ls -l /etc/hosts
-rw-r--r--. 1 root root 158 Jun 7 2013 /etc/hosts
补充:键盘上tab键,是一个命令补全键>
第四节
1、时间管理
在Linux中有硬件时钟与系统时钟等两种时钟。硬件时钟是指主机板上的时钟设备,也就是通常可在BIOS画面设定的时钟;系统时钟则是指kernel中 的时钟;所有Linux相关指令与函数都是读取系统时钟的设定
当Linux启动时,系统时钟会去读取硬件时钟的设定,之后系统时钟即独立运作
查看硬件时间:
[root@test ~]# hwclock # hw:hardware
Sun 26 Apr 2020 06:21:02 AM CST -0.380533 seconds
查看系统时间:
[root@test ~]# date
Sun Apr 26 06:21:43 CST 2020
说明:
UTC (Universal Time Coordinated):世界标准时间
GMT (Greenwich Mean Time):格林尼治时间
CST (China standard Time):中国标准时间
在生产中,我们会配置一个时钟服务器,来对时间进行校准
2、如何修改时间
语法:date -s 把时间设为字符串所描述的时间
[root@test ~]# date
Sun Apr 26 06:25:07 CST 2020
[root@test ~]# date -s "2020-4-25 22:25"
Sat Apr 25 22:25:00 CST 2020
[root@test ~]# date
Sat Apr 25 22:25:01 CST 2020
- 选项 %F 表示完整日期格式 ,等 价于%Y-%m-%d
[root@test ~]# date "+%F"
2020-04-25
[root@test ~]# date "+%Y-%m-%d"
2020-04-25
[root@test ~]# date "+%Y%m%d"
20200425
[root@test ~]# date "+%Y&%m&%d"
2020&04&25
[root@test ~]# date "+%Y:%m:%d"
2020:04:25
3、查看帮助
- Linux手册的常见章节
| 章节 | 内容类型 |
|---|---|
| 1 | 用户命令(可执行命令和shell程序) |
| 2 | 系统调用(从用户空间调用的内核例程) |
| 3 | 库函数(由程序库提供) |
| 4 | 特殊文件(如设备文件) |
| 5 | 文件格式(用于许多配置文件和结构) |
| 6 | 游戏(过去的有趣程序章节) |
| 7 | 惯例、标准和其他(协议、文件系统) |
| 8 | 系统管理和特权命令(维护任务) |
| 9 | Linux内核API(内核调用) |
范例:
要显示具体章节的man pages主题,请附上章节编辑参数: man 5 passwd,显示passwd(5),就是/etc/passwd文件内容说明
- Man Page导航使用
| 命令 | 结果 |
|---|---|
| 空格键 | 向前(向下)滚动一个屏幕 |
| PageDown | 向前(向下)滚动一个屏幕 |
| PageUp | 向后(向上)滚动一个屏幕 |
| 向下箭头键 | 向前(向下)滚动一行 |
| 向上箭头键 | 向后(向上)滚动一行 |
| d | 向前(向下)滚动半个屏幕 |
| u | 向后(向上)滚动半个屏幕 |
| /string | 在man page中向前(向下)搜索string |
| n | 在man page中重复之前的向前(向下)搜索 |
| shift+n | 在man page中重复之前的向后(向上)搜索 |
| g | 转到man page的开头 |
| shift+g | 转到man page的末尾 |
| q | 退出man,并返回到命令shell提示符 |
- 如何阅读man page
我们查询某个命令(主题),大多数共享相同的标题,并以相同的顺序显示,通常,主题不包含所有标题,下面列出一些常见的标题:
| 标题 | 描述 |
|---|---|
| NAME | 主题名称。通常是命令或文件 名。非常简短的描述 |
| SYNOPSIS | 命令语法的概要 |
| DESCRIPTION | 提供对主题的基本理解的深度描述 |
| OPTIONS | 命令执行选项的说明 |
| EXAMPLES | 有关如何使用命令、功能或文件的示例 |
| FILES | 与man page相关的文件上目录的列表 |
| SEE ALSO | 相关的信息,通常是其它man page主题 |
| BUGS | 软件中的已知错误 |
| AUTHOR | 有关参与编写该主题的人员的信息 |
常见的系统管理主题在第1节(用户命令)、第5节(文件格式)和第8节(管理命令)中
范例1:
man命令参数
-k 对man page执行关键字搜索,这会显示与关键字匹配的man page主题和章节编号的列表
[root@RHCE8 ~]# man -k passwd
chgpasswd (8) - update group passwords in batch mode
chpasswd (8) - update passwords in batch mode
fgetpwent_r (3) - get passwd file entry reentrantly
getpwent_r (3) - get passwd file entry reentrantly
gpasswd (1) - administer /etc/group and /etc/gshadow
grub2-mkpasswd-pbkdf2 (1) - Generate a PBKDF2 password hash.
lpasswd (1) - Change group or user password
openssl-passwd (1ssl) - compute password hashes
pam_localuser (8) - require users to be listed in /etc/passwd
passwd (1) - update user's authentication tokens
passwd (5) - password file
passwd2des (3) - RFS password encryption
pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shadow to opasswd
saslpasswd2 (8) - set a user's sasl password
smbpasswd (5) - The Samba encrypted password file
sslpasswd (1ssl) - compute password hashes
userpasswd (1) - A graphical tool to allow users to change their passwords.
vncpasswd (1) - change the VNC password
范例2:
[root@test ~]# man date # 查看data命令的帮助
[root@test ~]# date "+%Y-%m-%d %H:%M:%S"
2020-04-25 22:32:49
[root@test ~]# LANG=zh_CN.UTF8
[root@test ~]# man date
[root@test ~]# date "+%F %X"
2020-04-25 22时34分00秒
[root@test ~]# man date
- -h 或 --help
[root@test ~]# cat --help
用法:cat [选项]... [文件]...
将[文件]或标准输入组合输出到标准输出。
-A, --show-all 等于-vET
-b, --number-nonblank 对非空输出行编号
-e 等于-vE
-E, --show-ends 在每行结束处显示"$"
-n, --number 对输出的所有行编号
-s, --squeeze-blank 不输出多行空行
-t 与-vT 等价
-T, --show-tabs 将跳格字符显示为^I
-u (被忽略)
-v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
--help 显示此帮助信息并退出
--version 显示版本信息并退出
如果没有指定文件,或者文件为"-",则从标准输入读取。
示例:
cat f - g 先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。
cat 将标准输入的内容复制到标准输出。
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告cat 的翻译错误
要获取完整文档,请运行:info coreutils 'cat invocation
[root@test ~]# cat /etc/hosts /etc/resolv.conf
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# Generated by NetworkManager
nameserver 10.10.30.2
- help pwd(查看帮助,内部命令)
- 官方手册
- 百度、谷歌
找到以下几个命令的,通过查看帮助知道如何使用
fdisk lsblk grep
第五节
常用的关机,重启命令
shutdown
init 0 # 关机
reboot # 重启
poweroff # 关机
1、关机命令--shutdown
作用:关机,重启,定时关机
语法:shutdown [选项]
参数:
-r => 重新启动计算机
-h => 关机
-h 时间 =>定时关机
例如:
[root@test ~]# shutdown -h +10 # 10分钟之后关机
[root@test ~]# shutdown -h 23:30 # 指定具体的时间点进行关机
[root@test ~]# shutdown -h now # 立即关机
[root@test ~]# shutdown -r 22:22 # 22:22 以后重启
2、七个启动级别
作用:切换系统运行级别
语法:init 0-6
Linux 7个启动级别:
0 系统停机模式,系统默认运行级别不能设置为0,否则不能正常启动,机器关的
1 单用户模式,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登录
2 多用户模式,没有NFS和网络支持
3 完整的多用户文本模式,有NFS和网络,登陆后进入控制台命令行模式>
4 系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。例如在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置
5 图形化模式,登陆后进入图形GUI模式,X Window系>
6 重启模式,默认运行级别不能设为6,否则不能正常启动。运行init 6机器就会重启
例:
[root@localhost ~]# init 0 #关机
[root@localhost ~]# init 3 #进入3级别字符界面
[root@localhost ~]# init 5 #进入5级别图形界面
3、如何设置默认的运行级别
centos7不再使用/etc/inittab文件进行默认的启动级别配置,而使用比sysvinit的运行级更为自由的target替代。
第3运行级用multi-user.target替代。
第5运行级用graphical.target替代。
设置默认第三启动级别
[root@test ~]# systemctl set-default multi-user.target
设置默认第五启动级别
[root@test ~]# systemctl set-default graphical.target
[root@test ~]#runlevel
3 5 # 结果显示系统从运行级别3切换到了运行级别5
查看当前默认的启动级别
[root@test ~]# systemctl get-default
graphical.target