返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
系统配置

Installation and Configuration of SSH Service on Debian

https://cnzhx.net/blog/linux-server-ssh-key-authentication-configuration/

曦远 (DealiAxy)约 11 分钟
目录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

  1. Display the public key content on your local machine:
    cat ~/.ssh/id_rsa.pub
    
  2. Copy the entire output.
  3. Log in to the server using your password (or another method) and edit (or create) the authorized_keys file:
    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    nano ~/.ssh/authorized_keys
    
  4. Paste the public key content into this file and save it.
  5. 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.

To fully utilize the security advantage of key authentication, you can disable password authentication on the server. This prevents brute-force password attacks.

  1. Edit the SSH daemon configuration file:
    sudo nano /etc/ssh/sshd_config
    
  2. Find the PasswordAuthentication line and change it to:
    PasswordAuthentication no
    
  3. You may also want to ensure PubkeyAuthentication is set to yes:
    PubkeyAuthentication yes
    
  4. 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 许可协议。转载请注明出处!

相关文章

Nginx配置http跳转https-强制https访问

前言 最近上线了好几个项目,同时也申请了相应的SSL证书,不过发现了一个问题,用户在浏览器直接输入域名的时候,浏览器默认访问的是http站点,然而我们的网站只支持https访问,所以就访问不了咯。 其实可以在nginx全局配置里面把所有访问重定向到https,不过服务器有些站点是提供http服务的,所以只能每个站点分别配置。 根据搜索引擎找到的资料,本文总结了三种配置方法,有需要的朋友可以选择中意

2025-10-27 · 系统配置 · 约 1 分钟

给VMware里的Linux虚拟机配置静态ip

前言 最近用VMware的时候是使用桥接方式,不知道是路由的问题还是其他,路由器里面没有显示虚拟机的连接,虽然虚拟机和主机都在路由器里分配了不同的IP地址,但是每次resume之后虚拟机的IP地址总是变,很麻烦。 VMware设置 把下面这个勾去掉。 然后点击Nat Settings。 记录网关。 虚拟机设置。把网络改成这个VMnet8。 Linux系统中配置静态IP Debian nano /e

2025-10-27 · 系统配置 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…

Installation and Configuration of SSH Service on Debian · StarBlog