Installation and Configuration of SSH Service on Debian
https://cnzhx.net/blog/linux-server-ssh-key-authentication-configuration/
目录7 个章节
How to Configure SSH Key Authentication on Linux Server
Homepage > Blog > Linux Server SSH Key Authentication Configuration
SSH Key Authentication (SSH 密钥认证) vs. Password Authentication
Using SSH key authentication (SSH 密钥认证) is a more secure way to connect to a server compared to using a password. The principle is: you generate a pair of keys (a public key and a private key) on your client machine, and then place the public key on the server. When connecting, the server verifies the private key to confirm your identity.
The advantages include:
- Stronger security: A properly configured SSH key is almost impossible to crack.
- Convenience: You don't need to enter a password every time you connect.
Step 1: Generate an SSH Key Pair (客户端生成密钥对)
First, you need to generate a key pair on your local machine (client). Open a terminal and run the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-t rsa: Specifies the key type as RSA.-b 4096: Specifies the key length as 4096 bits, which is highly secure.-C "your_email@example.com": Adds a comment, typically an email address, to help identify the key.
The system will prompt you to enter a file location to save the key. Press Enter to use the default path (~/.ssh/id_rsa). Then, you will be asked to enter a passphrase (密码短语). You can set one for extra security or press Enter to leave it empty.
After completion, two files will be generated in the ~/.ssh/ directory:
id_rsa: Your private key. Keep it safe and never share it.id_rsa.pub: Your public key. This is the file you need to upload to the server.
Step 2: Upload the Public Key to the Server (将公钥上传到服务器)
You need to append the content of your public key (id_rsa.pub) to the ~/.ssh/authorized_keys file on the server.
Method 1: Using the ssh-copy-id command (recommended)
ssh-copy-id username@server_ip
This command will automatically handle the copying and setting the correct permissions.
Method 2: Manually copying
- Display the public key content on your local machine:
cat ~/.ssh/id_rsa.pub - Copy the entire output.
- Log in to the server using your password (or another method) and edit (or create) the
authorized_keysfile:mkdir -p ~/.ssh chmod 700 ~/.ssh nano ~/.ssh/authorized_keys - Paste the public key content into this file and save it.
- Set the correct file permissions:
chmod 600 ~/.ssh/authorized_keys
Step 3: Test the Connection (测试连接)
Now, try to log in to the server from your client:
ssh username@server_ip
If you set a passphrase, you will be prompted to enter it. Otherwise, you should be logged in directly without a password.
Step 4 (Optional but Recommended): Disable Password Authentication (禁用密码认证)
To fully utilize the security advantage of key authentication, you can disable password authentication on the server. This prevents brute-force password attacks.
- Edit the SSH daemon configuration file:
sudo nano /etc/ssh/sshd_config - Find the
PasswordAuthenticationline and change it to:PasswordAuthentication no - You may also want to ensure
PubkeyAuthenticationis set toyes:PubkeyAuthentication yes - Restart the SSH service to apply changes:
sudo systemctl restart sshd
Important: Make sure your SSH key authentication is working correctly before disabling password authentication, or you may lock yourself out of the server.
Conclusion (总结)
You have now successfully configured SSH key authentication (SSH 密钥认证) for your Linux server. This setup significantly enhances security and login convenience. Remember to keep your private key file (id_rsa) secure at all times.
_EOF_
Installation and Configuration of SSH Service on Debian
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
小心误关了NAS服务器!修改Linux的电源键功能
NAS居然离线关机了?电源键被傻逼系统接管了。改改`logind.conf`让这玩意忽略电源键,省得误操作重启关机。谁用谁知道,别真当NAS是玩具。
2025-10-27 · 系统配置 · 约 1 分钟
Ubuntu-18-04-安装最新版VLC播放器
添加软件源 sudo add-apt-repository ppa:videolan/master-daily sudo apt update Install sudo apt install vlc qtwayland5 运行VLC播放器 安装额外解码器 vlc-plugin-access-extra - multimedia player and streamer (extra access
2025-10-27 · 系统配置 · 约 1 分钟
Linux--使用Snap安装第三方微信客户端Electronic-Wechat
前言 腾讯估计是不会给Linux系统开发任何软件了,不过问题不大。我们有很多好用的第三方软件替代~~(无奈之举,摊手)~~ 关于Snap Ubuntu官网上的介绍 Snaps are applications packaged with all their dependencies to run on all popular Linux distributions from a single bu
2025-10-27 · 系统配置 · 约 1 分钟
Ubuntu-16-04-安装配置Docker
安装相关包 $ sudo apt-get update # 先更新一下软件源库信息 $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ software-properties-common 添加软件仓库 官方仓库 # 添加 Docker 官方的 GPG 密钥(为了确认所下载
2025-10-27 · 系统配置 · 约 1 分钟
讨论
共 0 条加载评论中…