mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-25 00:50:15 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
da4f4d54b1
172
published/20190107 Aliases- To Protect and Serve.md
Normal file
172
published/20190107 Aliases- To Protect and Serve.md
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (wxy)
|
||||||
|
[#]: reviewer: (wxy)
|
||||||
|
[#]: publisher: (wxy)
|
||||||
|
[#]: url: (https://linux.cn/article-10918-1.html)
|
||||||
|
[#]: subject: (Aliases: To Protect and Serve)
|
||||||
|
[#]: via: (https://www.linux.com/blog/learn/2019/1/aliases-protect-and-serve)
|
||||||
|
[#]: author: (Paul Brown https://www.linux.com/users/bro66)
|
||||||
|
|
||||||
|
命令别名:保护和服务
|
||||||
|
======
|
||||||
|
|
||||||
|
> Linux shell 允许你将命令彼此链接在一起,一次触发执行复杂的操作,并且可以对此创建别名作为快捷方式。
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
让我们将继续我们的别名系列。到目前为止,你可能已经阅读了我们的[关于别名的第一篇文章][1],并且应该非常清楚它们是如何为你省去很多麻烦的最简单方法。例如,你已经看到它们帮助我们减少了输入,让我们看看别名派上用场的其他几个案例。
|
||||||
|
|
||||||
|
### 别名即快捷方式
|
||||||
|
|
||||||
|
Linux shell 最美妙的事情之一是可以使用数以万计的选项和把命令连接在一起执行真正复杂的操作。好吧,也许这种美丽是在旁观者的眼中的,但是我们觉得这个功能很实用。
|
||||||
|
|
||||||
|
不利的一面是,你经常需要记得难以记忆或难以打字出来的命令组合。比如说硬盘上的空间非常宝贵,而你想要做一些清洁工作。你的第一步可能是寻找隐藏在你的家目录里的东西。你可以用来判断的一个标准是查找不再使用的内容。`ls` 可以帮助你:
|
||||||
|
|
||||||
|
```
|
||||||
|
ls -lct
|
||||||
|
```
|
||||||
|
|
||||||
|
上面的命令显示了每个文件和目录的详细信息(`-l`),并显示了每一项上次访问的时间(`-c`),然后它按从最近访问到最少访问的顺序排序这个列表(`-t`)。
|
||||||
|
|
||||||
|
这难以记住吗?你可能不会每天都使用 `-c` 和 `-t` 选项,所以也许是吧。无论如何,定义一个别名,如:
|
||||||
|
|
||||||
|
```
|
||||||
|
alias lt='ls -lct'
|
||||||
|
```
|
||||||
|
|
||||||
|
会更容易一些。
|
||||||
|
|
||||||
|
然后,你也可能希望列表首先显示最旧的文件:
|
||||||
|
|
||||||
|
```
|
||||||
|
alias lo='lt -F | tac'
|
||||||
|
```
|
||||||
|
|
||||||
|
![aliases][3]
|
||||||
|
|
||||||
|
*图 1:使用 lt 和 lo 别名。*
|
||||||
|
|
||||||
|
这里有一些有趣的事情。首先,我们使用别名(`lt`)来创建另一个别名 —— 这是完全可以的。其次,我们将一个新参数传递给 `lt`(后者又通过 `lt` 别名的定义传递给了 `ls`)。
|
||||||
|
|
||||||
|
`-F` 选项会将特殊符号附加到项目的名称后,以便更好地区分常规文件(没有符号)和可执行文件(附加了 `*`)、目录文件(以 `/` 结尾),以及所有链接文件、符号链接文件(以 `@` 符号结尾)等等。`-F` 选项是当你回归到单色终端的日子里,没有其他方法可以轻松看到列表项之间的差异时用的。在这里使用它是因为当你将输出从 `lt` 传递到 `tac` 时,你会丢失 `ls` 的颜色。
|
||||||
|
|
||||||
|
第三件我们需要注意的事情是我们使用了管道。管道用于你将一个命令的输出传递给另外一个命令时。第二个命令可以使用这些输出作为它的输入。在包括 Bash 在内的许多 shell 里,你可以使用管道符(`|`) 来做传递。
|
||||||
|
|
||||||
|
在这里,你将来自 `lt -F` 的输出导给 `tac`。`tac` 这个命令有点玩笑的意思,你或许听说过 `cat` 命令,它名义上用于将文件彼此连接(con`cat`),而在实践中,它被用于将一个文件的内容打印到终端。`tac` 做的事情一样,但是它是以逆序将接收到的内容输出出来。明白了吗?`cat` 和 `tac`,技术人有时候也挺有趣的。
|
||||||
|
|
||||||
|
`cat` 和 `tac` 都能输出通过管道传递过来的内容,在这里,也就是一个按时间顺序排序的文件列表。
|
||||||
|
|
||||||
|
那么,在有些离题之后,最终我们得到的就是这个列表将当前目录中的文件和目录以新鲜度的逆序列出(即老的在前)。
|
||||||
|
|
||||||
|
最后你需要注意的是,当在当前目录或任何目录运行 `lt` 时:
|
||||||
|
|
||||||
|
```
|
||||||
|
# 这可以工作:
|
||||||
|
lt
|
||||||
|
# 这也可以:
|
||||||
|
lt /some/other/directory
|
||||||
|
```
|
||||||
|
|
||||||
|
……而 `lo` 只能在当前目录奏效:
|
||||||
|
|
||||||
|
```
|
||||||
|
# 这可工作:
|
||||||
|
lo
|
||||||
|
# 而这不行:
|
||||||
|
lo /some/other/directory
|
||||||
|
```
|
||||||
|
|
||||||
|
这是因为 Bash 会展开别名的组分。当你键入:
|
||||||
|
|
||||||
|
```
|
||||||
|
lt /some/other/directory
|
||||||
|
```
|
||||||
|
|
||||||
|
Bash 实际上运行的是:
|
||||||
|
|
||||||
|
```
|
||||||
|
ls -lct /some/other/directory
|
||||||
|
```
|
||||||
|
|
||||||
|
这是一个有效的 Bash 命令。
|
||||||
|
|
||||||
|
而当你键入:
|
||||||
|
|
||||||
|
```
|
||||||
|
lo /some/other/directory
|
||||||
|
```
|
||||||
|
|
||||||
|
Bash 试图运行:
|
||||||
|
|
||||||
|
```
|
||||||
|
ls -lct -F | tac /some/other/directory
|
||||||
|
```
|
||||||
|
|
||||||
|
这不是一个有效的命令,主要是因为 `/some/other/directory` 是个目录,而 `cat` 和 `tac` 不能用于目录。
|
||||||
|
|
||||||
|
### 更多的别名快捷方式
|
||||||
|
|
||||||
|
* `alias lll='ls -R'` 会打印出目录的内容,并深入到子目录里面打印子目录的内容,以及子目录的子目录,等等。这是一个查看一个目录下所有内容的方式。
|
||||||
|
* `mkdir='mkdir -pv'` 可以让你一次性创建目录下的目录。按照 `mkdir` 的基本形式,要创建一个包含子目录的目录,你必须这样:
|
||||||
|
|
||||||
|
```
|
||||||
|
mkdir newdir
|
||||||
|
mkdir newdir/subdir
|
||||||
|
```
|
||||||
|
|
||||||
|
或这样:
|
||||||
|
|
||||||
|
```
|
||||||
|
mkdir -p newdir/subdir
|
||||||
|
```
|
||||||
|
|
||||||
|
而用这个别名你将只需要这样就行:
|
||||||
|
|
||||||
|
```
|
||||||
|
mkdir newdir/subdir
|
||||||
|
```
|
||||||
|
|
||||||
|
你的新 `mkdir` 也会告诉你创建子目录时都做了什么。
|
||||||
|
|
||||||
|
### 别名也是一种保护
|
||||||
|
|
||||||
|
别名的另一个好处是它可以作为防止你意外地删除或覆写已有的文件的保护措施。你可能听说过这个 Linux 新用户的传言,当他们以 root 身份运行:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm -rf /
|
||||||
|
```
|
||||||
|
|
||||||
|
整个系统就爆了。而决定输入如下命令的用户:
|
||||||
|
|
||||||
|
```
|
||||||
|
rm -rf /some/directory/ *
|
||||||
|
```
|
||||||
|
|
||||||
|
就很好地干掉了他们的家目录的全部内容。这里不小心键入的目录和 `*` 之间的那个空格有时候很容易就会被忽视掉。
|
||||||
|
|
||||||
|
这两种情况我们都可以通过 `alias rm='rm -i'` 别名来避免。`-i` 选项会使 `rm` 询问用户是否真的要做这个操作,在你对你的文件系统做出不可弥补的损失之前给你第二次机会。
|
||||||
|
|
||||||
|
对于 `cp` 也是一样,它能够覆盖一个文件而不会给你任何提示。创建一个类似 `alias cp='cp -i'` 来保持安全吧。
|
||||||
|
|
||||||
|
### 下一次
|
||||||
|
|
||||||
|
我们越来越深入到了脚本领域,下一次,我们将沿着这个方向,看看如何在命令行组合命令以给你真正的乐趣,并可靠地解决系统管理员每天面临的问题。
|
||||||
|
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.linux.com/blog/learn/2019/1/aliases-protect-and-serve
|
||||||
|
|
||||||
|
作者:[Paul Brown][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[wxy](https://github.com/wxy)
|
||||||
|
校对:[wxy](https://github.com/wxy)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://www.linux.com/users/bro66
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://linux.cn/article-10377-1.html
|
||||||
|
[2]: https://www.linux.com/files/images/fig01png-0
|
||||||
|
[3]: https://www.linux.com/sites/lcom/files/styles/rendered_file/public/fig01_0.png?itok=crqTm_va (aliases)
|
||||||
|
[4]: https://www.linux.com/licenses/category/used-permission
|
@ -1,176 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: ( )
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: subject: (Aliases: To Protect and Serve)
|
|
||||||
[#]: via: (https://www.linux.com/blog/learn/2019/1/aliases-protect-and-serve)
|
|
||||||
[#]: author: (Paul Brown https://www.linux.com/users/bro66)
|
|
||||||
|
|
||||||
Aliases: To Protect and Serve
|
|
||||||
======
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
Happy 2019! Here in the new year, we’re continuing our series on aliases. By now, you’ve probably read our [first article on aliases][1], and it should be quite clear how they are the easiest way to save yourself a lot of trouble. You already saw, for example, that they helped with muscle-memory, but let's see several other cases in which aliases come in handy.
|
|
||||||
|
|
||||||
### Aliases as Shortcuts
|
|
||||||
|
|
||||||
One of the most beautiful things about Linux's shells is how you can use zillions of options and chain commands together to carry out really sophisticated operations in one fell swoop. All right, maybe beauty is in the eye of the beholder, but let's agree that this feature published practical.
|
|
||||||
|
|
||||||
The downside is that you often come up with recipes that are often hard to remember or cumbersome to type. Say space on your hard disk is at a premium and you want to do some New Year's cleaning. Your first step may be to look for stuff to get rid off in you home directory. One criteria you could apply is to look for stuff you don't use anymore. `ls` can help with that:
|
|
||||||
|
|
||||||
```
|
|
||||||
ls -lct
|
|
||||||
```
|
|
||||||
|
|
||||||
The instruction above shows the details of each file and directory (`-l`) and also shows when each item was last accessed (`-c`). It then orders the list from most recently accessed to least recently accessed (`-t`).
|
|
||||||
|
|
||||||
Is this hard to remember? You probably don’t use the `-c` and `-t` options every day, so perhaps. In any case, defining an alias like
|
|
||||||
|
|
||||||
```
|
|
||||||
alias lt='ls -lct'
|
|
||||||
```
|
|
||||||
|
|
||||||
will make it easier.
|
|
||||||
|
|
||||||
Then again, you may want to have the list show the oldest files first:
|
|
||||||
|
|
||||||
```
|
|
||||||
alias lo='lt -F | tac'
|
|
||||||
```
|
|
||||||
|
|
||||||
![aliases][3]
|
|
||||||
|
|
||||||
Figure 1: The lt and lo aliases in action.
|
|
||||||
|
|
||||||
[Used with permission][4]
|
|
||||||
|
|
||||||
There are a few interesting things going here. First, we are using an alias (`lt`) to create another alias -- which is perfectly okay. Second, we are passing a new parameter to `lt` (which, in turn gets passed to `ls` through the definition of the `lt` alias).
|
|
||||||
|
|
||||||
The `-F` option appends special symbols to the names of items to better differentiate regular files (that get no symbol) from executable files (that get an `*`), files from directories (end in `/`), and all of the above from links, symbolic and otherwise (that end in an `@` symbol). The `-F` option is throwback to the days when terminals where monochrome and there was no other way to easily see the difference between items. You use it here because, when you pipe the output from `lt` through to `tac` you lose the colors from `ls`.
|
|
||||||
|
|
||||||
The third thing to pay attention to is the use of piping. Piping happens when you pass the output from an instruction to another instruction. The second instruction can then use that output as its own input. In many shells (including Bash), you pipe something using the pipe symbol (`|`).
|
|
||||||
|
|
||||||
In this case, you are piping the output from `lt -F` into `tac`. `tac`'s name is a bit of a joke. You may have heard of `cat`, the instruction that was nominally created to con _cat_ enate files together, but that in practice is used to print out the contents of a file to the terminal. `tac` does the same, but prints out the contents it receives in reverse order. Get it? `cat` and `tac`. Developers, you so funny!
|
|
||||||
|
|
||||||
The thing is both `cat` and `tac` can also print out stuff piped over from another instruction, in this case, a list of files ordered chronologically.
|
|
||||||
|
|
||||||
So... after that digression, what comes out of the other end is the list of files and directories of the current directory in inverse order of freshness.
|
|
||||||
|
|
||||||
The final thing you have to bear in mind is that, while `lt` will work the current directory and any other directory...
|
|
||||||
|
|
||||||
```
|
|
||||||
# This will work:
|
|
||||||
lt
|
|
||||||
# And so will this:
|
|
||||||
lt /some/other/directory
|
|
||||||
```
|
|
||||||
|
|
||||||
... `lo` will only work with the current directory:
|
|
||||||
|
|
||||||
```
|
|
||||||
# This will work:
|
|
||||||
lo
|
|
||||||
# But this won't:
|
|
||||||
lo /some/other/directory
|
|
||||||
```
|
|
||||||
|
|
||||||
This is because Bash expands aliases into their components. When you type this:
|
|
||||||
|
|
||||||
```
|
|
||||||
lt /some/other/directory
|
|
||||||
```
|
|
||||||
|
|
||||||
Bash REALLY runs this:
|
|
||||||
|
|
||||||
```
|
|
||||||
ls -lct /some/other/directory
|
|
||||||
```
|
|
||||||
|
|
||||||
which is a valid Bash command.
|
|
||||||
|
|
||||||
However, if you type this:
|
|
||||||
|
|
||||||
```
|
|
||||||
lo /some/other/directory
|
|
||||||
```
|
|
||||||
|
|
||||||
Bash tries to run this:
|
|
||||||
|
|
||||||
```
|
|
||||||
ls -lct -F | tac /some/other/directory
|
|
||||||
```
|
|
||||||
|
|
||||||
which is not a valid instruction, because `tac` mainly because _/some/other/directory_ is a directory, and `cat` and `tac` don't do directories.
|
|
||||||
|
|
||||||
### More Alias Shortcuts
|
|
||||||
|
|
||||||
* `alias lll='ls -R'` prints out the contents of a directory and then drills down and prints out the contents of its subdirectories and the subdirectories of the subdirectories, and so on and so forth. It is a way of seeing everything you have under a directory.
|
|
||||||
|
|
||||||
* `mkdir='mkdir -pv'` let's you make directories within directories all in one go. With the base form of `mkdir`, to make a new directory containing a subdirectory you have to do this:
|
|
||||||
|
|
||||||
```
|
|
||||||
mkdir newdir
|
|
||||||
mkdir newdir/subdir
|
|
||||||
```
|
|
||||||
|
|
||||||
Or this:
|
|
||||||
|
|
||||||
```
|
|
||||||
mkdir -p newdir/subdir
|
|
||||||
```
|
|
||||||
|
|
||||||
while with the alias you would only have to do this:
|
|
||||||
|
|
||||||
```
|
|
||||||
mkdir newdir/subdir
|
|
||||||
```
|
|
||||||
|
|
||||||
Your new `mkdir` will also tell you what it is doing while is creating new directories.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Aliases as Safeguards
|
|
||||||
|
|
||||||
The other thing aliases are good for is as safeguards against erasing or overwriting your files accidentally. At this stage you have probably heard the legendary story about the new Linux user who ran:
|
|
||||||
|
|
||||||
```
|
|
||||||
rm -rf /
|
|
||||||
```
|
|
||||||
|
|
||||||
as root, and nuked the whole system. Then there's the user who decided that:
|
|
||||||
|
|
||||||
```
|
|
||||||
rm -rf /some/directory/ *
|
|
||||||
```
|
|
||||||
|
|
||||||
was a good idea and erased the complete contents of their home directory. Notice how easy it is to overlook that space separating the directory path and the `*`.
|
|
||||||
|
|
||||||
Both things can be avoided with the `alias rm='rm -i'` alias. The `-i` option makes `rm` ask the user whether that is what they really want to do and gives you a second chance before wreaking havoc in your file system.
|
|
||||||
|
|
||||||
The same goes for `cp`, which can overwrite a file without telling you anything. Create an alias like `alias cp='cp -i'` and stay safe!
|
|
||||||
|
|
||||||
### Next Time
|
|
||||||
|
|
||||||
We are moving more and more into scripting territory. Next time, we'll take the next logical step and see how combining instructions on the command line gives you really interesting and sophisticated solutions to everyday admin problems.
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.linux.com/blog/learn/2019/1/aliases-protect-and-serve
|
|
||||||
|
|
||||||
作者:[Paul Brown][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://www.linux.com/users/bro66
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://www.linux.com/blog/learn/2019/1/aliases-protect-and-serve
|
|
||||||
[2]: https://www.linux.com/files/images/fig01png-0
|
|
||||||
[3]: https://www.linux.com/sites/lcom/files/styles/rendered_file/public/fig01_0.png?itok=crqTm_va (aliases)
|
|
||||||
[4]: https://www.linux.com/licenses/category/used-permission
|
|
@ -1,135 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (geekpi)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: subject: (Be your own certificate authority)
|
|
||||||
[#]: via: (https://opensource.com/article/19/4/certificate-authority)
|
|
||||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez/users/elenajon123)
|
|
||||||
|
|
||||||
Be your own certificate authority
|
|
||||||
======
|
|
||||||
Create a simple, internal CA for your microservice architecture or
|
|
||||||
integration testing.
|
|
||||||
![][1]
|
|
||||||
|
|
||||||
The Transport Layer Security ([TLS][2]) model, which is sometimes referred to by the older name SSL, is based on the concept of [certificate authorities][3] (CAs). These authorities are trusted by browsers and operating systems and, in turn, _sign_ servers' certificates to validate their ownership.
|
|
||||||
|
|
||||||
However, for an intranet, a microservice architecture, or integration testing, it is sometimes useful to have a _local CA_ : one that is trusted only internally and, in turn, signs local servers' certificates.
|
|
||||||
|
|
||||||
This especially makes sense for integration tests. Getting certificates can be a burden because the servers will be up for minutes. But having an "ignore certificate" option in the code could allow it to be activated in production, leading to a security catastrophe.
|
|
||||||
|
|
||||||
A CA certificate is not much different from a regular server certificate; what matters is that it is trusted by local code. For example, in the **requests** library, this can be done by setting the **REQUESTS_CA_BUNDLE** variable to a directory containing this certificate.
|
|
||||||
|
|
||||||
In the example of creating a certificate for integration tests, there is no need for a _long-lived_ certificate: if your integration tests take more than a day, you have already failed.
|
|
||||||
|
|
||||||
So, calculate **yesterday** and **tomorrow** as the validity interval:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
>>> import datetime
|
|
||||||
>>> one_day = datetime.timedelta(days=1)
|
|
||||||
>>> today = datetime.date.today()
|
|
||||||
>>> yesterday = today - one_day
|
|
||||||
>>> tomorrow = today - one_day
|
|
||||||
```
|
|
||||||
|
|
||||||
Now you are ready to create a simple CA certificate. You need to generate a private key, create a public key, set up the "parameters" of the CA, and then self-sign the certificate: a CA certificate is _always_ self-signed. Finally, write out both the certificate file as well as the private key file.
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
||||||
from cryptography.hazmat.primitives import hashes, serialization
|
|
||||||
from cryptography import x509
|
|
||||||
from cryptography.x509.oid import NameOID
|
|
||||||
|
|
||||||
private_key = rsa.generate_private_key(
|
|
||||||
public_exponent=65537,
|
|
||||||
key_size=2048,
|
|
||||||
backend=default_backend()
|
|
||||||
)
|
|
||||||
public_key = private_key.public_key()
|
|
||||||
builder = x509.CertificateBuilder()
|
|
||||||
builder = builder.subject_name(x509.Name([
|
|
||||||
x509.NameAttribute(NameOID.COMMON_NAME, 'Simple Test CA'),
|
|
||||||
]))
|
|
||||||
builder = builder.issuer_name(x509.Name([
|
|
||||||
x509.NameAttribute(NameOID.COMMON_NAME, 'Simple Test CA'),
|
|
||||||
]))
|
|
||||||
builder = builder.not_valid_before(yesterday)
|
|
||||||
builder = builder.not_valid_after(tomorrow)
|
|
||||||
builder = builder.serial_number(x509.random_serial_number())
|
|
||||||
builder = builder.public_key(public_key)
|
|
||||||
builder = builder.add_extension(
|
|
||||||
x509.BasicConstraints(ca=True, path_length=None),
|
|
||||||
critical=True)
|
|
||||||
certificate = builder.sign(
|
|
||||||
private_key=private_key, algorithm=hashes.SHA256(),
|
|
||||||
backend=default_backend()
|
|
||||||
)
|
|
||||||
private_bytes = private_key.private_bytes(
|
|
||||||
encoding=serialization.Encoding.PEM,
|
|
||||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
|
||||||
encryption_algorithm=serialization.NoEncrption())
|
|
||||||
public_bytes = certificate.public_bytes(
|
|
||||||
encoding=serialization.Encoding.PEM)
|
|
||||||
with open("ca.pem", "wb") as fout:
|
|
||||||
fout.write(private_bytes + public_bytes)
|
|
||||||
with open("ca.crt", "wb") as fout:
|
|
||||||
fout.write(public_bytes)
|
|
||||||
```
|
|
||||||
|
|
||||||
In general, a real CA will expect a [certificate signing request][4] (CSR) to sign a certificate. However, when you are your own CA, you can make your own rules! Just go ahead and sign what you want.
|
|
||||||
|
|
||||||
Continuing with the integration test example, you can create the private keys and sign the corresponding public keys right then. Notice **COMMON_NAME** needs to be the "server name" in the **https** URL. If you've configured name lookup, the needed server will respond on **service.test.local**.
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
service_private_key = rsa.generate_private_key(
|
|
||||||
public_exponent=65537,
|
|
||||||
key_size=2048,
|
|
||||||
backend=default_backend()
|
|
||||||
)
|
|
||||||
service_public_key = service_private_key.public_key()
|
|
||||||
builder = x509.CertificateBuilder()
|
|
||||||
builder = builder.subject_name(x509.Name([
|
|
||||||
x509.NameAttribute(NameOID.COMMON_NAME, 'service.test.local')
|
|
||||||
]))
|
|
||||||
builder = builder.not_valid_before(yesterday)
|
|
||||||
builder = builder.not_valid_after(tomorrow)
|
|
||||||
builder = builder.public_key(public_key)
|
|
||||||
certificate = builder.sign(
|
|
||||||
private_key=private_key, algorithm=hashes.SHA256(),
|
|
||||||
backend=default_backend()
|
|
||||||
)
|
|
||||||
private_bytes = service_private_key.private_bytes(
|
|
||||||
encoding=serialization.Encoding.PEM,
|
|
||||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
|
||||||
encryption_algorithm=serialization.NoEncrption())
|
|
||||||
public_bytes = certificate.public_bytes(
|
|
||||||
encoding=serialization.Encoding.PEM)
|
|
||||||
with open("service.pem", "wb") as fout:
|
|
||||||
fout.write(private_bytes + public_bytes)
|
|
||||||
```
|
|
||||||
|
|
||||||
Now the **service.pem** file has a private key and a certificate that is "valid": it has been signed by your local CA. The file is in a format that can be given to, say, Nginx, HAProxy, or most other HTTPS servers.
|
|
||||||
|
|
||||||
By applying this logic to testing scripts, it's easy to create servers that look like authentic HTTPS servers, as long as the client is configured to trust the right CA.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://opensource.com/article/19/4/certificate-authority
|
|
||||||
|
|
||||||
作者:[Moshe Zadka (Community Moderator)][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://opensource.com/users/moshez/users/elenajon123
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rh_commun_4604_02_mech_connections_rhcz0.5x.png?itok=YPPU4dMj
|
|
||||||
[2]: https://en.wikipedia.org/wiki/Transport_Layer_Security
|
|
||||||
[3]: https://en.wikipedia.org/wiki/Certificate_authority
|
|
||||||
[4]: https://en.wikipedia.org/wiki/Certificate_signing_request
|
|
@ -1,166 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (cycoe)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: subject: (Monitoring CPU and GPU Temperatures on Linux)
|
|
||||||
[#]: via: (https://itsfoss.com/monitor-cpu-gpu-temp-linux/)
|
|
||||||
[#]: author: (It's FOSS Community https://itsfoss.com/author/itsfoss/)
|
|
||||||
|
|
||||||
Monitoring CPU and GPU Temperatures on Linux
|
|
||||||
======
|
|
||||||
|
|
||||||
_**Brief: This articles discusses two simple ways of monitoring CPU and GPU temperatures in Linux command line.**_
|
|
||||||
|
|
||||||
Because of **[Steam][1]** (including _[Steam Play][2]_ , aka _Proton_ ) and other developments, **GNU/Linux** is becoming the gaming platform of choice for more and more computer users everyday. A good number of users are also going for **GNU/Linux** when it comes to other resource-consuming computing tasks such as [video editing][3] or graphic design ( _Kdenlive_ and _[Blender][4]_ are good examples of programs for these).
|
|
||||||
|
|
||||||
Whether you are one of those users or otherwise, you are bound to have wondered how hot your computer’s CPU and GPU can get (even more so if you do overclocking). If that is the case, keep reading. We will be looking at a couple of very simple commands to monitor CPU and GPU temps.
|
|
||||||
|
|
||||||
My setup includes a [Slimbook Kymera][5] and two displays (a TV set and a PC monitor) which allows me to use one for playing games and the other to keep an eye on the temperatures. Also, since I use [Zorin OS][6] I will be focusing on **Ubuntu** and **Ubuntu** derivatives.
|
|
||||||
|
|
||||||
To monitor the behaviour of both CPU and GPU we will be making use of the useful `watch` command to have dynamic readings every certain number of seconds.
|
|
||||||
|
|
||||||
![][7]
|
|
||||||
|
|
||||||
### Monitoring CPU Temperature in Linux
|
|
||||||
|
|
||||||
For CPU temps, we will combine `watch` with the `sensors` command. An interesting article about a [gui version of this tool has already been covered on It’s FOSS][8]. However, we will use the terminal version here:
|
|
||||||
|
|
||||||
```
|
|
||||||
watch -n 2 sensors
|
|
||||||
```
|
|
||||||
|
|
||||||
`watch` guarantees that the readings will be updated every 2 seconds (and this value can — of course — be changed to what best fit your needs):
|
|
||||||
|
|
||||||
```
|
|
||||||
Every 2,0s: sensors
|
|
||||||
|
|
||||||
iwlwifi-virtual-0
|
|
||||||
Adapter: Virtual device
|
|
||||||
temp1: +39.0°C
|
|
||||||
|
|
||||||
acpitz-virtual-0
|
|
||||||
Adapter: Virtual device
|
|
||||||
temp1: +27.8°C (crit = +119.0°C)
|
|
||||||
temp2: +29.8°C (crit = +119.0°C)
|
|
||||||
|
|
||||||
coretemp-isa-0000
|
|
||||||
Adapter: ISA adapter
|
|
||||||
Package id 0: +37.0°C (high = +82.0°C, crit = +100.0°C)
|
|
||||||
Core 0: +35.0°C (high = +82.0°C, crit = +100.0°C)
|
|
||||||
Core 1: +35.0°C (high = +82.0°C, crit = +100.0°C)
|
|
||||||
Core 2: +33.0°C (high = +82.0°C, crit = +100.0°C)
|
|
||||||
Core 3: +36.0°C (high = +82.0°C, crit = +100.0°C)
|
|
||||||
Core 4: +37.0°C (high = +82.0°C, crit = +100.0°C)
|
|
||||||
Core 5: +35.0°C (high = +82.0°C, crit = +100.0°C)
|
|
||||||
```
|
|
||||||
|
|
||||||
Amongst other things, we get the following information:
|
|
||||||
|
|
||||||
* We have 5 cores in use at the moment (with the current highest temperature being 37.0ºC).
|
|
||||||
* Values higher than 82.0ºC are considered high.
|
|
||||||
* A value over 100.0ºC is deemed critical.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[][9]
|
|
||||||
|
|
||||||
Suggested read Top 10 Command Line Games For Linux
|
|
||||||
|
|
||||||
The values above lead us to the conclusion that the computer’s workload is very light at the moment.
|
|
||||||
|
|
||||||
### Monitoring GPU Temperature in Linux
|
|
||||||
|
|
||||||
Let us turn to the graphics card now. I have never used an **AMD** dedicated graphics card, so I will be focusing on **Nvidia** ones. The first thing to do is download the appropriate, current driver through [additional drivers in Ubuntu][10].
|
|
||||||
|
|
||||||
On **Ubuntu** (and its forks such as **Zorin** or **Linux Mint** ), going to _Software & Updates_ > _Additional Drivers_ and selecting the most recent one normally suffices. Additionally, you can add/enable the official _ppa_ for graphics cards (either through the command line or via _Software & Updates_ > _Other Software_ ). After installing the driver you will have at your disposal the _Nvidia X Server_ gui application along with the command line utility _nvidia-smi_ (Nvidia System Management Interface). So we will use `watch` and `nvidia-smi`:
|
|
||||||
|
|
||||||
```
|
|
||||||
watch -n 2 nvidia-smi
|
|
||||||
```
|
|
||||||
|
|
||||||
And — the same as for the CPU — we will get updated readings every two seconds:
|
|
||||||
|
|
||||||
```
|
|
||||||
Every 2,0s: nvidia-smi
|
|
||||||
|
|
||||||
Fri Apr 19 20:45:30 2019
|
|
||||||
+-----------------------------------------------------------------------------+
|
|
||||||
| Nvidia-SMI 418.56 Driver Version: 418.56 CUDA Version: 10.1 |
|
|
||||||
|-------------------------------+----------------------+----------------------+
|
|
||||||
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
|
|
||||||
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|
|
||||||
|===============================+======================+======================|
|
|
||||||
| 0 GeForce GTX 106... Off | 00000000:01:00.0 On | N/A |
|
|
||||||
| 0% 54C P8 10W / 120W | 433MiB / 6077MiB | 4% Default |
|
|
||||||
+-------------------------------+----------------------+----------------------+
|
|
||||||
|
|
||||||
+-----------------------------------------------------------------------------+
|
|
||||||
| Processes: GPU Memory |
|
|
||||||
| GPU PID Type Process name Usage |
|
|
||||||
|=============================================================================|
|
|
||||||
| 0 1557 G /usr/lib/xorg/Xorg 190MiB |
|
|
||||||
| 0 1820 G /usr/bin/gnome-shell 174MiB |
|
|
||||||
| 0 7820 G ...equest-channel-token=303407235874180773 65MiB |
|
|
||||||
+-----------------------------------------------------------------------------+
|
|
||||||
```
|
|
||||||
|
|
||||||
The chart gives the following information about the graphics card:
|
|
||||||
|
|
||||||
* it is using the open source driver version 418.56.
|
|
||||||
* the current temperature of the card is 54.0ºC — with the fan at 0% of its capacity.
|
|
||||||
* the power consumption is very low: only 10W.
|
|
||||||
* out of 6 GB of vram (video random access memory), it is only using 433 MB.
|
|
||||||
* the used vram is being taken by three processes whose IDs are — respectively — 1557, 1820 and 7820.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[][11]
|
|
||||||
|
|
||||||
Suggested read Googler: Now You Can Google From Linux Terminal!
|
|
||||||
|
|
||||||
Most of these facts/values show that — clearly — we are not playing any resource-consuming games or dealing with heavy workloads. Should we started playing a game, processing a video — or the like —, the values would start to go up.
|
|
||||||
|
|
||||||
#### Conclusion
|
|
||||||
|
|
||||||
Althoug there are gui tools, I find these two commands very handy to check on your hardware in real time.
|
|
||||||
|
|
||||||
What do you make of them? You can learn more about the utilities involved by reading their man pages.
|
|
||||||
|
|
||||||
Do you have other preferences? Share them with us in the comments, ;).
|
|
||||||
|
|
||||||
Halof!!! (Have a lot of fun!!!).
|
|
||||||
|
|
||||||
![avatar][12]
|
|
||||||
|
|
||||||
### Alejandro Egea-Abellán
|
|
||||||
|
|
||||||
It’s FOSS Community Contributor
|
|
||||||
|
|
||||||
I developed a liking for electronics, linguistics, herpetology and computers (particularly GNU/Linux and FOSS). I am LPIC-2 certified and currently work as a technical consultant and Moodle administrator in the Department for Lifelong Learning at the Ministry of Education in Murcia, Spain. I am a firm believer in lifelong learning, the sharing of knowledge and computer-user freedom.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://itsfoss.com/monitor-cpu-gpu-temp-linux/
|
|
||||||
|
|
||||||
作者:[It's FOSS Community][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://itsfoss.com/author/itsfoss/
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://itsfoss.com/install-steam-ubuntu-linux/
|
|
||||||
[2]: https://itsfoss.com/steam-play-proton/
|
|
||||||
[3]: https://itsfoss.com/best-video-editing-software-linux/
|
|
||||||
[4]: https://www.blender.org/
|
|
||||||
[5]: https://slimbook.es/
|
|
||||||
[6]: https://zorinos.com/
|
|
||||||
[7]: https://itsfoss.com/wp-content/uploads/2019/04/monitor-cpu-gpu-temperature-linux-800x450.png
|
|
||||||
[8]: https://itsfoss.com/check-laptop-cpu-temperature-ubuntu/
|
|
||||||
[9]: https://itsfoss.com/best-command-line-games-linux/
|
|
||||||
[10]: https://itsfoss.com/install-additional-drivers-ubuntu/
|
|
||||||
[11]: https://itsfoss.com/review-googler-linux/
|
|
||||||
[12]: https://itsfoss.com/wp-content/uploads/2019/04/EGEA-ABELLAN-Alejandro.jpg
|
|
@ -1,5 +1,5 @@
|
|||||||
[#]: collector: (lujun9972)
|
[#]: collector: (lujun9972)
|
||||||
[#]: translator: ( )
|
[#]: translator: (arrowfeng)
|
||||||
[#]: reviewer: ( )
|
[#]: reviewer: ( )
|
||||||
[#]: publisher: ( )
|
[#]: publisher: ( )
|
||||||
[#]: url: ( )
|
[#]: url: ( )
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[#]: collector: (lujun9972)
|
[#]: collector: (lujun9972)
|
||||||
[#]: translator: ( )
|
[#]: translator: (geekpi)
|
||||||
[#]: reviewer: ( )
|
[#]: reviewer: ( )
|
||||||
[#]: publisher: ( )
|
[#]: publisher: ( )
|
||||||
[#]: url: ( )
|
[#]: url: ( )
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[#]: collector: (lujun9972)
|
[#]: collector: (lujun9972)
|
||||||
[#]: translator: ( )
|
[#]: translator: (arrowfeng)
|
||||||
[#]: reviewer: ( )
|
[#]: reviewer: ( )
|
||||||
[#]: publisher: ( )
|
[#]: publisher: ( )
|
||||||
[#]: url: ( )
|
[#]: url: ( )
|
||||||
|
133
translated/tech/20190411 Be your own certificate authority.md
Normal file
133
translated/tech/20190411 Be your own certificate authority.md
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (geekpi)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
[#]: subject: (Be your own certificate authority)
|
||||||
|
[#]: via: (https://opensource.com/article/19/4/certificate-authority)
|
||||||
|
[#]: author: (Moshe Zadka https://opensource.com/users/moshez/users/elenajon123)
|
||||||
|
|
||||||
|
|
||||||
|
自己成为证书颁发机构
|
||||||
|
======
|
||||||
|
为你的微服务架构或者集成测试创建一个简单的内部 CA
|
||||||
|
![][1]
|
||||||
|
|
||||||
|
传输层安全([TLS][2])模型,有时也称它的旧名称 SSL,是基于[证书颁发机构][3] (CA) 的概念。这些机构受到浏览器和操作系统的信任,然后_签名_服务器的的证书则用于验证其所有权。
|
||||||
|
|
||||||
|
但是,对于内部网络,微服务架构或集成测试,有时候_本地 CA_ 更有用:一个只在内部受信任的CA,然后签名本地服务器的证书。
|
||||||
|
|
||||||
|
这对集成测试特别有意义。获取证书可能会带来负担,因为这会占用服务器几分钟。但是在代码中使用“忽略证书”可能会引入生产环境,从而导致安全灾难。
|
||||||
|
|
||||||
|
CA 证书与常规服务器证书没有太大区别。重要的是它被本地代码信任。例如,在 **requests** 库中,可以通过将 **REQUESTS_CA_BUNDLE** 变量设置为包含此证书的目录来完成。
|
||||||
|
|
||||||
|
在为集成测试创建证书的例子中,不需要_长期的_证书:如果你的集成测试需要超过一天,那么你会失败。
|
||||||
|
|
||||||
|
因此,计算**昨天**和**明天**作为有效期间隔:
|
||||||
|
|
||||||
|
```
|
||||||
|
>>> import datetime
|
||||||
|
>>> one_day = datetime.timedelta(days=1)
|
||||||
|
>>> today = datetime.date.today()
|
||||||
|
>>> yesterday = today - one_day
|
||||||
|
>>> tomorrow = today - one_day
|
||||||
|
```
|
||||||
|
|
||||||
|
现在你已准备好创建一个简单的 CA 证书。你需要生成私钥,创建公钥,设置 CA 的“参数”,然后自签名证书:CA 证书_总是_自签名的。最后,导出证书文件以及私钥文件。
|
||||||
|
|
||||||
|
```
|
||||||
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||||
|
from cryptography.hazmat.primitives import hashes, serialization
|
||||||
|
from cryptography import x509
|
||||||
|
from cryptography.x509.oid import NameOID
|
||||||
|
|
||||||
|
|
||||||
|
private_key = rsa.generate_private_key(
|
||||||
|
public_exponent=65537,
|
||||||
|
key_size=2048,
|
||||||
|
backend=default_backend()
|
||||||
|
)
|
||||||
|
public_key = private_key.public_key()
|
||||||
|
builder = x509.CertificateBuilder()
|
||||||
|
builder = builder.subject_name(x509.Name([
|
||||||
|
x509.NameAttribute(NameOID.COMMON_NAME, 'Simple Test CA'),
|
||||||
|
]))
|
||||||
|
builder = builder.issuer_name(x509.Name([
|
||||||
|
x509.NameAttribute(NameOID.COMMON_NAME, 'Simple Test CA'),
|
||||||
|
]))
|
||||||
|
builder = builder.not_valid_before(yesterday)
|
||||||
|
builder = builder.not_valid_after(tomorrow)
|
||||||
|
builder = builder.serial_number(x509.random_serial_number())
|
||||||
|
builder = builder.public_key(public_key)
|
||||||
|
builder = builder.add_extension(
|
||||||
|
x509.BasicConstraints(ca=True, path_length=None),
|
||||||
|
critical=True)
|
||||||
|
certificate = builder.sign(
|
||||||
|
private_key=private_key, algorithm=hashes.SHA256(),
|
||||||
|
backend=default_backend()
|
||||||
|
)
|
||||||
|
private_bytes = private_key.private_bytes(
|
||||||
|
encoding=serialization.Encoding.PEM,
|
||||||
|
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||||
|
encryption_algorithm=serialization.NoEncrption())
|
||||||
|
public_bytes = certificate.public_bytes(
|
||||||
|
encoding=serialization.Encoding.PEM)
|
||||||
|
with open("ca.pem", "wb") as fout:
|
||||||
|
fout.write(private_bytes + public_bytes)
|
||||||
|
with open("ca.crt", "wb") as fout:
|
||||||
|
fout.write(public_bytes)
|
||||||
|
```
|
||||||
|
|
||||||
|
通常,真正的 CA 会有[证书签名请求][4] (CSR) 来签名证书。但是,当你是自己的 CA 时,你可以制定自己的规则!请继续并签名你想要的内容。
|
||||||
|
|
||||||
|
继续集成测试的例子,你可以创建私钥并立即签名相应的公钥。注意 **COMMON_NAME** 需要是 **https** URL 中的“服务器名称”。如果你已配置名称查询,则相应的服务器会响应 **service.test.local**。
|
||||||
|
|
||||||
|
```
|
||||||
|
service_private_key = rsa.generate_private_key(
|
||||||
|
public_exponent=65537,
|
||||||
|
key_size=2048,
|
||||||
|
backend=default_backend()
|
||||||
|
)
|
||||||
|
service_public_key = service_private_key.public_key()
|
||||||
|
builder = x509.CertificateBuilder()
|
||||||
|
builder = builder.subject_name(x509.Name([
|
||||||
|
x509.NameAttribute(NameOID.COMMON_NAME, 'service.test.local')
|
||||||
|
]))
|
||||||
|
builder = builder.not_valid_before(yesterday)
|
||||||
|
builder = builder.not_valid_after(tomorrow)
|
||||||
|
builder = builder.public_key(public_key)
|
||||||
|
certificate = builder.sign(
|
||||||
|
private_key=private_key, algorithm=hashes.SHA256(),
|
||||||
|
backend=default_backend()
|
||||||
|
)
|
||||||
|
private_bytes = service_private_key.private_bytes(
|
||||||
|
encoding=serialization.Encoding.PEM,
|
||||||
|
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||||
|
encryption_algorithm=serialization.NoEncrption())
|
||||||
|
public_bytes = certificate.public_bytes(
|
||||||
|
encoding=serialization.Encoding.PEM)
|
||||||
|
with open("service.pem", "wb") as fout:
|
||||||
|
fout.write(private_bytes + public_bytes)
|
||||||
|
```
|
||||||
|
|
||||||
|
现在 **service.pem** 文件有一个私钥和一个“有效”的证书:它已由本地的 CA 签名。该文件的格式可以给 Nginx、HAProxy 或大多数其他 HTTPS 服务器使用。
|
||||||
|
|
||||||
|
通过将此逻辑用在测试脚本中,只要客户端配置信任该 CA,那么就可以轻松创建看起来真实的 HTTPS 服务器。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://opensource.com/article/19/4/certificate-authority
|
||||||
|
|
||||||
|
作者:[Moshe Zadka (Community Moderator)][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[geekpi](https://github.com/geekpi)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://opensource.com/users/moshez/users/elenajon123
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rh_commun_4604_02_mech_connections_rhcz0.5x.png?itok=YPPU4dMj
|
||||||
|
[2]: https://en.wikipedia.org/wiki/Transport_Layer_Security
|
||||||
|
[3]: https://en.wikipedia.org/wiki/Certificate_authority
|
||||||
|
[4]: https://en.wikipedia.org/wiki/Certificate_signing_request
|
@ -0,0 +1,164 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (cycoe)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
[#]: subject: (Monitoring CPU and GPU Temperatures on Linux)
|
||||||
|
[#]: via: (https://itsfoss.com/monitor-cpu-gpu-temp-linux/)
|
||||||
|
[#]: author: (It's FOSS Community https://itsfoss.com/author/itsfoss/)
|
||||||
|
|
||||||
|
在 Linux 上监控 CPU 和 GPU 温度
|
||||||
|
======
|
||||||
|
|
||||||
|
_**摘要:本篇文章讨论了在 Linux 命令行中监控 CPU 和 GPU 温度的两种简单方式。**_
|
||||||
|
|
||||||
|
由于 **[Steam][1]**(包括 _[Steam Play][2]_,也就是我们所熟知的 _Proton_)和一些其他的发展,**GNU/Linux** 正在成为越来越多计算机用户的日常游戏平台的选择。也有相当一部分用户在遇到像[视频编辑][3]或图形设计等(_Kdenlive_ 和 _[Blender][4]_ 是这类应用程序中很好的例子)资源消耗型计算任务时,也会使用 **GNU/Linux**。
|
||||||
|
|
||||||
|
不管你是否是这些用户中的一员或其他用户,你也一定想知道你的电脑 CPU 和 GPU 能有多热(如果你想要超频的话更会如此)。如果情况是这样,那么继续读下去。我们会介绍两个非常简单的命令来监控 CPU 和 GPU 温度。
|
||||||
|
|
||||||
|
我的装置包括一台 [Slimbook Kymera][5] 和两台显示器(一台 TV 和一台 PC 监视器),使得我可以用一台来玩游戏,另一台来留意监控温度。另外,因为我使用 [Zorin OS][6],我会将关注点放在 **Ubuntu** 和 **Ubuntu** 的衍生发行版上。
|
||||||
|
|
||||||
|
为了监控 CPU 和 GPU 的行为,我们将利用实用的 `watch` 命令在每几秒钟之后动态地得到示数。
|
||||||
|
|
||||||
|
![][7]
|
||||||
|
|
||||||
|
### 在 Linux 中监控 CPU 温度
|
||||||
|
|
||||||
|
对于 CPU 温度,我们将结合使用 `watch` 与 `sensors` 命令。一篇关于[此工具的图形用户界面版本][8]的有趣文章已经在 It's FOSS 中介绍过了。然而,我们将在此处使用命令行版本:
|
||||||
|
|
||||||
|
```
|
||||||
|
watch -n 2 sensors
|
||||||
|
```
|
||||||
|
|
||||||
|
`watch` 保证了示数会在每 2 秒钟更新一次(-当然- 这个周期值能够根据你的需要去更改):
|
||||||
|
|
||||||
|
```
|
||||||
|
Every 2,0s: sensors
|
||||||
|
|
||||||
|
iwlwifi-virtual-0
|
||||||
|
Adapter: Virtual device
|
||||||
|
temp1: +39.0°C
|
||||||
|
|
||||||
|
acpitz-virtual-0
|
||||||
|
Adapter: Virtual device
|
||||||
|
temp1: +27.8°C (crit = +119.0°C)
|
||||||
|
temp2: +29.8°C (crit = +119.0°C)
|
||||||
|
|
||||||
|
coretemp-isa-0000
|
||||||
|
Adapter: ISA adapter
|
||||||
|
Package id 0: +37.0°C (high = +82.0°C, crit = +100.0°C)
|
||||||
|
Core 0: +35.0°C (high = +82.0°C, crit = +100.0°C)
|
||||||
|
Core 1: +35.0°C (high = +82.0°C, crit = +100.0°C)
|
||||||
|
Core 2: +33.0°C (high = +82.0°C, crit = +100.0°C)
|
||||||
|
Core 3: +36.0°C (high = +82.0°C, crit = +100.0°C)
|
||||||
|
Core 4: +37.0°C (high = +82.0°C, crit = +100.0°C)
|
||||||
|
Core 5: +35.0°C (high = +82.0°C, crit = +100.0°C)
|
||||||
|
```
|
||||||
|
|
||||||
|
除此之外,我们还能得到如下信息:
|
||||||
|
|
||||||
|
* 我们有 5 个核心正在被使用(并且当前的最高温度为 37.0ºC)。
|
||||||
|
* 温度超过 82.0ºC 会被认为是过热。
|
||||||
|
* 超过 100.0ºC 的温度会被认为是超过临界值。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[推荐阅读:Linux 上排行前 10 的命令行游戏][9]
|
||||||
|
|
||||||
|
|
||||||
|
根据以上的温度值我们可以得出结论,我的电脑目前的工作负载非常小。
|
||||||
|
|
||||||
|
### 在 Linux 中监控 GPU 温度
|
||||||
|
|
||||||
|
现在让我们来看看显示卡。我从来没使用过 **AMD** 的显示卡,因此我会将重点放在 **Nvidia** 的显示卡上。我们需要做的第一件事是从 [Ubuntu 的附加驱动][10] 中下载合适的最新驱动。
|
||||||
|
|
||||||
|
在 **Ubuntu**(**Zorin** 或 **Linux Mint** 也是相同的)中,进入_软件和更新_ > _附加驱动_选项,选择最新的可用驱动。另外,你可以添加或启用显示卡的官方 _ppa_(通过命令行或通过_软件和更新_ > _其他软件_来实现)。安装驱动程序后,你将可以使用 _Nvidia X Server_ 的 GUI 程序以及命令行工具 _nvidia-smi_(Nvidia 系统管理界面)。因此我们将使用 `watch` 和 `nvidia-smi`:
|
||||||
|
|
||||||
|
```
|
||||||
|
watch -n 2 nvidia-smi
|
||||||
|
```
|
||||||
|
|
||||||
|
与 CPU 的情况一样,我们会在每两秒得到一次更新的示数:
|
||||||
|
|
||||||
|
```
|
||||||
|
Every 2,0s: nvidia-smi
|
||||||
|
|
||||||
|
Fri Apr 19 20:45:30 2019
|
||||||
|
+-----------------------------------------------------------------------------+
|
||||||
|
| Nvidia-SMI 418.56 Driver Version: 418.56 CUDA Version: 10.1 |
|
||||||
|
|-------------------------------+----------------------+----------------------+
|
||||||
|
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
|
||||||
|
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|
||||||
|
|===============================+======================+======================|
|
||||||
|
| 0 GeForce GTX 106... Off | 00000000:01:00.0 On | N/A |
|
||||||
|
| 0% 54C P8 10W / 120W | 433MiB / 6077MiB | 4% Default |
|
||||||
|
+-------------------------------+----------------------+----------------------+
|
||||||
|
|
||||||
|
+-----------------------------------------------------------------------------+
|
||||||
|
| Processes: GPU Memory |
|
||||||
|
| GPU PID Type Process name Usage |
|
||||||
|
|=============================================================================|
|
||||||
|
| 0 1557 G /usr/lib/xorg/Xorg 190MiB |
|
||||||
|
| 0 1820 G /usr/bin/gnome-shell 174MiB |
|
||||||
|
| 0 7820 G ...equest-channel-token=303407235874180773 65MiB |
|
||||||
|
+-----------------------------------------------------------------------------+
|
||||||
|
```
|
||||||
|
|
||||||
|
从这个表格中我们得到了关于显示卡的如下信息:
|
||||||
|
|
||||||
|
* 它正在使用版本号为 418.56 的开源驱动。
|
||||||
|
* 显示卡的当前温度为 54.0ºC,并且风扇的使用量为 0%。
|
||||||
|
* 电量的消耗非常低:仅仅 10W。
|
||||||
|
* 总量为 6GB 的 vram(视频随机存取存储器),只使用了 433MB。
|
||||||
|
* vram 正在被 3 个进程使用,他们的 ID 分别为 1557、1820 和 7820。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[推荐阅读:现在你可以在 Linux 终端中使用谷歌了!][11]
|
||||||
|
|
||||||
|
|
||||||
|
大部分这些事实或数值都清晰地表明,我们没有在玩任何消耗系统资源的游戏或处理大负载的任务。当我们开始玩游戏、处理视频或其他类似任务时,这些值就会开始上升。
|
||||||
|
|
||||||
|
#### 结论
|
||||||
|
|
||||||
|
即便我们有 GUI 工具,但我还是发现这两个命令对于实时监控硬件非常的顺手。
|
||||||
|
|
||||||
|
你将如何去使用它们呢?你可以通过阅读他们的 man 手册来学习更多关于这些工具的使用技巧。
|
||||||
|
|
||||||
|
你有其他偏爱的工具吗?在评论里分享给我们吧 ;)。
|
||||||
|
|
||||||
|
玩得开心!
|
||||||
|
|
||||||
|
![化身][12]
|
||||||
|
|
||||||
|
### Alejandro Egea-Abellán
|
||||||
|
|
||||||
|
It's FOSS 社区贡献者
|
||||||
|
|
||||||
|
我对电子、语言学、爬虫学、计算机(尤其是 GNU/Linux 和 FOSS)有着浓厚兴趣。我通过了 LPIC-2 认证,目前在西班牙穆尔西亚教育部终身学习部们担任技术顾问和 Moodle(译注:Moodle 是一个开源课程管理系统)管理员。我是终身学习、知识共享和计算机用户自由的坚定信奉者。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://itsfoss.com/monitor-cpu-gpu-temp-linux/
|
||||||
|
|
||||||
|
作者:[It's FOSS Community][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[cycoe](https://github.com/cycoe)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://itsfoss.com/author/itsfoss/
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://itsfoss.com/install-steam-ubuntu-linux/
|
||||||
|
[2]: https://itsfoss.com/steam-play-proton/
|
||||||
|
[3]: https://itsfoss.com/best-video-editing-software-linux/
|
||||||
|
[4]: https://www.blender.org/
|
||||||
|
[5]: https://slimbook.es/
|
||||||
|
[6]: https://zorinos.com/
|
||||||
|
[7]: https://itsfoss.com/wp-content/uploads/2019/04/monitor-cpu-gpu-temperature-linux-800x450.png
|
||||||
|
[8]: https://itsfoss.com/check-laptop-cpu-temperature-ubuntu/
|
||||||
|
[9]: https://itsfoss.com/best-command-line-games-linux/
|
||||||
|
[10]: https://itsfoss.com/install-additional-drivers-ubuntu/
|
||||||
|
[11]: https://itsfoss.com/review-googler-linux/
|
||||||
|
[12]: https://itsfoss.com/wp-content/uploads/2019/04/EGEA-ABELLAN-Alejandro.jpg
|
Loading…
Reference in New Issue
Block a user