DevOps 最低要求 — Linux


这是我对成为一名 DevOps 工程师至少应该了解 Linux 的看法。

1.Linux文件系统及结构

根目录:了解根 (/) 目录和子目录(如 /etc/、/var/、/home/ 和 /bin/)的重要性和结构。

文件类型:识别标准文件类型,例如常规文件、目录、符号链接和设备文件。

路径和 PATH 变量:无论何时浏览文件系统,都可以使用绝对路径和相对路径。 您可以使用 pwd 命令来了解您所在的位置:

pwd
# output
/Users/flavius/Workspace/example

ls
# files/directories in the current directory
a   b   c   dir

例如,如果我想转到 dir 目录,我可以使用绝对路径或相对路径:

# Relative Path
cd dir

# Absolute Path
cd /Users/flavius/Workspace/example/dir

PATH 变量不是相对或绝对路径,它是 Linux 中至关重要的环境设置,决定系统在何处搜索可执行文件。

# Example setting of the PATH variable
export PATH=$PATH:/new/directory/path

文件权限:掌握用户、组和其他权限的概念,以及 chmod、chown 和 chgrp 等命令。 在进入这些命令的示例之前,让我们深入了解权限。

ls -la

drwxr-xr-x    6 flavius  staff   192 Aug 27 15:42 .
drwxr-xr-x  127 flavius  staff  4064 Aug 27 15:42 ..
-rw-r--r--    1 flavius  staff     0 Aug 27 15:42 a
-rw-r--r--    1 flavius  staff     0 Aug 27 15:42 b
-rw-r--r--    1 flavius  staff     0 Aug 27 15:42 c
drwxr-xr-x    2 flavius  staff    64 Aug 27 15:42 dir

通过使用 ls -la 选项,您可以轻松找到当前目录中的所有文件和目录(包括隐藏的文件和目录)及其权限。它们的权限是从行首开始的权限。 如果权限以“d”开头,则意味着该结构是一个目录。让我们看看其他字母代表什么:“r” 读取,“w” 写入,“x” 执行。

看一个例子,分析一下a文件的权限:rw-r--r--。 正如您所看到的,其中有 9 个字符。

前三个是文件所有者拥有的权限:rw-

接下来的三个是文件所有者所在组的权限:r--

最后三项是其他用户拥有的权限:r--

现在,请耐心等待,因为我们将涉及一些简单的数学知识,以便更轻松地更改权限。 如果您在这 3 个位置之一看到一个字母,则表示某个位已启用且值为 1(二进制),否则,该位已禁用且值为 0。

让我们举几个例子来让事情更容易理解:

r-- 转换为 100(二进制)

rw- 转换为 110(二进制)

r-x 转换为 101(二进制)

要将数字从二进制转换为十进制,最简单的方法是从右向左开始,乘以 2 的幂,然后将它们加在一起,如下所示:

基本上,为了使其尽可能简单,如果启用读取,则其值为 4,如果启用写入,则其值为 2,如果启用执行,则其值为 1。

现在,让我们回到具有 rw-r--r-- 权限的文件。

假设您想向该组授予写入权限,可以使用以下命令来实现:

# g+w translates to group gets write permissions
chmod g+w a

# using numbers (the first number corresponds to the owner, the second to the group, the third to others)
chmod 664 a

如果您想授予完全权限,您可以:

# Everyone had read, now we give to everyone write and execute permissions
chmod ugo+wx a

# using numbers
chmod 777 a

2. 命令行基础知识

基础知识:ls、cd、cat、echo、mkdir、rm

文件操作:cp、mv、find、tar 和 zip 用于处理和查找文件。

文本操作:grep、awk、sed 和 cut 对于文本处理非常有用。

网络实用程序:netstat、ifconfig(或 ip)、curl 和 ping 对于故障排除至关重要。

# List the contents of the current directory in a long listing format
ls -l

# List the contents of the current directory including hidden files or directories
ls -la

# Navigate to the 'documents' directory located in the 'user' home directory
cd /home/user/documents

# Go to the previous directory
cd ..

# Display the content of 'filename.txt' on the screen
cat filename.txt

# Display the phrase "Hello, World!" on the screen
echo "Hello, World!"

# Create a new directory named 'new_directory' in the current location
mkdir new_directory

# Delete the file named 'file_to_delete.txt'
rm file_to_delete.txt

# Recursively delete 'directory_to_delete' and all its contents (use with caution)
rm -r directory_to_delete

# Copy the file 'source.txt' to 'destination.txt'
cp source.txt destination.txt

# Move (or rename) the file 'oldname.txt' to 'newname.txt'
mv oldname.txt newname.txt

# Search for files in the '/home/user' directory with the name 'target.txt'
find /home/user -name target.txt

# Create a compressed archive named 'backup.tar.gz' of the 'backup' directory
tar -czvf backup.tar.gz backup/

# Create a compressed archive named 'archive.zip' containing the files 'file1.txt' and 'file2.txt'
zip archive.zip file1.txt file2.txt

# Search for the pattern "example" in 'filename.txt'
grep "example" filename.txt

# Print the second column of a file separated by commas
awk -F ',' '{print $2}' filename.txt

# Replace the first instance of the word "apple" with "orange" in 'filename.txt' and display the result
sed 's/apple/orange/' filename.txt

# Extract the third column of a file separated by colons
cut -d ':' -f 3 filename.txt

# Display all active network connections
netstat -a

# Display the configuration of all network interfaces
ifconfig

# Show information for all network interfaces
ip addr show

# Fetch the content of a website
curl www.example.com

# Send echo request packets to a domain to test connectivity
ping www.example.com

3. 包管理

不同的 Linux 发行版有自己的包管理器。 至少熟悉以下其中一项:

Debian/Ubuntu:apt-get 或 apt

RedHat/CentOS:yum 或 dnf

SUSE:zypper

# Installs a package.
apt-get/yum/dnf/zypper install package_name

# Removes a package.
apt-get/yum/dnf/zypper remove package_name

# Update packages
apt-get/yum/dnf/zypper update

4. 进程管理了解如何使用 ps、top、htop 和 Kill 等命令和工具列出、终止进程并确定进程的优先级非常重要。

# Display a snapshot of the current processes
ps aux

# Display a real-time sorted view of system processes
top

# Interactive process viewer, an improved version of top
htop

# Send a SIGTERM signal to a process with process ID (PID) of 1234
kill 1234

5. VI

VI 是几乎每个 UNIX 系统上默认提供的文本编辑器,几十年来一直是系统管理的主要内容。

它有不同的模式,但最相关的是:

正常模式 您在此模式下开始。 它允许导航、删除、复制和其他文本操作。

插入模式 添加新文本

命令模式 保存、退出等

# Open a file named example.txt with vi (the file gets created if it doesn't exist)
vi example.txt

# Inside vi:

# Enter Insert Mode
i

# Save changes and exit
:wq

# Exit without saving changes
:q!

# Search for the string example
/example

# Set line number
:set_number

6. 用户和组管理

能够轻松地添加、删除和修改用户和组(useradd、usermod、groupadd 等)并了解 /etc/passwd 和 /etc/group 文件。

# Add a new user named 'newuser' with a home directory
useradd -m newuser

# Modify 'newuser' to have the login shell as /bin/tcsh
usermod -s /bin/tcsh newuser

# Add a new group named 'newgroup'
groupadd newgroup

# Add 'newuser' to the 'newgroup'
usermod -aG newgroup newuser

7. Shell 脚本

基本熟练掌握脚本编写,使用 bash 可以自动执行许多任务。 您应该知道如何使用变量、循环和条件语句。

本教程非常适合学习 bash。

8. 日志记录:

了解日志文件通常存储的位置 (/var/log/) 以及可以帮助读取日志文件的工具,例如 less、tail、head。

# View contents of a file.txt with navigation capabilities
less file.txt

# Display the last 10 lines of file.txt
tail file.txt

# Display the first 10 lines of file.txt
head file.txt

# Display the last 5 lines of file.txt
tail -n 5 file.txt

# Display the first 5 lines of file.txt
head -n 5 file.txt

9. 网络基础知识:

了解基本的网络概念,例如 IP 寻址、子网、端口和协议。 您可以查看本教程以获取更多信息。

了解如何配置和管理网络接口、路由和防火墙规则。 /etc/network/interfaces、iptables 等工具和文件可能会在这里发挥作用。

10. Secure Shell (SSH):

SSH 是远程管理的基础。 了解如何使用 ssh 进行远程登录、scp 进行文件传输以及有关 SSH 密钥的概念。

您可以使用 ssh-keygen 命令生成 ssh 密钥。 这将创建一个名为 .ssh 的目录,并在其中创建两个密钥(公共密钥和私有密钥)。

如果您接受默认值,这些密钥的名称将为 id_rsa 和 id_rsa.pub。

现在您已经创建了密钥,您就可以连接到远程服务器了。 在远程服务器的 .ssh 目录中,可以创建一个名为authorized_keys 的特殊文件,如果您在其中添加公钥的内容,您将能够使用私钥连接到它。

如果您使用默认名称,则不需要指定私钥的路径,但否则,这是必须的:

ssh -i path_to_private_key user@server

11. 磁盘和存储:

了解如何查看磁盘使用情况(df、du)、管理分区和文件系统(fdisk、mkfs)以及挂载/卸载存储设备非常重要。

# Display the amount of disk space used and available on mounted filesystems
df -h

# Estimate file space usage for example_dir
du -sh example_dir/

# Display the sizes of files and directories within example_dir
du -ah example_dir/

Linux 确实很重要,要成为 DevOps 工程师,必须了解如何导航文件系统、权限、如何远程连接到服务器、在 bash 中编写脚本以及使用 vi。

展开阅读全文

页面更新:2024-05-04

标签:密钥   文件系统   变量   路径   文本   命令   权限   最低   模式   文件   目录

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top