Merge pull request #463 from wenchaoh997/zh_cn

zh_cn translation
This commit is contained in:
Michael Cade 2024-01-04 09:36:53 +00:00 committed by GitHub
commit d506339e4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 194 deletions

View File

@ -1,76 +1,54 @@
---
title: '#90DaysOfDevOps - The Big Picture: DevOps and Linux - Day 14'
title: '#90DaysOfDevOps - 概览: DevOps 与 Linux - 第十四天'
published: false
description: 90DaysOfDevOps - The Big Picture DevOps and Linux
description: 90DaysOfDevOps - 概览: DevOps 与 Linux
tags: 'devops, 90daysofdevops, learning'
cover_image: null
canonical_url: null
id: 1049033
---
## The Big Picture: DevOps and Linux
Linux and DevOps share very similar cultures and perspectives; both are focused on customization and scalability. Both of these aspects of Linux are of particular importance for DevOps.
## 概览: DevOps 与 Linux
Linux和DevOps有着非常相似的文化和观点两者都专注于定制化和可扩展性。Linux 的这两个方面对于 DevOps 来说都特别重要。
A lot of technologies start on Linux, especially if they are related to software development or managing infrastructure.
许多技术都是从Linux开始的特别是当它们与软件开发或基础设施管理有关的时候。
As well lots of open source projects, especially DevOps tools, were designed to run on Linux from the start.
此外许多开源项目尤其是DevOps工具从一开始就被设计为在Linux上运行。
From a DevOps perspective or in fact any operations role perspective you are going to come across Linux I would say mostly. There is a place for WinOps but the majority of the time you are going to be administering and deploying Linux servers.
从DevOps或任何运营角色的角度来看我会认为你将接触到Linux。WinOps是一种选择但大多数时候你需要管理和部署Linux服务器。
I have been using Linux on a daily basis for a number of years but my go to desktop machine has always been either macOS or Windows. However, when I moved into the Cloud Native role I am in now I took the plunge to make sure that my laptop was fully Linux based and my daily driver, whilst I still needed Windows for work-based applications and a lot of my audio and video gear does not run on Linux I was forcing myself to run a Linux desktop full time to get a better grasp of a lot of the things we are going to touch on over the next 7 days.
几年来我一直在使用Linux但我的桌面电脑一直是macOS或Windows。当我转变成为云原生角色Cloud Native role我需要确保我的笔记本电脑完全基于Linux和我的日常驱动程序我仍然需要Windows用于工作的应用程序而且我的许多音频和视频设备无法在Linux上运行我强迫自己只运行Linux桌面以更好地掌握很多东西。我们将在接下来的7天里进行讨论。
## Getting Started
I am not suggesting you do the same as me by any stretch as there are easier options and less destructive but I will say taking that full-time step forces you to learn faster on how to make things work on Linux.
For the majority of these 7 days, I am actually going to deploy a Virtual Machine in Virtual Box on my Windows machine. I am also going to deploy a desktop version of a Linux distribution, whereas a lot of the Linux servers you will be administering will likely be servers that come with no GUI and everything is shell-based. However, as I said at the start a lot of the tools that we covered throughout this whole 90 days started out on Linux I would also strongly encourage you to take the dive into running that Linux Desktop for that learning experience as well.
For the rest of this post, we are going to concentrate on getting a Ubuntu Desktop virtual machine up and running in our Virtual Box environment. Now we could just download [Virtual Box](https://www.virtualbox.org/) and grab the latest [Ubuntu ISO](https://ubuntu.com/download) from the sites linked and go ahead and build out our desktop environment but that wouldn't be very DevOps of us, would it?
## 开始
我并不是要求你和我一样完全使用Linux因为有更简单、破坏性更小的选择。但我会说只使用Linux可以迫使你更快地学习如何在Linux上工作。
在这7天的大部分时间里我将在Windows电脑上的Virtual Box中部署虚拟机。我打算部署Linux发行版的桌面版本而实际工作中你将管理的许多Linux服务器可能是没有GUI的服务器并且一切都是基于shell的。但是正如我在开始时所说我们在整个90天学习中介绍的许多工具都是从Linux开始的我也强烈建议你潜心使用该Linux桌面以获得学习体验。
在这篇文章的其余部分我们将专注于在我们的Virtual Box环境中启动并运行Ubuntu桌面虚拟机。现在我们可以下载[Virtual Box](https://www.virtualbox.org/)并从链接的站点获取最新的[Ubuntu ISO](https://ubuntu.com/download)然后继续构建我们的桌面环境但这一步对我们来说不是很DevOps对吧
Another good reason to use most Linux distributions is that they are free and open-source. We are also choosing Ubuntu as it is probably the most widely used distribution deployed not thinking about mobile devices and enterprise RedHat Enterprise servers. I might be wrong there but with CentOS and the history there I bet Ubuntu is high on the list and it's super simple.
使用大多数Linux发行版的另一个理由是它们是免费和开源的。我们选择Ubuntu因为它可能是使用最广泛的发行版目前没有考虑移动设备和企业RedHat Enterprise服务器。我可能是错的但对于 CentOS 和那里的历史我敢打赌Ubuntu是比较热门的而且它非常简单。
## 简介HashiCorp Vagrant
Vagrant 是一个CLI程序用于管理虚拟机的生命周期。我们可以使用vagrant在许多不同的平台上启动虚拟机包括vSphereHyper-vVirtual Box和Docker。它也有其他提供商我们将使用Virtual Box所以我们可以用到它。
## Introducing HashiCorp Vagrant
我们需要做的第一件事是在我们的机器上安装Vagrant当您转到下载页面时您将看到供你选择的所有操作系统。[HashiCorp Vagrant](https://www.vagrantup.com/downloads) 我正在使用Windows所以我下载了二进制文件并继续在我的系统上安装它。
接下来,我们还需要安装[Virtual Box](https://www.virtualbox.org/wiki/Downloads)。同样的它也可以被安装在许多不同的操作系统上。如果你运行的是WindowsmacOS或Linux那么可以选择它和vagrant我们的安装环节就到此为止。
这两个安装过程都非常简单,并且周围都有很棒的社区,所以如果你有问题,请随时联系他们,我也可以尝试提供帮助。
## 我们的第一个VAGRANTFILE
Vagrant is a CLI utility that manages the lifecycle of your virtual machines. We can use vagrant to spin up and down virtual machines across many different platforms including vSphere, Hyper-v, Virtual Box and also Docker. It does have other providers but we will stick with that we are using Virtual Box here so we are good to go.
VAGRANT文件描述了我们要部署的计算机类型还定义了此计算机的配置和预配。
在保存这些和组织你的VAGRANT文件时我倾向于将它们放在我工作区的文件夹中。您可以在下面看到这在我的系统上的外观。希望在此之后你将与Vagrant一起玩并体验启动不同系统的便利性这可以帮助你不断探索Linux桌面。
![](../../Days/Images/Day14_Linux1.png)
The first thing we need to do is get Vagrant installed on our machine, when you go to the downloads page you will see all the operating systems listed for your choice. [HashiCorp Vagrant](https://www.vagrantup.com/downloads) I am using Windows so I grabbed the binary for my system and went ahead and installed this to my system.
Next up we also need to get [Virtual Box](https://www.virtualbox.org/wiki/Downloads) installed. Again this can also be installed on many different operating systems again a good reason to choose this and vagrant is that if you are running Windows, macOS, or Linux then we have you covered here.
Both installations are pretty straightforward. If you have issues both have great communities around them also feel free to reach out and I can try to assist also.
## Our first VAGRANTFILE
The VAGRANTFILE describes the type of machine we want to deploy. It also defines how we want the configuration and provisioning of this machine need to look.
When it comes to saving these and organizing your VAGRANTFILEs I tend to put them in their own folders in my workspace. You can see below how this looks on my system. Hopefully following this you will play around with Vagrant and see the ease of spinning up different systems, it is also great for that rabbit hole is known as distro hopping for Linux Desktops.
![](Images/Day14_Linux1.png)
Let's take a look at that VAGRANTFILE then and see what we are building.
让我们看一下VAGRANT文件看看我们正在构建什么。
```
@ -93,12 +71,9 @@ end
```
This is a very simple VAGRANTFILE overall we are saying we want a specific "box" a box being possibly either a public image or private build of the system you are looking for. You can find a long list of "boxes" publicly available here in the [public catalog of Vagrant boxes](https://app.vagrantup.com/boxes/search)
Next line we are saying we want to use a specific provider in this case it is `VirtualBox` and then we want to define our machine's memory to `8GB and our number of CPUs to `4`. My experience also tells me that you may want to also add the following line if you experience display issues. This will set the video memory to what you want, I would ramp this right up to `128MB but depends on your system.
总的来说这是一个非常简单的VAGRANTFILE。我们想要一个特定的“盒子”一个盒子可能是您正在寻找的系统的公共映像或私有版本。您可以在[public catalog of Vagrant boxes](https://app.vagrantup.com/boxes/search) 中找到一系列公开的“盒子”。
下一行我们说我们想使用特定的程序,这次使用的是`VirtualBox`。我们还将机器的内存`8GB`和CPU的数量定义为`4`。根据我的经验如果您遇到问题您可能还需要添加以下行。这会将视频内存设置为您想要的我会将其提升到128MB ,但这取决于您的系统。
```
@ -107,66 +82,50 @@ v.customize ["modifyvm", :id, "--vram", ""]
```
I have also placed a copy of this specific vagrant file in the [Linux Folder](Linux/VAGRANTFILE)
我还在[Linux 文件夹](Linux/VAGRANTFILE) 中放置了这个特定vagrant文件的副本。
## 配置我们的 Linux 桌面
## Provisioning our Linux Desktop
现在我们已经准备好在工作站的终端中启动并运行我们的第一台机器。就我而言我正在Windows机器上使用PowerShell。导航到您的项目文件夹您将在其中找到您的VAGRANTFILE。到达那里后您可以输入命令`vagrant up`,如果一切正常,您将看到类似以下内容。
We are now ready to get our first machine up and running, in your workstations terminal. In my case I am using PowerShell on my Windows machine, navigate to your projects folder and where you will find your VAGRANTFILE. Once there you can type the command `vagrant up` and if everything is correct then you will see something like the below.
![](../../Days/Images/Day14_Linux2.png)
![](Images/Day14_Linux2.png)
此处要添加的另一样东西是,网络将设置为虚拟机上的网络`NAT`。在此阶段我们不需要了解NAT我计划在网络的章节中讨论它。这是在家庭网络上获取机器的简单按钮它也是[Virtual Box documentation](https://www.virtualbox.org/manual/ch06.html#network_nat)上的默认网络模式。你可以在虚拟盒子文档中找到更多信息。
Another thing to add here is that the network will be set to `NAT` on your virtual machine, at this stage we don't really need to know about NAT and I plan to have a whole session talking about in the next section about Networking. But know that it is the easy button when it comes to getting a machine on your home network, it is also the default networking mode on Virtual Box. You can find out more in the [Virtual Box documentation](https://www.virtualbox.org/manual/ch06.html#network_nat)
`vagrant up`完成后,我们现在可以使用`vagrant ssh`直接跳转到新 VM 的终端。
![](../../Days/Images/Day14_Linux3.png)
Once `vagrant up` is complete we can now use `vagrant ssh` to jump straight into the terminal of our new VM.
这是我们接下来的几天里将进行大部分探索的地方但我还想深入了解我为您的开发人员工作站所做的一些自定义当将其作为日常驱动程序运行时使你的工作变得更加简单。当然有一个很酷的非标准终端才像是在做DevOps对吧
![](Images/Day14_Linux3.png)
在Virtual Box中进行确认您应该会在选择VM时看到登录提示。
![](../../Days/Images/Day14_Linux4.png)
This is where we will do most of our exploring over the next few days but I also want to dive into some customizations for your developer workstation that I have done and it makes your life much simpler when running this as your daily driver, and of course, are you really in DevOps unless you have a cool nonstandard terminal?
But just to confirm in Virtual Box you should see the login prompt when you select your VM.
![](Images/Day14_Linux4.png)
Oh and if you made it this far and you have been asking "WHAT IS THE USERNAME & PASSWORD?"
如果你做到了这一步,你会问“用户名和密码是什么?”
- Username = vagrant
- Password = vagrant
明天我们将了解一些命令以及它们的作用,终端将成为实现一切的地方。
Tomorrow we are going to get into some of the commands and what they do, The terminal is going to be the place to make everything happen.
## Resources
## 相关资料
- [Learn the Linux Fundamentals - Part 1](https://www.youtube.com/watch?v=kPylihJRG70)
- [Linux for hackers (don't worry you don't need be a hacker!)](https://www.youtube.com/watch?v=VbEx7B_PTOE)
There are going to be lots of resources I find as we go through and much like the Go resources I am generally going to be keeping them to FREE content so we can all partake and learn here.
随着我们的学习我会发现很多资源就像Go资源一样我通常会将它们保留为免费内容以便我们都可以在这里参与和学习。
As I mentioned next up we will take a look at the commands we might be using on a daily whilst in our Linux environments.
正如我接下来会提到的我们将看看我们在Linux环境中可能每天使用的命令。
See you on [Day15](day15.md)
[第十五天](day15.md)见

View File

@ -1,208 +1,190 @@
---
title: '#90DaysOfDevOps - Linux Commands for DevOps (Actually everyone) - Day 15'
title: '#90DaysOfDevOps - DevOps 中的Linux命令 (通用) - 第十五天'
published: false
description: 90DaysOfDevOps - Linux Commands for DevOps (Actually everyone)
description: 90DaysOfDevOps - DevOps 中的Linux命令 (通用)
tags: "devops, 90daysofdevops, learning"
cover_image: null
canonical_url: null
id: 1048834
---
## Linux Commands for DevOps (Actually everyone)
## DevOps 中的Linux命令 (通用)
I mentioned it [yesterday](day14.md) that we are going to be spending a lot of time in the terminal with some commands to get stuff done.
我[昨天](day14.md)提到,我们将在终端上花费大量时间使用一些命令来完成工作。
I also mentioned that with our vagrant provisioned VM we can use `vagrant ssh` and gain access to our box. You will need to be in the same directory as we provisioned it from.
我还提到通过我们的vagran提供的VM我们可以使用`vagrant ssh`访问我们的盒子。你需要位于我们预配它的同一目录中。
For SSH you won't need the username and password, you will only need that if you decide to login to the Virtual Box console.
对于 SSH你不需要用户名和密码只有在您决定登录Virtual Box控制台时才需要用户名和密码。
This is where we want to be as per below:
这就是我们想要的地方,如图下所示:
![](Images/Day15_Linux1.png)
![](../../Days/Images/Day15_Linux1.png)
## Commands
## 命令
Obviously I cannot cover all the commands here, there are pages and pages of documentation that cover these but also if you are ever in your terminal and you just need to understand options to a specific command we have the `man` pages short for manual. We can use this to go through each of the commands we touch on during this post to find out more options for each one. We can run `man man` which will give you the help for manual pages. To escape the man pages you should press `q` for quit.
我无法涵盖此处的所有命令,有一页又一页的文档涵盖了这些命令,但是如果你在终端中并且只需要了解特定命令的选项,我们有`man`将手册的页面缩短。我们可以使用它来浏览我们在这篇文章中提到的每个命令,以找出每个命令的更多选项。我们可以运行`man man`它将为你提供手册页的帮助。若要退出手册页,你应该按`q`退出。
![](Images/Day15_Linux2.png)
![](Images/Day15_Linux3.png)
![](../../Days/Images/Day15_Linux2.png)
![](../../Days/Images/Day15_Linux3.png)
`sudo` If you are familar with Windows and the right click `run as administrator` we can think of `sudo` as very much this. When you run a command with this command you will be running it as `root` it will prompt you for the password before running the command.
`sudo`如果您熟悉Windows和右键单击`以管理员身份运行``sudo`意义与之类似。当你将此命令与你要运行的命令一起使用时,会在运行命令之前提示您输入密码,并以`root`的身份运行命令。
![](Images/Day15_Linux4.png)
![](../../Days/Images/Day15_Linux4.png)
For one off jobs like installing applications or services you might need that `sudo command` but what if you have several tasks to deal with and you want to live as `sudo` for a while? This is where you can use `sudo su` again the same as `sudo` once entered you will be prompted for your `root` password. In a test VM like ours this is fine but I would find it very hard for us to be rolling around as `root` for prolonged periods, bad things can happen. To get out of this elevated position you simply type in `exit`
对于安装应用程序或服务等一次性工作,您可能需要用到`sudo command`,但是如果您有多项任务要处理并且想`sudo`一段时间怎么办?你可以再次使用`sudo su`与使用`sudo`输入`root`密码相同的方法。在像我们这样的测试虚拟机中,这没什么问题。但我发现我们很难四处走动,因为 root 长时间可能会发生不好的事情。要摆脱这个更高的位置,您只需输入`exit`。
![](Images/Day15_Linux5.png)
![](../../Days/Images/Day15_Linux5.png)
I find myself using `clear` all the time, the `clear` command does exactly what it says it is going to clear the screen of all previous commands, putting your prompt to the top and giving you a nice clean workspace. Windows I think is `cls` in the .mdprompt.
我发现自己一直在使用`clear``clear`命令完全按照它所说的去做它将清除所有先前命令的显示内容将您的提示放在顶部并为您提供一个漂亮干净的工作区。我认为在Windows中的命令提示符cmd prompt是`cls`。
![](Images/Day15_Linux6.png)
![](../../Days/Images/Day15_Linux6.png)
Let's now look at some commands where we can actually create things within our system and then visualise them in our terminal, first of all we have `mkdir` this will allow us to create a folder in our system. With the following command we can create a folder in our home directory called Day15 `mkdir Day15`
现在让我们看一些命令,我们可以在其中实际创建系统内容,然后在终端中可视化它们,首先,我们有`mkdir`这将允许我们在系统中创建一个文件夹。使用以下命令,我们可以在主目录中创建一个名为 Day15 `mkdir Day15`的文件夹。
![](Images/Day15_Linux7.png)
![](../../Days/Images/Day15_Linux7.png)
With `cd` this allows us to change directory, so for us to move into our newly created directory we can do this with `cd Day15` tab can also be used to autocomplete the directory available. If we want to get back to where we started we can use `cd ..`
有了`cd`我们可以更改目录因此对于我们移动到新创建的目录。我们可以使用tab用于自动完成可用的目录`cd Day15`。如果我们想回到我们开始的地方,我们可以使用`cd ..`。
![](Images/Day15_Linux8.png)
![](../../Days/Images/Day15_Linux8.png)
`rmdir` allows for us to remove the directory, if we run `rmdir Day15` then the folder will be removed (note that this will only work if you have nothing in the folder)
`rmdir`允许我们删除目录,如果我们运行`rmdir Day15`,那么该文件夹将被删除(注意:这仅在文件夹中没有任何内容时才有效)
![](Images/Day15_Linux9.png)
![](../../Days/Images/Day15_Linux9.png)
I am sure we have all done it where we have navigated to the depths of our file system to a directory and not known where we are. `pwd` gives us the print out of the working directory, pwd as much as it looks like password it stands for print working directory.
我敢肯定,我们都在导航到文件系统深处的目录并且不知道我们在哪里。`pwd`为我们提供了工作目录的打印输出PWD就像密码的缩写它代表打印工作目录。
![](Images/Day15_Linux10.png)
![](../../Days/Images/Day15_Linux10.png)
We know how to create folders and directories but how do we create files? We can create files using the `touch` command if we were to run `touch Day15` this would create a file. Ignore `mkdir` we are going see this again later.
我们知道了如何创建文件夹和目录,但如何创建文件?如果我们运行`touch Day15`,我们将使用`touch`命令创建文件。忽略`mkdir`,稍后将再次看到这个命令。
![](Images/Day15_Linux11.png)
![](../../Days/Images/Day15_Linux11.png)
`ls` I can put my house on this, you will use this command so many times, this is going to list the all the files and folders in the current directory. Let's see if we can see that file we just created.
`ls`我很肯定,你会多次使用这个命令,这将列出当前目录中的所有文件和文件夹。让我们看看我们是否可以看到我们刚刚创建的文件。
![](Images/Day15_Linux12.png)
![](../../Days/Images/Day15_Linux12.png)
How can we find files on our Linux system? `locate` is going to allow us to search our file system. If we use `locate Day15` it will report back that location of the file. Bonus round is that if you know that the file does exist but you get a blank result then run `sudo updatedb` which will index all the files in the file system then run your `locate` again. If you do not have `locate` available to you, you can install it using this command `sudo apt install mlocate`
我们如何在Linux系统上找到文件`locate`将允许我们搜索我们的文件系统。如果我们使用它`locate Day15`,它将报告文件的位置。如果您知道该文件确实存在,但得到一个空白结果,则可以运行`sudo updatedb`将再次索引文件系统中的所有你的`locate`文件。如果您没有`locate`,可以使用此命令`sudo apt install mlocate`安装它
![](Images/Day15_Linux13.png)
![](../../Days/Images/Day15_Linux13.png)
What about moving files from one location to another? `mv` is going to allow you to move your files. Example `mv Day15 90DaysOfDevOps` will move your file to the 90DaysOfDevOps folder.
将文件从一个位置移动到另一个位置该怎么做?`mv`将允许你移动文件。示例:`mv Day15 90DaysOfDevOps`将文件移动到 90DaysOfDevOps 文件夹。
![](Images/Day15_Linux14.png)
![](../../Days/Images/Day15_Linux14.png)
We have moved our file but what if we want to rename it now to something else? We can do that using the `mv` command again... WOT!!!? yep we can simply use `mv Day15 day15` to change to upper case or we could use `mv day15 AnotherDay` to change it altogether, now use `ls` to check the file.
我们已经移动了文件,但是如果我们现在想将其重命名怎么办?我们可以再次使用该`mv`命令来执行此操作...是的,我们可以简单地用`mv Day15 day15`更改为大写,或者我们可以完全`mv day15 AnotherDay`更改它,现在用`ls`检查文件。
![](Images/Day15_Linux15.png)
![](../../Days/Images/Day15_Linux15.png)
Enough is enough, let's now get rid (delete)of our file and maybe even our directory if we have one created. `rm` simply `rm AnotherDay` will remove our file. We will also use quite a bit `rm -R` which will recursively work through a folder or location. We might also use `rm -R -f` to force the removal of all of those files. Spoiler if you run `rm -R -f /` add sudo to it and you can say goodbye to your system....!
现在让我们摆脱(删除)我们的文件,甚至我们的目录(如果我们创建了一个)。`rm AnotherDay`会删除我们的文件。我们还将经常使用`rm -R`的递归方式处理文件夹或位置。我们还可能使用`rm -R -f`强制删除所有这些文件。如果你运行`rm -R -f /`添加sudo你可以说和你的系统说再见....
![](Images/Day15_Linux16.png)
![](../../Days/Images/Day15_Linux16.png)
We have looked at moving files around but what if I just want to copy files from one folder to another, simply put its very similar to the `mv` command but we use `cp` so we can now say `cp Day15 Desktop`
我们已经了解移动文件,但是如果我只想将文件从一个文件夹复制到另一个文件夹,与`mv`命令非常相似,但我们使用`cp`,因此我们现在可以用`cp Day15 Desktop`。
![](Images/Day15_Linux17.png)
![](../../Days/Images/Day15_Linux17.png)
We have created folders and files but we haven't actually put any contents into our folder, we can add contents a few ways but an easy way is `echo` we can also use `echo` to print out a lot of things in our terminal, I personally use echo a lot to print out system variables to know if they are set or not at least. we can use `echo "Hello #90DaysOfDevOps" > Day15` and this will add this to our file. We can also append to our file using `echo "Commands are fun!" >> Day15`
我们已经创建了文件夹和文件,但我们没有将任何内容放入我们的文件夹中,我们可以通过几种方式添加内容,但一个简单的方法是`echo`我们还可以用`echo`在我们的终端中打印出很多东西。我经常使用echo来打印出系统变量以了解它们是否被设置。我们可以使用`echo "Hello #90DaysOfDevOps" > Day15`,这会将其添加到我们的文件中。我们还可以使用以下命令`echo "Commands are fun!" >> Day15`附加到我们的文件。
![](Images/Day15_Linux18.png)
![](../../Days/Images/Day15_Linux18.png)
Another one of those commands you will use a lot! `cat` short for concatenate. We can use `cat Day15` to see the contents inside the file. Great for quickly reading those configuration files.
您将经常使用其中的另一个命令!`cat`是连接词concatenate的缩写。我们可以用它来`cat Day15`查看文件内的内容。非常适合快速读取配置文件。
![](Images/Day15_Linux19.png)
![](../../Days/Images/Day15_Linux19.png)
If you have a long complex configuration file and you want or need to find something fast in that file vs reading every line then `grep` is your friend, this will allow us to search your file for a specific word using `cat Day15 | grep "#90DaysOfDevOps"`
如果你有一个很长的复杂配置文件,并且你希望或需要在该文件中找到快速内容而不是阅读每一行,那么`grep`就是你的朋友。我们能够使用`cat Day15 | grep "#90DaysOfDevOps"`。
![](Images/Day15_Linux20.png)
![](../../Days/Images/Day15_Linux20.png)
If you are like me and you use that `clear` command a lot then you might miss some of the commands previously ran, we can use `history` to find out all those commands we have run prior. `history -c` will remove the history.
如果您像我一样并且经常使用`clear`命令,那么您可能会丢失之前运行的一些命令,我们可以用来`history`找出我们之前运行过的所有`clear`命令。`history -c`将删除历史记录。
When you run `history` and you would like to pick a specific command you can use `!3` to choose the 3rd command in the list.
当您运行`history`并想要选择一个特定命令时,您可以使用该命令选择列表中的第三个命令`!3`。
You are also able to use `history | grep "Command` to search for something specific.
您还可以使用`history | grep "Command"`来搜索特定内容。
On servers to trace back when was a command executed, it can be useful to append the date and time to each command in the history file.
在服务器上追溯已执行的命令,将日期和时间附加到历史记录文件的每个命令中可能很有用。
The following system variable controls this behaviour:
以下系统变量控制此行为:
```
HISTTIMEFORMAT="%d-%m-%Y %T "
```
You can easily add to your bash_profile:
您可以轻松地添加到bash_profile
```
echo 'export HISTTIMEFORMAT="%d-%m-%Y %T "' >> ~/.bash_profile
```
So as useful to allow the history file grow bigger:
为了将历史记录文件变大:
```
echo 'export HISTSIZE=100000' >> ~/.bash_profile
echo 'export HISTFILESIZE=10000000' >> ~/.bash_profile
```
![](Images/Day15_Linux21.png)
![](../../Days/Images/Day15_Linux21.png)
Need to change your password? `passwd` is going allow us to change our password. Note that when you add your password in like this when it is hidden it will not be shown in `history` however if your command has `-p PASSWORD` then this will be visible in your `history`.
需要更改密码?`passwd`允许我们更改密码。请注意,当你像这样添加隐藏密码时,它将不会显示在`history`。如果你的命令有`-p PASSWORD`,那么这将显示在你的`history`。
![](Images/Day15_Linux22.png)
![](../../Days/Images/Day15_Linux22.png)
We might also want to add new users to our system, we can do this with `useradd` we have to add the user using our `sudo` command, we can add a new user with `sudo useradd NewUser`
我们可能还想在系统添加新用户,我们可以用到`useradd`。我们必须使用我们的`sudo`命令添加用户,我们可以添加一个新用户`sudo useradd NewUser`。
![](Images/Day15_Linux23.png)
![](../../Days/Images/Day15_Linux23.png)
Creating a group again requires `sudo` and we can use `sudo groupadd DevOps` then if we want to add our new user to that group we can do this by running `sudo usermod -a -G DevOps` `-a` is add and `-G` is group name.
再次创建一个组需要用到`sudo`,我们可以使用`sudo groupadd DevOps`。如果我们想将新用户添加到该组,我们可以通过运行`sudo usermod -a -G DevOps`来做到这一点。`-a`是添加,`-G`是组名。
![](Images/Day15_Linux24.png)
![](../../Days/Images/Day15_Linux24.png)
How do we add users to the `sudo` group, this would be a very rare occassion for this to happen but in order to do this it would be `usermod -a -G sudo NewUser`
我们如何将用户添加到`sudo`组中。这是非常罕见的情况,但要做到这一点可以使用`usermod -a -G sudo NewUser`。
### Permissions
### 权限
read, write and execute are the permissions we have on all of our files and folders on our Linux system.
读取、写入和执行是我们对Linux系统上所有文件和文件夹的权限。
A full list:
完整列表:
- 0 = 无 `---`
- 1 = 仅执行 `--X`
- 2 = 仅写入 `-W-`
- 3 = 写入和执行 `-WX`
- 4 = 只可读 `R--`
- 5 = 可读和执行 `R-X`
- 6 = 可读和写入 `RW-`
- 7 = 可读、写入和执行 `RWX`
- 0 = None `---`
- 1 = Execute only `--X`
- 2 = Write only `-W-`
- 3 = Write & Exectute `-WX`
- 4 = Read Only `R--`
- 5 = Read & Execute `R-X`
- 6 = Read & Write `RW-`
- 7 = Read, Write & Execute `RWX`
你还将看到`777`或`775`,这些数字表示上述列表相同的含义,但每个数字代表**用户 - 组 - 每个人**的权限。
You will also see `777` or `775` and these represent the same numbers as the list above but each one represents **User - Group - Everyone**
让我们看一下我们的文件。`ls -al Day15`你可以看到上面提到的3个组用户和组有读写但每个人都只有读。
Let's take a look at our file. `ls -al Day15` you can see the 3 groups mentioned above, user and group has read & write but everyone only has read.
![](../../Days/Images/Day15_Linux25.png)
![](Images/Day15_Linux25.png)
我们可以通过`chmod`更改此设置。如果你在系统上创建大量二进制文件,并且你需要提供执行这些二进制文件的能力,你可能会发现自己需要这样做。`chmod 750 Day15`,现在运行`ls -al Day15`。如果你想运行整个文件夹,那么你可以使用`-R`递归地做到这一点。
We can change this using `chmod` you might find yourself doing this if you are creating binaries a lot on your systems as well and you need to give the ability to execute those binaries. `chmod 750 Day15` now run `ls -al Day15` if you want to run this for a whole folder then you can use `-R` to recursively do that.
![](../../Days/Images/Day15_Linux26.png)
![](Images/Day15_Linux26.png)
如何更改文件的所有者?我们可以使用`chown`。如果我们想将Day15的所有权从用户`vagrant`更改为`NewUser`,我们可以再次用到`sudo chown NewUser Day15`,并根据实际情况使用`-R`。
What about changing the owner of the file? We can use `chown` for this operation, if we wanted to change the ownership of our `Day15` from user `vagrant` to `NewUser` we can run `sudo chown NewUser Day15` again `-R` can be used.
![](../../Days/Images/Day15_Linux27.png)
![](Images/Day15_Linux27.png)
当你有一个只需要特定的输出时,你会用到`awk`。比如运行`who`时,我们会得到包含信息的行,但也许我们只需要名称。我们可以运行`who | awk '{print $1}'`以获取第一列的列表。
A command that you will come across is `awk` where this comes in real use is when you have an output that you only need specific data from. like running `who` we get lines with information, but maybe we only need the names. We can run `who | awk '{print $1}'` to get just a list of that first column.
![](../../Days/Images/Day15_Linux28.png)
![](Images/Day15_Linux28.png)
如果你希望从标准输入中读取数据流,并生成和执行命令行。这意味着它可以接受命令的输出并将其作为另一个命令的输入,`xargs`是此一个有用的工具。例如我想要系统上所有Linux用户帐户的列表。运行`cut -d: -f1 < /etc/passwd`并获取我们在下面看到的长列表。
If you are looking to read streams of data from standard input, then generates and executes command lines; meaning it can take output of a command and passes it as argument of another command. `xargs` is a useful tool for this use case. If for example I want a list of all the Linux user accounts on the system I can run. `cut -d: -f1 < /etc/passwd` and get the long list we see below.
![](../../Days/Images/Day15_Linux29.png)
![](Images/Day15_Linux29.png)
如果我想压缩该列表,我可以通过`cut -d: -f1 < /etc/passwd | sort | xargs`,使用`xargs`来实现。
If I want to compact that list I can do so by using `xargs` in a command like this `cut -d: -f1 < /etc/passwd | sort | xargs`
![](../../Days/Images/Day15_Linux30.png)
![](Images/Day15_Linux30.png)
我也没有介绍`cut`,它允许我们从文件的每一行中删除部分内容。它可用于按字节位置、字符和字段剪切行的一部分。`cut -d " " -f 2 list.txt`命令允许我们删除我们拥有的第一个字母并仅显示我们的数字。这里有很多组合可以使用此命令,我确信我花了太多时间尝试使用此命令。而我可以手动提取数据,更快地完成这一操作。
I didn't mention the `cut` command either, this allows us to remove sections from each line of a file. It can be used to cut parts of a line by byte position, character and field. The `cut -d " " -f 2 list.txt` command allows us to remove that first letter we have and just display our numbers. There are so many combinations that can be used here with this command, I am sure I have spent too much time trying to use this command when I could have extracted data quicker manually.
![](Images/Day15_Linux31.png)
![](../../Days/Images/Day15_Linux31.png)
Also to note if you type a command and you are no longer with happy with it and you want to start again just hit control + c and this will cancel that line and start you fresh.
还要注意的是,如果你输入一个命令并且不再满意它,你想重新开始,只需按下`control + c`,这将取消该行并重新开始。
## Resources
## 相关资料
- [Learn the Linux Fundamentals - Part 1](https://www.youtube.com/watch?v=kPylihJRG70)
- [Linux for hackers (don't worry you don't need be a hacker!)](https://www.youtube.com/watch?v=VbEx7B_PTOE)
See you on [Day16](day16.md)
This is a pretty heavy list already but I can safely say that I have used all of these commands in my day to day, be it from an administering Linux servers or in my Linux Desktop, it is very easy when you are in Windows or macOS to navigate the UI but in Linux Servers they are not there, everything is done through the terminal.
[第十六天](day16.md)见
这是一个相当长的列表但我可以肯定地说我在日常中使用了所有这些命令。无论是从管理Linux服务器还是在我的Linux桌面上。在Windows或macOS中你有图像界面。但在Linux服务器中一切都是通过终端完成的。