0Pricing
Linux Command Line Mastery · 课时

安全 Shell 密钥管理

使用密钥对实现无需密码的 SSH 身份验证,提升安全性和便利性。

安全 Shell 密钥管理 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are SSH Keys?

SSH keys are a secure way to log into a remote server without needing a password. Think of them as a digital key and lock system.

  • They provide stronger security than traditional passwords.
  • They offer convenience by enabling passwordless logins.
  • This lesson will guide you through setting them up.

Public and Private Key Pair

An SSH key system uses two parts: a public key and a private key.

  • Your private key stays on your local computer and must be kept secret. Never share it!
  • Your public key can be freely shared. You place it on any server you want to connect to.
  • When you try to connect, the server uses your public key to verify your private key, allowing access.

Generating Your Own SSH Keys

You create an SSH key pair using the ssh-keygen command in your terminal.

By default, it creates keys in the ~/.ssh/ directory (a hidden folder in your home directory). The most common key type is RSA.

You'll be prompted for a passphrase. This adds an extra layer of security to your private key, encrypting it. It's highly recommended!

Hands-on: Using `ssh-keygen`

Let's generate an RSA key pair. When prompted, you can press Enter to use default file locations and choose a strong passphrase (or leave it empty for no passphrase, though not recommended for security).

ssh-keygen -t rsa -b 4096

Understanding Key Files

After running ssh-keygen, you'll find two new files in your ~/.ssh/ directory:

  • id_rsa: This is your private key. Keep it safe and never share it!
  • id_rsa.pub: This is your public key. You'll copy this to remote servers.

The command also shows a key fingerprint, a unique identifier for your key pair.

Copying Your Public Key to a Server

To enable passwordless login, your public key needs to be on the remote server. The easiest way to do this is with the ssh-copy-id command.

It automatically appends your public key to the ~/.ssh/authorized_keys file on the server. You'll need to enter the server's password just this one time.

ssh-copy-id user@remote_host

Manual Public Key Installation

If ssh-copy-id isn't available on your system, you can manually copy your public key. This involves reading your local public key and piping it to the remote server via SSH, appending it to the authorized_keys file.

Make sure the .ssh directory exists and has correct permissions on the server (chmod 700 ~/.ssh).

cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys"

Testing Passwordless Login

Once your public key is on the server, try logging in. If you set a passphrase, you'll be prompted for it. If not, you should connect directly without any password prompts!

This confirms that your SSH key authentication is working correctly.

ssh user@remote_host

Using `ssh-agent` for Convenience

If your private key has a passphrase, you'll be asked for it every time you connect. The SSH agent helps by storing your decrypted private key in memory.

  • Start the agent: eval "$(ssh-agent -s)"
  • Add your key: ssh-add ~/.ssh/id_rsa (enter passphrase once)

Now you can connect multiple times without re-entering your passphrase until the agent restarts.

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Quick Check: SSH Key Files

Which of the following files contains your public key and is safe to share with remote servers?

Recap: Secure Key Management

You've learned how to set up and manage SSH key pairs for secure, passwordless authentication.

  • Generate keys with ssh-keygen.
  • Understand private (id_rsa) and public (id_rsa.pub) keys.
  • Copy public keys to servers using ssh-copy-id or manually.
  • Use ssh-agent and ssh-add for passphrase convenience.

SSH keys are a fundamental tool for efficient and secure remote access in Linux!

常见问题解答

「安全 Shell 密钥管理」课时是免费的吗?

是的 — 「安全 Shell 密钥管理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。

「安全 Shell 密钥管理」这节课中我会学到什么?

使用密钥对实现无需密码的 SSH 身份验证,提升安全性和便利性。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Linux Command Line Mastery 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「安全 Shell 密钥管理」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?

能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 网络诊断:`traceroute`、`nslookup`、`dig`
  2. 防火墙管理:`ufw`、`firewalld`、`iptables`
  3. 安全 Shell 密钥管理
  4. 使用 tcpdump 捕获并检查流量
← 返回 Linux Command Line Mastery