文章目录
前言:命令基础格式
命令的格式:
command [-options] [parameter]
说明:
- command :命令名,相应功能的英文单词或单词的缩写
- [-options]:选项,指定了命令的运行特性,指明了运行命令中的哪一个功能代码
- 短选项:如 -l -d -o
- 长选项:如 --help
- [parameter]:参数,命令的作用对象,也就是说命令对什么生效
Ctrl + l 清屏
注:鼠标上滑还是可以找到之前代码记录哒~
注:为了理解更简单,也为了后续各位在职场中能用惯英文版,博主敲代码会同时开启一个中文远程连接会话和一个英文远程连接会话。
1.查看帮助
- 命令: help(推荐)
作用:shell 内部命令帮助信息(提供简要用法说明) - 命令: man
作用:系统帮助手册页(提供更全面、详细的命令、函数、系统调用等相关文档信息)
1.1 help(推荐)
例如:
我们现在只知道有命令 “ cd ”,但是不知道这个命令是用来干什么的,则可以用 help 命令 。
# 方法一:help 命令名称 (不推荐,专为shell内部命令服务,有可能调不出来)
[root@bogon ~]# help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
-e if the -P option is supplied, and the current working
directory cannot be determined successfully, exit with
a non-zero status
-@ on systems that support it, present a file with extended
attributes as a directory containing the file attributes
The default is to follow symbolic links, as if `-L' were specified.
`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
# 方法二:命令名称 --help (推荐)
[root@bogon ~]# cd --help
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without following
symbolic links: resolve symbolic links in DIR before
processing instances of `..'
-e if the -P option is supplied, and the current working
directory cannot be determined successfully, exit with
a non-zero status
-@ on systems that support it, present a file with extended
attributes as a directory containing the file attributes
The default is to follow symbolic links, as if `-L' were specified.
`..' is processed by removing the immediately previous pathname component
back to a slash or the beginning of DIR.
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
1.2 man
man是manual(手册)的简写,无中文版
# man 命令名称
[root@bogon ~]# man cd
.....................(忒多,自己试试吧)
以下是超全man命令操作键,全网无代餐,挑你用得习惯的记吧!
页面操作键 | 功能 |
---|---|
鼠标滚轮 | 上下翻滚 |
上键 / k | 上滚一行 |
u | 上滚半页 |
b | 上滚一页 |
下键 / Enter键 /e / j | 下滚一行 |
d | 下滚半页 |
空格键 / f / z | 下滚一页 |
g / Esc-< | 跳转首页 |
G / Esc-> | 跳转尾页 |
光标操作键 | 功能 |
---|---|
e | 前移一行 |
y | 后移一行 |
g / Esc-< | 移至首页第一行 |
G / Esc-> | 移至尾页最后一行 |
搜索操作键 | 功能 |
---|---|
/word | 从上至下搜索字符串“word” |
?word | 从下至上搜索字符串“word” |
n | 定位到下一搜索到的关键词 |
N | 定位到上一搜索到的关键词 |
同时按“Esc”、“-” 、“u” | 取消所有搜索到关键词的高亮显示 |
其他操作键 | 功能 |
---|---|
h / H | 显示帮助信息 |
m加字母 | 设置书签(输入单引号加字母可跳转到对应书签位置) |
!加shell命令 | 不退出manual手册执行shell命令 |
q | 退出manual手册 |
2.IP地址
- 命令: ip a
作用:查看本机网络接口信息,包括接口基本状态、IP地址信息、链路层信息 - 命令: dhclient
作用:自动获取网络配置、管理IP地址租约等 - 命令: nmcli
作用:用于控制NetworkManager的命令行工具,用于网络连接管理、网络设备管理、无线网络管理、查看网络信息
2.1 ip a
目前只需先记住之前讲过的获取本机 IP地址 的方法:
2.2 dhclient
[root@bogon ~]# dhclient
bash: dhclient: command not found...
#注:如果提示没有该命令,则为外部命令,需要安装之后才能使用(怎么安装?后续会讲)
2.3 nmcli
看看nmcli用法:
应用一下:例如,查看网络设备
从图中可以看到 “d[evice] NetworkManager 管理的设备”
[root@bogon ~]# nmcli d
DEVICE TYPE STATE CONNECTION
ens160 ethernet connected ens160
lo loopback connected (externally) lo
[root@bogon ~]# nmcli device
DEVICE TYPE STATE CONNECTION
ens160 ethernet connected ens160
lo loopback connected (externally) lo
练习:要连接网卡设备
从help中可知,连接是 “ c[onnection] NetworkManager’s connections ”,即 “c[onnection] NetworkManager 的连接”
输入“nmcli connection ens160”
显示为:
说明输入错误!why?~
1、nmcli 未明确你要执行的操作是针对设备连接,还是其他如连接配置文件等对象的操作,导致命令执行错误。
2、所有要让 nmcli 明确操作的设备。
3、“d[evice] NetworkManager 管理的设备”
明确命令中要有 d 和 c 两个“object”
输入“nmcli d c ens160
ok ~ 成功 ! ! !
3. 日期
- 命令: date(必须记)
作用:在操作系统中显示或设置系统日期和时间。 - 命令: timedatectl
作用:设置或者查看时区的命令- 命令:status
作用:显示状态信息 - 命令:list-timezones
作用:显示已知时区 - 命令:set-time
作用:设置时间 - 命令:set-timezone
作用:设置时区
- 命令:status
- 命令: cal
作用:查看日历
3.1 date
看看date用法:
[root@bogon ~]# date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
......................................后续还有,这里摘取重要部分
如果我们要显示系统时间即时区:
[root@bogon ~]# date
Mon May 5 09:06:22 PM CST 2025
注:CST 表示的是中国时区 UTC 表示世界时区 CST = UTC + 8
如果我们要按照我们日常的习惯去显示日期: 年 - 月 - 日 时:分:秒:
#格式化时间
[root@bogon ~]# date +%Y-%m-%d
2025-05-05
[root@bogon ~]# date +%Y-%m-%d %H:%M:%S
date: extra operand ‘%H:%M:%S’
Try 'date --help' for more information.
#注:出现上述错误的原因是因为这个命令参数中含有空格--------- 需要把格式化的内容(含有空格)放到引号中
[root@bogon ~]# date "+%Y-%m-%d %H:%M:%S"
2025-05-05 21:19:20
#注:双引号行
[root@bogon ~]# date '+%Y-%m-%d %H:%M:%S'
2025-05-05 21:21:51
#注:单引号也行
[root@bogon ~]# date '+%Y-%m-%d %H:%M:%S"
#注:单双引号不可混合使用
如果我们要查看几天(周、月、年)前(后)的时间:
#查看两天之前的时间
[root@bogon ~]# date -d "2 days ago"
Sat May 3 09:45:22 PM CST 2025
#或
[root@bogon ~]# date -d "-2 days"
Sat May 3 09:46:54 PM CST 2025
#查看两天之后的时间
[root@bogon ~]# date -d "2 days "
Wed May 7 09:49:32 PM CST 2025
#或
[root@bogon ~]# date -d "+2 days"
Wed May 7 09:47:59 PM CST 2025
#也可以查看几周(月、年)前(后)
[root@bogon ~]# date -d "+2 weeks"
Mon May 19 09:52:00 PM CST 2025
[root@bogon ~]# date -d "+2 month"
Sat Jul 5 09:52:15 PM CST 2025
时间戳 :时间戳是一个用不重复的字符串 计算方式是当前时间距离1970-1-1 00:00:00的秒数,%s
#查看当前时间的时间戳
[root@bogon ~]# date +%s
1746453410
[root@bogon ~]# date +%s
1746453415
[root@bogon ~]# date +%s
1746453417
#注:每秒都不一样
#如:计算2024-5-20的时间戳
[root@bogon ~]# date -d "2024-5-20" +%s
1716134400
设置当前时间
#设置为2024年10月1日
[root@bogon ~]# date -s 20241001
Tue Oct 1 12:00:00 AM CST 2024
#设置为10:01:00
[root@bogon ~]# date -s 10:01:00
Tue Oct 1 10:01:00 AM CST 2024
#注:想设置回来可直接看时间设置或恢复快照
3.2 timedatectl命令
[root@bogon ~]# timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
Commands:
status Show current time settings
show Show properties of systemd-timedated
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization
systemd-timesyncd Commands:
timesync-status Show status of systemd-timesyncd
show-timesync Show properties of systemd-timesyncd
Options:
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
--monitor Monitor status of systemd-timesyncd
-p --property=NAME Show only properties by this name
-a --all Show all properties, including empty ones
--value When showing properties, only print the value
See the timedatectl(1) man page for details.
3.2.1 status
#显示当前状态信息
[root@bogon ~]# timedatectl status
Local time: 一 2025-05-05 22:34:34 CST #当前中国时区时间
Universal time: 一 2025-05-05 14:34:34 UTC #当前世界时区时间
RTC time: 一 2025-05-05 14:34:34
Time zone: Asia/Shanghai (CST, +0800) #时区,就是安装系统时我们选择的时区
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
#查看系统时间
#方法一:直接date,显示更简洁
[root@bogon ~]# date
Tue Oct 1 10:42:45 AM CST 2024
#方法二:timedatectl show,具体详细的信息
[root@bogon ~]# timedatectl show
Timezone=Asia/Shanghai
LocalRTC=no
CanNTP=yes
NTP=yes
NTPSynchronized=no
TimeUSec=Tue 2024-10-01 10:44:43 CST
RTCTimeUSec=Mon 2025-05-05 22:42:03 CST
3.2.2 list-timezone
#查看已知时区 时区列表
[root@bogon ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
Africa/Brazzaville
Africa/Bujumbura
Africa/Cairo
Africa/Casablanca
Africa/Ceuta
Africa/Conakry
Africa/Dakar
Africa/Dar_es_Salaam
Africa/Djibouti
Africa/Douala
Africa/El_Aaiun
Africa/Freetown
Africa/Gaborone
Africa/Harare
Africa/Johannesburg
Africa/Juba
Africa/Kampala
Africa/Khartoum
Africa/Kigali
Africa/Kinshasa
Africa/Lagos
Africa/Libreville
Africa/Lome
Africa/Luanda
Africa/Lubumbashi
Africa/Lusaka
Africa/Malabo
Africa/Maputo
Africa/Maseru
Africa/Mbabane
Africa/Mogadishu
Africa/Monrovia
Africa/Nairobi
Africa/Ndjamena
lines 1-45
3.2.3 set-time
#修改时间
[root@bogon ~]# timedatectl set-time 2024-3-8
Failed to set time: Automatic time synchronization is enabled
#注:直接修改会提示失败,需要先关闭ntp服务
[root@bogon ~]# timedatectl status
Local time: Tue 2024-10-01 10:49:19 CST
Universal time: Tue 2024-10-01 02:49:19 UTC
RTC time: Mon 2025-05-05 14:46:18
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: active #NTP目前 active 活跃
RTC in local TZ: no
#关闭ntp服务
[root@localhost ~]# timedatectl set-ntp no
#查看NTP状态
[root@bogon ~]# timedatectl set-ntp no
[root@bogon ~]# timedatectl status
Local time: Tue 2024-10-01 10:51:48 CST
Universal time: Tue 2024-10-01 02:51:48 UTC
RTC time: Mon 2025-05-05 14:48:43
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive #NTP目前 inactive 不活跃,已关闭
RTC in local TZ: no
#修改时间
[root@bogon ~]# timedatectl set-time 2024-3-8
[root@bogon ~]# timedatectl status
Local time: Fri 2024-03-08 00:00:09 CST #修改成功
Universal time: Thu 2024-03-07 16:00:09 UTC
RTC time: Thu 2024-03-07 16:00:09
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
3.2.4 set-timezone
#修改时区
[root@bogon ~]# timedatectl set-timezone Africa/Asmara
[root@bogon ~]# timedatectl status
Local time: Thu 2024-03-07 19:12:15 EAT
Universal time: Thu 2024-03-07 16:12:15 UTC
RTC time: Thu 2024-03-07 16:12:16
Time zone: Africa/Asmara (EAT, +0300)
System clock synchronized: no
NTP service: inactive
RTC in local TZ: no
#注:如果需要修改时间和时区的话,需要先关闭ntp服务,修改完成之后,一定要记得将ntp服务开启
[root@bogon ~]# timedatectl set-ntp yes
[root@bogon ~]# timedatectl status
Local time: Mon 2025-05-05 18:02:24 EAT
Universal time: Mon 2025-05-05 15:02:24 UTC
RTC time: Thu 2024-03-07 16:12:35
Time zone: Africa/Asmara (EAT, +0300)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
练习:将时区改为上海,将时间改为系统默认的时间
[root@bogon ~]# timedatectl set-ntp no
[root@bogon ~]# timedatectl set-time 2025-05-05
[root@bogon ~]# timedatectl set-timezone Asia/Shanghai
[root@bogon ~]# timedatectl set-ntp yes
[root@bogon ~]# timedatectl status
Local time: Mon 2025-05-05 05:00:20 CST
Universal time: Sun 2025-05-04 21:00:20 UTC
RTC time: Sun 2025-05-04 21:00:21
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: active
RTC in local TZ: no
3.3 cal
看看cal用法:
[root@bogon ~]# cal --help
Usage:
cal [options] [[[day] month] year]
cal [options] <timestamp|monthname>
Display a calendar, or some part of it.
Without any arguments, display the current month.
Options:
-1, --one show only a single month (default)
-3, --three show three months spanning the date
-n, --months <num> show num months starting with date's month
-S, --span span the date when displaying multiple months
-s, --sunday Sunday as first day of week
-m, --monday Monday as first day of week
-j, --julian use day-of-year for all calendars
--reform <val> Gregorian reform date (1752|gregorian|iso|julian)
--iso alias for --reform=iso
-y, --year show the whole year
-Y, --twelve show the next twelve months
-w, --week[=<num>] show US or ISO-8601 week numbers
-v, --vertical show day vertically instead of line
--color[=<when>] colorize messages (auto, always or never)
colors are enabled by default
-h, --help display this help
-V, --version display version
For more details see cal(1).
查看这个月日历
[root@bogon ~]# cal
May 2025
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
查看某年日历
#查看2025年日历
[root@bogon ~]# cal 2050
2050
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
30 31
查看某一年某一月日历
#查看2030年5月日历
[root@bogon ~]# cal 5 2030
May 2030
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
4. 用户切换
- 命令: su
作用:实现用户切换
看看su用法:
[root@bogon ~]# su --help
Usage:
su [options] [-] [<user> [<argument>...]]
Change the effective user ID and group ID to that of <user>.
A mere - implies -l. If <user> is not given, root is assumed.
Options:
-m, -p, --preserve-environment do not reset environment variables
-w, --whitelist-environment <list> don't reset specified variables
-g, --group <group> specify the primary group
-G, --supp-group <group> specify a supplemental group
-, -l, --login make the shell a login shell
-c, --command <command> pass a single command to the shell with -c
--session-command <command> pass a single command to the shell with -c
and do not create a new session
-f, --fast pass -f to the shell (for csh or tcsh)
-s, --shell <shell> run <shell> if /etc/shells allows it
-P, --pty create a new pseudo-terminal
-h, --help display this help
-V, --version display version
For more details see su(1).
用户切换
#从root用户切换到普通用户
[root@bogon ~]# su -l wuxinchun
[wuxinchun@bogon ~]$
#从普通用户切换到管理员用户(注:需要输入密码)
[wuxinchun@bogon ~]$ su -l root
Password:
[root@bogon ~]#
#或
[wuxinchun@bogon ~]$ su -l
Password:
[root@bogon ~]#
#注:如果su命令后面不带用户的话,默认会切换到root用户
知识补充:
– 用户的分类:管理员用户 和 普通用户
– 管理员用户:指的是root用户
– 普通用户:指的是自己创建的用户(博主的是wuxinchun)
– @前是当前登录用户的用户名 ,@后表示的是主机名称
– 用户切换成功之后,命令提示符发生了以下的变化:
①由[root@bogon ~]变成了[wuxinchun@bogon ~]
②后面的命令提示符由#变成了$(#表示的是管理员用户 $表示的是普通用户)
– 普通用户切换管理员用户需输密码,管理员用户到普通用户不需要输密码
带-l和不带-l的区别:
#直接打su wuxinchun
[root@bogon ~]# su wuxinchun
[wuxinchun@bogon root]$
#带-l
[wuxinchun@bogon ~]$
#不带-l
[wuxinchun@bogon root]$
#注:带 -l 在切换用户的同时也会切换用户的工作环境。不带 -l 只切换用户。(一般要带-l)
如果切换用户后,需要回到之前的用户改怎么办?
方法一:可以通过su命令切换回去
方法二:可以使用exit命令来退出当前用户的登录状态
[root@bogon ~]# exit
logout
[wuxinchun@bogon root]$ exit
exit
5. 设置主机名称
- 命令: hostnamectl
作用:为了区分,以及后面会搭建级群的时候可以更好的标识一个虚拟机。
看看hostnamect用法
[root@bogon ~]# hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...
Query or change system hostname.
Commands:
status Show current hostname settings
hostname [NAME] Get/set system hostname
icon-name [NAME] Get/set icon name for host
chassis [NAME] Get/set chassis type for host
deployment [NAME] Get/set deployment environment for host
location [NAME] Get/set location for host
Options:
-h --help Show this help
--version Show package version
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--transient Only set transient hostname
--static Only set static hostname
--pretty Only set pretty hostname
--json=pretty|short|off
Generate JSON output
See the hostnamectl(1) man page for details.
主机名称相关操作
#查看当前的主机名称
由“ hostname [NAME] Get/set system hostname”可知
[root@bogon ~]# hostnamectl hostname
bogon #博主的主机名称为bogon
#将主机名称修改为redhat
[root@bogon ~]# hostnamectl hostname redhat
[root@bogon ~]# hostnamectl hostname
redhat
#注:想命令提示符[root@bogon ~]#显示的主机名称也改变,则须重新加载Xshell会话
[root@redhat ~]# #重启后改变
6. 获取当前的路径
- 命令: pwd
作用:可以获取当前用户所在的路径
#获取当前路径
[root@redhat ~]# pwd
/root
#注:当前路径为root
#切换路径
[root@redhat ~]# cd /home #相当于我们Windows操作系统的路径,后续会细讲
[root@redhat home]# pwd
/home