TranslateProject/sources/tech/20220119 Manage your passwords in the Linux terminal.md

259 lines
9.8 KiB
Markdown
Raw Normal View History

[#]: subject: "Manage your passwords in the Linux terminal"
[#]: via: "https://opensource.com/article/22/1/manage-passwords-linux-terminal"
[#]: author: "Seth Kenlon https://opensource.com/users/seth"
[#]: collector: "lujun9972"
2022-04-11 21:34:32 +08:00
[#]: translator: "hwlife"
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
2022-04-11 23:11:43 +08:00
在 Linux 终端里管理你的密码
======
2022-04-11 23:11:43 +08:00
Pass 是一个经典的类 UNIX 密码管理系统,使用 GnuPG (GPG) 作为加密方式,并且终端作为它的主要界面。
![Linux keys on the keyboard for a desktop computer][1]
2022-04-11 23:11:43 +08:00
这些日子里,我们有了几十个密码。幸运的是,这些密码大部分几乎都是用于网站的,而且你通过互联网浏览器访问了许多网站,许多浏览器都有内置的密码管理器。最普遍的互联网浏览器也有一个同步的功能帮助你所有设备上运行的浏览器之间分发共享密码,所以当你需要它的时候,绝不会找不到你的登录信息。如过这不能够满足你,还有类似 [BitWarden][2] 这样优秀的开源项目可以托管你的加密的密码,确保只有你自己才能解锁它们。这些方案帮助你轻松的维护唯一的密码,同时用这些方便的系统来选择密码。但是我的主密码存储库比以上这些方式简单的多。我主要是使用 [pass][3] ,一个经典的类 UNIX 密码管理系统,使用 GnuPG (GPG) 作为加密方式,并且终端作为它的主要界面。
2022-04-11 23:11:43 +08:00
### 安装 pass
2022-04-11 23:11:43 +08:00
你可以从你的发行版仓库中安装 `pass` 命令。
2022-04-11 23:11:43 +08:00
在 FedoraMageia和类似的发行版上你可以用你的包管理器来安装它
```
`$ sudo dnf install pass`
```
2022-04-11 23:11:43 +08:00
在 Elementary Mint 和其它基于 Debian 的发行版上:
```
`$ sudo apt install pass`
```
2022-04-11 23:11:43 +08:00
在 macOS ,你可以使用 [Homebrew][4] 来安装它:
```
`$ brew install pass`
```
2022-04-11 23:11:43 +08:00
### 设置 GnuPG
2022-04-11 23:11:43 +08:00
在使用 `pass` 之前,你需要一个有效的 PGP ("Pretty Good Privacy") 密钥。如果你已经维护了一个 PGP 密钥,你可以跳过这个步骤,或者你可以选择为使用 `pass` 而创建一个新的密钥。最喜闻乐见的开源 PGP 协议是 Linux 附带的 GnuPG (GPG) ,你可以在 macOS 上从 [gpgtools.org][5] Homebrew 或者 [Macports][6] 来安装它。要创建 GnuPG 密码,运行这个命令:
```
`$ gpg --generate-key`
```
2022-04-11 23:11:43 +08:00
提示你输入你的名字和电子邮件并且为密钥创建密码。你的密钥是一个数字文件,你的密码只有你自己知道。它俩组合起来能够加密和解锁加密的信息,比如包含密码的文件。
2022-04-11 23:11:43 +08:00
GPG 密钥更像是一个房门钥匙或者车钥匙,如果你是去它,它加密的任何东西将不能在获得。光知道你的密码是不够的。
2022-04-11 23:11:43 +08:00
如果你已经管理了几个 SSH 密钥,你可能就知道怎么回事了。如果你对数字加密密钥是个新手,可能得花些时间来使用。备份你的 `~/.gnupg` 目录,你不能随意删除它,除非下次你一时兴起决定尝试一个新的发行版。
2022-04-11 23:11:43 +08:00
做一个备份保持备份安全。
2022-04-11 23:11:43 +08:00
### 设置 pass
2022-04-11 23:11:43 +08:00
要开始使用 `pass` 你必须初始化一个 _密码仓库_ 定义为一个已设置的使用指定加密密钥的储存位置。您可以通过与密钥相关联的名称或数字指纹来指示要用于密码存储的GPG密钥。你自己的名字通常更容易选择
```
$ pass init seth
mkdir: created directory '/home/seth/.password-store/'
Password store initialized for seth
```
2022-04-11 23:11:43 +08:00
如果你忘记了你的名称,你可以使用 `gpg` 命令查看数字指纹和关联你名称的密钥:
```
$ gpg --list-keys
gpg --list-keys
/home/seth/.gnupg/pubring.kbx
\-----------------------------
pub  ed25519 2022-01-06 [SC] [expires: 2024-01-06]
     2BFF94286461216C907CBA52F067996F13EF10D8
uid  [ultimate] Seth Kenlon <[seth@example.com][7]>
sub  cv25519 2022-01-06 [E] [expires: 2024-01-06]
```
2022-04-11 23:11:43 +08:00
用数字指纹初始化一个和你的名称基本相同的密码仓库:
```
`$ pass init 2BFF94286461216C907CBA52F067996F13EF10D8`
```
2022-04-11 23:11:43 +08:00
### 存储密码
2022-04-11 23:11:43 +08:00
使用 `pass add` 命令添加密码到你的密码仓库:
```
$ pass add [www.example.com][8]
Enter password for [www.example.com][8]:
```
2022-04-11 23:11:43 +08:00
提示你键入你要添加的密码。
2022-04-11 23:11:43 +08:00
密码现在存储到了你的密码仓库中。你可以自己查看一下:
```
$ ls /root/.password-store/
[www.example.com.gpg][9]
```
2022-04-11 23:11:43 +08:00
当然,这个文件是不可读的,并且你尝试对它运行 `cat``less` 时,在你的终端上会显示乱码(如果显示太过杂乱,可以使用 `reset` 命令来恢复你的终端。)
2022-04-11 23:11:43 +08:00
### 用 pass 编辑密码
I use different user names for different activities online, so the username for a site is often just as important as the password. The `pass` system allows for this, even though it doesn't prompt you for it by default. You can add a user name to a password file using the `pass edit` command:
```
`$ pass edit www.example.com`
```
This opens a text editor (specifically the editor you have set as your `EDITOR` or `VISUAL` [environment variable][10]) displaying the contents of the `www.example.com` file. Currently, that's just a password, but you can add a user name and even another URL or any information you want. It's an encrypted file, so you're free to keep what you want in it.
```
bd%dc$3a49af49498bb6f31bc964718C
user: seth123
url: example.com
```
Save the file and close it.
### Get a password from pass
To see the contents of a password file, use the `pass show` command:
```
$ pass show [www.example.com][8]
bd%dc$3a49af49498bb6f31bc964718C
user: seth123
url: [www.example.org][11]
```
### Search for a password
Sometimes it's tough to remember whether a password is filed under `www.example.com` or just `example.com` or even something like `app.example.com`. Furthermore, some website infrastructures use different URLs for different site functions, so you might file a password away under `www.example.com` even though you also use the same login information for the partner site `www.example.org`.
When in doubt, use `grep`. The `pass grep` command shows all instances of a search term, either in a file name or in the contents of a file:
```
$ pass grep example
[www.example.com][8]:
url: [www.example.org][11]
```
### Using pass with a browser
I use `pass` for information beyond just internet passwords, but websites are where I most often need passwords. I usually have a terminal open somewhere on my computer, so it's not much trouble to **Alt+Tab** to a terminal and get the information I need with `pass`. But that's not what I do because there are plugins to integrate `pass` with web browsers.
#### Pass host script
First, install the `pass` host script:
```
`$ curl -sSL github.com/passff/passff-host/release/latest/download/install_host_app.sh`
```
This install script places a Python script that helps your browser access your password store and GPG keys. Run it along with the name of the browser you use (or nothing, to see all options):
```
`$ bash ./install_host_app.sh firefox`
```
If you use multiple browsers, you can install it for each.
#### Pass Add-on
Once you've installed the host application, you can install an add-on or extension for your browser. Search for the `PassFF` plugin in your browser's add-on or extension manager.
![PassFF][12]
(Seth Kenlon, [CC BY-SA 4.0][13])
Install the add-on, and then close and re-launch your browser.
Navigate to a site you've got a password for in your password store. There's now a small **P** icon in the right of your login text fields.
![PassFF browser prompt][14]
(Seth Kenlon, [CC BY-SA 4.0][13])
Click on the **P** button to see a list of matching site names in your password store.
![PassFF browser menu][15]
(Seth Kenlon, [CC BY-SA 4.0][13])
Click the pen-and-paper icon to fill in the form or the paper-airplane icon to fill and auto-submit the form.
Easy password management and fully integrated!
### Try pass as your Linux password manager
The `pass` command is a great option for users who want to manage passwords and personal information using tools they already use on a daily basis. If you rely on GPG and a terminal already, then you may enjoy the `pass` system. It's also an important option for users who don't want their passwords tied to a specific application. Maybe you don't use just one browser, or you don't like the idea that it might be difficult to extract your passwords from an application if you decide to stop using it. With `pass`, you maintain control of your secrets in a UNIX-like and straightforward system.
--------------------------------------------------------------------------------
via: https://opensource.com/article/22/1/manage-passwords-linux-terminal
作者:[Seth Kenlon][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/seth
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/linux_keyboard_desktop.png?itok=I2nGw78_ (Linux keys on the keyboard for a desktop computer)
[2]: http://bitwarden.com
[3]: https://www.passwordstore.org/
[4]: https://opensource.com/article/20/6/homebrew-mac
[5]: https://gpgtools.org/
[6]: https://opensource.com/article/20/11/macports
[7]: mailto:seth@example.com
[8]: http://www.example.com
[9]: http://www.example.com.gpg
[10]: https://opensource.com/article/19/8/what-are-environment-variables
[11]: http://www.example.org
[12]: https://opensource.com/sites/default/files/uploads/passff.jpg (PassFF)
[13]: https://creativecommons.org/licenses/by-sa/4.0/
[14]: https://opensource.com/sites/default/files/uploads/passff-button-web.jpg (PassFF browser prompt)
[15]: https://opensource.com/sites/default/files/uploads/passff-menu-web.jpg (PassFF browser menu)