【90 天挑战 HCIE Cloud】第 1 天 Linux 开启云端之门

举报
齐月太白 发表于 2021/08/04 23:42:40 2021/08/04
【摘要】 1991 年 Linus Torvalds 播下的一颗开源操作系统的种子 - Linux,在今天终于成为云和数字时代众多领域的基石,据统计,公有云上的服务器实例,操作系统的 90% 采用的是 Linux 的各类发型版本,这个比例还在不断地上涨,毫不夸张的说,上云就得跟 Linux 打交道,使用 Linux,就是我们开启云端数字化之旅的第一步。

为什么是 Linux

1991 年 Linus Torvalds 播下的一颗开源操作系统的种子 - Linux,在今天终于成为云和数字时代众多领域的基石,据统计,公有云上的服务器实例,操作系统的 90% 采用的是 Linux 的各类发行版本,这个比例还在不断地上涨,毫不夸张的说,上云就得跟 Linux 打交道,使用 Linux,就是我们开启云端数字化之旅的第一步

因此,90 天挑战 HCIE Cloud,我们将从 Linux 操作系统的使用要求开始,打开云端之门,逐步展开后续的旅程

发行版本

因为本系列文章的主题是 “90 天挑战 HCIE Cloud”,Linux 发行版本的选择自然就是 openEuler,这是目前华为云力推的 Linux 发行版,参考下图

image.png

部分网友没有购买云端的 ECS 服务,又想体验下 openEuler 系统,怎么办呢?一个替代方案是 WSL (Windows Subsystem for Linux),是微软在近几年拥抱开源的又一创举,让我们在安装 Windows 的笔记本电脑上即可无缝体验 Linux,步骤如下:

  • 在安装了 Win 10 的电脑上,打开 Microsoft Store
  • 搜索 openEuler,下载并完成安装,参考下面截图

image.png

作为一个 HCIE Cloud 专家,对于 Linux 系统要掌握到什么程度,后面会按照主题展开,针对每个主题,以出题的方式,列出在 Linux 上用户需要完成的任务,考察对于 Linux 基本命令是否熟悉,以及是否有能力完成特定任务如建站、建论坛和安装数据库等

注:Linux 是众多开发者开源协作的成果,Linux 系列的学习内容,计划以众筹的方式,邀请各位网友、读者一起参与

  • 在评论区,给出你对相关问题的解答(如评论区已有正确解答,请勿重复作答,请勿灌水)
  • 分享关于 Linux 常用的小 Tips、小功能点,后面将定期合入本章节
  • 使用 Linux 过程中遇到的难题,或者就是单纯出一个题目,考一考作者和其他网友
    积极参与的网友,将有机会赢得点对点 HCIE Cloud 辅导机会,说不定能帮助提高 10 分~

下面按照不同主题,分别介绍 Linux 的基础能力要求,主要是一些常用命令

注:所有命令,均在华为云 ECS 服务预装的 openEuler 20.03 操作系统上执行

文件系统 FileSystem (FS)

FS_001 常用操作

想象一下,自己平时在 Windows 系统上,对文件会执行哪些操作呢?请列出下面操作的命令示例,如 mv file1 file2

  • 文件夹遍历、文件信息浏览
  • 文件和文件夹的创建和删除
  • 文件和文件夹的移动、复制(没有黏贴,Linux 系统默认是不带剪贴板的)
  • 文件和文件夹的属性查询,如大小、文件类型、修改日期等

FS_002 压缩与解压

Linux 完成压缩和解压的命令有好几个,参考下表,针对每种命令,请列出压缩和解压的典型用法(含典型参数)

命令 文件后缀名 说明
zip unzip *.zip 压缩操作后保留源文件,支持目录压缩
bzip2 bunzip2 *.bz2 可选择是否保留源文件,支持目录压缩
gzip gunzip *.gz 执行压缩操作后,原文件将删除,不支持目录压缩
tar *.tar.gz *.tar.bz2 *.tar 最常用的打包和压缩命令,支持多种压缩格式,支持目录压缩

FS_003 目录结构

Linux 采用的以根目录为核心的树形目录结构,根目录下的文件夹都有特别的用途,请说明不同文件夹的主要用途是什么

[root@hcie ~]# ls /
bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

有一些 Linux 目录非常重要,就像 C:\Program Files 之于 Windows,这些目录决定了 Linux 系统的运作、管理和运维模式,下面将列出部分重要的目录示例:

  • /boot/vmlinuz-4.19.90-2003.4.0.0036.oe1.x86_64 - 系统内核文件,计算机系统读完 BIOS 并载入 MBR 开机程序后,该文件将加载到内存中运行
  • /lib/modules/$(uname -r)/kernel/ - 系统内核启动时加载的核心模块
  • /bin - 最常用的内核程序,注意这里面的软件都是 symbol 链接文件,实际指向都是 /usr/bin 下的目录
  • /etc - Linux 的配置中心,在管理系统时经常会用到,非常多的功能源自这个目录,如网络配置、用户和密码管理、定时任务、系统启动方式、防火墙、文件自动挂载、字体配置、日期和时间相关配置… 这个名单还可以列的很长,等着我们不断地去发现~

上面 $(uname -r) 的语法是什么意思,请自行 Google

FS_004 文件类型

Linux 文件类型包括 3 种:目录文件(没错,Linux 目录也是一种文件)、普通文件、链接文件、字符设备文件、块设备文件、套接字文件和管道文件,最常用的是前面 3 种,请指出下面 /etc /dev 两个目录下,包含了哪几种文件类型

下面列出了 /etc 中关键目录,Linux 配置涉及的大部分功能,都包括在内,请看一下用过哪些,逐步了解所有配置项的用法

[root@hcie ~]# ll /etc
-rw-r--r--.  1 root root   3.0K May 18  2020 bashrc
drwxr-xr-x.  2 root root   4.0K May 18  2020 bluetooth
drwxr-xr-x.  2 root root   4.0K Mar 24  2020 chkconfig.d
drwx------   4 root root   4.0K May 18  2020 cloud
-rw-------   1 root root      0 May 18  2020 cron.allow
drwx------.  2 root root   4.0K May 18  2020 cron.d
drwx------.  2 root root   4.0K May 18  2020 cron.daily
drwx------.  2 root root   4.0K May 18  2020 cron.hourly
drwx------.  2 root root   4.0K Mar 24  2020 cron.monthly
-rw-------.  1 root root    451 Mar 24  2020 crontab
drwx------.  2 root root   4.0K Mar 24  2020 cron.weekly
-rw-r--r--.  1 root root     70 Mar 24  2020 filesystems
drwxr-x---.  7 root root   4.0K May 18  2020 firewalld
drwxr-xr-x.  3 root root   4.0K May 18  2020 fonts
-rw-r--r--.  1 root root    427 May 18  2020 fstab
-rw-r--r--.  1 root root    594 May 18  2020 group
----------.  1 root root    471 May 18  2020 gshadow
-rw-r--r--.  1 root root      9 Apr  9  2019 host.conf
-rw-r--r--   1 root root      9 Aug 14 10:27 hostname
-rw-r--r--.  1 root root    177 Aug 14 10:27 hosts
lrwxrwxrwx.  1 root root     11 Mar 24  2020 init.d -> rc.d/init.d
-rw-r--r--.  1 root root    490 Mar 24  2020 inittab
-rw-r--r--.  1 root root    943 Apr  9  2019 inputrc
drwxr-xr-x.  2 root root   4.0K May 18  2020 iproute2
drwxr-xr-x.  2 root root   4.0K Mar 24  2020 ipset
-rw-r--r--   1 root root     70 May 18  2020 issue
-rw-r--r--   1 root root     70 May 18  2020 issue.net
drwxr-xr-x.  4 root root   4.0K May 18  2020 kernel
-rw-r--r--.  1 root root     17 Aug 14 10:27 locale.conf
lrwxrwxrwx.  1 root root     35 May 18  2020 localtime -> ../usr/share/zoneinfo/Asia/Shanghai
-r--r--r--.  1 root root     33 May 18  2020 machine-id
-rw-r--r--.  1 root root    846 Mar 24  2020 mke2fs.conf
drwxr-xr-x.  2 root root   4.0K May 18  2020 modprobe.d
drwxr-xr-x.  2 root root   4.0K Mar 24  2020 modules-load.d
drwxr-xr-x.  2 root root   4.0K Mar 24  2020 my.cnf.d
-rw-r--r--.  1 root root    767 Aug 27  2018 netconfig
drwxr-xr-x.  7 root root   4.0K May 18  2020 NetworkManager
-rw-r--r--.  1 root root     58 Apr  9  2019 networks
drwxr-xr-x.  2 root root   4.0K May 18  2020 nftables
-r--r--r--.  1 root root    181 Mar 25  2020 openEuler-latest
-rw-r--r--.  1 root root     30 Mar 24  2020 openEuler-release
-rw-r--r--.  1 root root    128 Mar 24  2020 os-release
-rw-r--r--.  1 root root   1.3K May 18  2020 passwd
drwxr-xr-x. 13 root root   4.0K Aug 14 11:16 pki
-rw-r--r--.  1 root root   2.1K Apr  9  2019 profile
drwxr-xr-x.  2 root root   4.0K May 18  2020 profile.d
-rw-r--r--.  1 root root   6.5K Apr  9  2019 protocols
lrwxrwxrwx.  1 root root     10 Mar 24  2020 rc0.d -> rc.d/rc0.d
lrwxrwxrwx.  1 root root     10 Mar 24  2020 rc1.d -> rc.d/rc1.d
lrwxrwxrwx.  1 root root     10 Mar 24  2020 rc2.d -> rc.d/rc2.d
lrwxrwxrwx.  1 root root     10 Mar 24  2020 rc3.d -> rc.d/rc3.d
lrwxrwxrwx.  1 root root     10 Mar 24  2020 rc4.d -> rc.d/rc4.d
lrwxrwxrwx.  1 root root     10 Mar 24  2020 rc5.d -> rc.d/rc5.d
lrwxrwxrwx.  1 root root     10 Mar 24  2020 rc6.d -> rc.d/rc6.d
drwxr-xr-x. 10 root root   4.0K Mar 24  2020 rc.d
lrwxrwxrwx.  1 root root     13 Mar 24  2020 rc.local -> rc.d/rc.local
-rw-r--r--   1 root root    133 Aug 14 10:27 resolv.conf
drwxr-xr-x.  6 root root   4.0K May 18  2020 security
-rw-r--r--.  1 root root   677K Apr  9  2019 services
----------   1 root root    740 May 18  2020 shadow
drwxr-xr-x.  3 root root   4.0K Aug 14 10:27 ssh
drwxr-xr-x.  2 root root   4.0K May 18  2020 ssl
-r--r-----.  1 root root   1.4K Jan 12  2020 sudoers
drwxr-x---.  2 root root   4.0K Mar 24  2020 sudoers.d
drwxr-xr-x.  5 root root   4.0K May 18  2020 sysconfig
-rw-r--r--   1 root root   1.2K May 18  2020 sysctl.conf
drwxr-xr-x.  2 root root   4.0K May 18  2020 sysctl.d
drwxr-xr-x.  5 root root   4.0K May 18  2020 systemd
lrwxrwxrwx.  1 root root     17 Mar 24  2020 system-release -> openEuler-release
-rw-r--r--.  1 root root     46 Mar 24  2020 system-release-cpe
-rw-r--r--.  1 root root    351 Feb 29  2020 vimrc
-rw-r--r--.  1 root root    351 Feb 29  2020 virc
-rw-r--r--.  1 root root   4.9K Aug 17  2018 wgetrc
drwxr-xr-x.  6 root root   4.0K May 18  2020 X11
drwxr-xr-x.  2 root root   4.0K May 18  2020 yum
lrwxrwxrwx.  1 root root     12 Mar 24  2020 yum.conf -> dnf/dnf.conf
drwxr-xr-x.  2 root root   4.0K Aug 14 11:22 yum.repos.d

[root@hcie ~]# ll /dev
total 0
drwxr-xr-x 6 root root         120 Aug 14 10:27 cpu
crw------- 1 root root     10,  62 Aug 14 10:27 cpu_dma_latency
crw------- 1 root root     10, 203 Aug 14 10:27 cuse
drwxr-xr-x 6 root root         120 Aug 14 10:27 disk
lrwxrwxrwx 1 root root          13 Aug 14 10:27 fd -> /proc/self/fd
lrwxrwxrwx 1 root root          28 Aug 14 10:27 log -> /run/systemd/journal/dev-log
crw-r----- 1 root kmem      1,   1 Aug 14 10:27 mem
crw------- 1 root root     10,  59 Aug 14 10:27 memory_bandwidth
drwxrwxrwt 2 root root          40 Aug 14 10:27 mqueue
drwxr-xr-x 2 root root          60 Aug 14 10:27 net
crw------- 1 root root     10,  61 Aug 14 10:27 network_latency
crw------- 1 root root     10,  60 Aug 14 10:27 network_throughput
crw-r----- 1 root kmem      1,   4 Aug 14 10:27 port
crw-rw-rw- 1 root tty       5,   0 Aug 14 10:27 tty
crw--w---- 1 root tty       4,   0 Aug 14 10:27 tty0
crw-rw---- 1 root tty       7,   0 Aug 14 10:27 vcs
crw-rw---- 1 root tty       7,   1 Aug 14 10:27 vcs1
brw-rw---- 1 root disk    253,   0 Aug 14 10:27 vda
brw-rw---- 1 root disk    253,   1 Aug 14 10:27 vda1

# file 指令可以查看指定文件类型
[root@hcie /]# file /etc/bashrc
/etc/bashrc: ASCII text
[root@hcie /]# file /dev/cpu
/dev/cpu: directory
[root@hcie /]# file /dev/fd
/dev/fd: symbolic link to /proc/self/fd
[root@hcie /]# file /dev/port
/dev/port: character special (1/4)
[root@hcie /]# file /dev/vda
/dev/vda: block special (253/0)

FS_005 文件搜索

熟练使用的话,Linux 文件搜索体验超出 Windows 一大截,简直不要太强大,请看强大的 find 命令支持哪些能力,写出示例语句,如 find / -name hcie_cloud 实现以下场景:

  • 支持指定路径搜索,如查找当前目录下的所有文件
  • 支持文件类型搜索
  • 支持使用通配符,如查找根目录下文件名含有 hello 的文件
  • 支持根据文件访问、修改时间查找,如查找根目录下边最近 10 分钟内被访问过或修改过的文件
  • 支持根据文件大小进行查找
  • 支持多个查询条件并行查找
  • 其他更多强大能力,请 man find 或 find --help

Linux 的命令行程序,让人不太容易“看到”要找的文件,但只要有一点蛛丝马迹,在 find 强大的探查能力下也无处可藏

FS_006 文件内容搜索

find 用于寻找文件,如果要在文件内部查找想要的内容,grep 就能派上用场,可以根据查询条件,按行搜索相应内容

请说明以下 grep 语句的含义

grep 'hcie cloud' file
grep '[a-z].target' file
grep '^hcie' file_a file_b file_c
grep 'hcie_cloud$' /home

FS_007 文件链接

Linux 链接有硬链接 Hard Link 和 软链接 Symbolic Link

  • 硬链接:Linux 文件系统中,所有的文件都会分配一个唯一的编号 inode,多个 inode 可以指向同一个文件,这就是硬链接,目录文件、普通文件等都属于硬链接,Linux 系统支持为一个文件指定多个硬链接,当一个硬链接删除时,文件并不会被删除,只有当文件 inode 对应的所有硬链接都删除了,文件才会从系统中释放
  • 软链接:又称符号链接,相当于 Windows 中的快捷方式,关联的是一个文件或目录的路径

两者差别如下:

对比项 硬链接 软链接
适用场景 关键文件保护,防止误删 提供文件访问的多个入口
执行命令 ln source_file target_file ln -s source_file symbolic_file
文件类型 目录文件 (d) 或普通文件 (-) 链接文件 (l)

请基于以上关于文件链接的说明,如果用户希望对 /etc/systemd 目录进行保护,保存路径在 /home/systemd_backup 下,避免用户误删文件,应该使用什么样的命令 ?

FS_008 绝对和相对路径

Linux 的整个目录是一个树形结构,根节点是 /,以根节点开始指定的路径都是绝对路径,如果从当前路径来找的就是相对路径,请参考下面指令,回答下面几个问题

[root@hcie doc]# cd /usr/share/doc
[root@hcie doc]# ll . | grep python2
drwxr-xr-x. 2 root root 4.0K May 18  2020 python2
drwxr-xr-x. 2 root root 4.0K May 18  2020 python2-devel

小测试:

  • 如何查看当前路径
  • 使用绝对路径,打开 python2 文件夹
  • 使用相对路径,打开 python2 文件夹
  • 使用相对路径,访问 /usr/bin/whoami 指令

通过上面的小测试,您是否能判断出,什么时候用绝对路径,什么时候用相对路径了呢?
简单来说:哪个快用哪个~ 访问根目录较近的目录或文件,使用绝对路径会快一些,如果是访问当前路径临近的目录或文件,则相对路径更高效

FS_009 $PATH

为什么有些命令的执行,如系统自带的 ls mv cp 等,不需要指定绝对路径或相对路径,奥秘就在环境变量 $PATH 里面

[root@hcie share]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

当 Linux 遇到一个未指定相对路径和绝对路径的指令时,会在 $PATH 下面的目录去搜索,找到指令所在的路径,并调用执行

[root@hcie sbin]# pwd
/usr/sbin
[root@hcie sbin]# ./fdisk -l
Disk /dev/vda: 120 GiB, 128849018880 bytes, 251658240 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x64860148

Device     Boot Start       End   Sectors  Size Id Type
/dev/vda1  *     2048 251658206 251656159  120G 83 Linux
[root@hcie sbin]# fdisk -l
Disk /dev/vda: 120 GiB, 128849018880 bytes, 251658240 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x64860148

Device     Boot Start       End   Sectors  Size Id Type
/dev/vda1  *     2048 251658206 251656159  120G 83 Linux

上面指令中,./fdisk -l 和 fdisk -l 的执行效果是完全一样的

小测试:如果要添加新的路径放到 $PATH 下,应该如何操作呢?

参考资料

鸟哥的 Linux 私房菜
《循序渐进 Linux》 作者高俊峰

目录和前后章节

90 天挑战 HCIE Cloud
上一章 - 写在前面
下一章 - Linux 开启云端之门(二)

实时了解更新,请关注微信公众号和微博 齐月太白

image.png

点击进入微博 齐月太白:https://weibo.com/moontaibai

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。