如何免密ssh登录服务器

How to Add SSH Public Key to Server

*Public key authentication allows you to access a server via SSH without password. Here are two methods to copy the public ssh key to the server.*

I believe you understand the basic SSH concept. Your Linux server has ssh enabled. You have generated ssh keys on your personal computer. Now you want to upload your public key to the authorized keys of the server so that you can access it without typing your account password all the time.

This quick tutorial shows you two methods to add a public SSH key to the server.

Requirements

Before you see that, let’s be clear about what you should already have:

  • Your destination server should have ssh enabled
  • You should have generated public and private ssh keys (just use the command ssh-keygen -t rsa)
  • You should have a user account and password on the server. Even root account will do.
  • You should know the IP address of the server

Now that you have made sure of the above three requirements, let’s see how to use public key authentication.

The authentication is per user base so the public key goes in the intended user’s home.

Method 1: Automatically copy the ssh key to server

The first method is where the end user copies its personal computer’s public key to the list of the authorized keys on the remote server.

Here, I assume that you were able to log in to the remote server using ssh user_name@ip_of_server. It asks for your account’s password and you enter the server.

If you add your public key to the server, you should be able to log in without typing the password all the time.

OpenSSH provides a handy tool call called ssh-copy-id for copying ssh public keys to remote systems. It even creates required directories and files.

As I mentioned earlier, you should know the username and password to the server you want to access via public key authentication.

ssh-copy-id -i ~/.ssh/id_rsa.pub YOUR_USER_NAME@IP_ADDRESS_OF_THE_SERVER

When prompted, enter the password for your user account at the remote server. Your public key should be copied at the appropriate folder on the remote server automatically.

I have used ~/.ssh/id_rsa.pub because that is the default location for the public ssh key. If you have it at some other location, you should use that in the above command.

Method 2: Manually copy the public ssh key to the server

The first method had the action on the user side. Let’s say that you are the sysadmin and your server doesn’t allow SSH login via password. The only way to access the server is using SSH public key authentication.

In such a case, you can ask the end user to provide her/his public key. Now what you can do is to create .ssh/authorized_keys directory and then copy the public key here.

Let me show the steps.

Step 1: Get the public key

Ask the end user to provide the public key by typing the following command:

cat ~/.ssh/id_rsa.pub

It will show a long random string starting with ssh-rsa:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ3GIJzTX7J6zsCrywcjAM/7Kq3O9ZIvDw2OFOSXAFVqilSFNkHlefm1iMtPeqsIBp2t9cbGUf55xNDULz/bD/4BCV43yZ5lh0cUYuXALg9NI29ui7PEGReXjSpNwUD6ceN/78YOK41KAcecq+SS0bJ4b4amKZIJG3JWm49NWvoo0hdM71sblF956IXY3cRLcTjPlQ84mChKL1X7+D645c7O4Z1N3KtL7l5nVKSG81ejkeZsGFzJFNqvr5DuHdDL5FAudW23me3BDmrM9ifUmt1a00mWci/1qUlaVFft085yvVq7KZbF2OP2NQACUkwfwh+iSTP username@hostname

You can get this text via email or messaging tools. Normally, it shouldn’t be a problem.

Step 2: Create ssh directory in the user’s home directory (as a sysadmin)

Keep in mind that you have to create these new directories and files in the end user’s home directory, not your own (root/sysadmin).

mkdir -p /home/user_name/.ssh && touch /home/user_name/.ssh/authorized_keys

Now open this /home/user_name/.ssh/authorized_keys file with a text editor like Vim and add the public key of the user here:

vim /home/user_name/.ssh/authorized_keys

Save and close the file. It’s almost ready.

Step 3: Set appropriate permission to the file

Having appropriate file permission on the ssh file is very important otherwise you’ll see errors like Permission denied (publickey).

First, make sure to set the correct file permissions:

chmod 700 /home/user_name/.ssh && chmod 600 /home/user_name/.ssh/authorized_keys

You created those file with either root or your own admin accounts for some other user. You need to change the ownership to the user:

chown -R username:username /home/username/.ssh

Now that it’s done, you can ask the end user to log in to the server.

Do let me know if you face any issues or if you have any suggestion on this topic.


原文:https://linuxhandbook.com/add-ssh-public-key-to-server/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSH (Secure Shell) 免密登录 Linux 服务器是一种安全的远程访问协议,它允许用户在不输入用户名和密码的情况下,从一台计算机连接到另一台远程服务器执行命令或管理文件。以下是设置 SSH 免密登录的基本步骤: 1. **安装 SSH**: 在 Linux 服务器上,如果你还没有安装 SSH,可以通过包管理器安装,如 Debian/Ubuntu 使用 `apt-get install openssh-server`,Red Hat/CentOS 则用 `yum install openssh-server`。 2. **配置 SSH 私钥对**: - 首先,在本地机器生成一个新的 SSH 密钥对,通常在 `~/.ssh` 目录下: ``` ssh-keygen -t rsa ``` - 接着,会提示选择保存位置,直接按回车默认即可,然后确认操作。私钥(id_rsa)将会被保留,公钥(id_rsa.pub)将自动复制到剪贴板,粘贴到服务器的 authorized_keys 文件中。 3. **将公钥添加到服务器**: 登录服务器,编辑 `.ssh/authorized_keys` 文件(如果不存在,创建一个并追加公钥内容),并将你的公钥粘贴进去。确保权限设置为 600(只读给用户和组): ``` chmod 600 ~/.ssh/authorized_keys ``` 4. **测试免密登录**: 回到本地,尝试使用 `ssh user@server_ip`(将 `user` 替换为实际用户名,`server_ip` 为服务器的 IP 地址),如果没有密码提示,则说明免密登录已经设置成功。 5. **防火墙设置**: 如果你的服务器开启了防火墙,可能需要允许来自特定 IP 或 IP 地址范围的 SSH 连接。你可以通过修改防火墙规则(比如 iptables 或 ufw)来开放相应的端口(通常是 22)。 相关问题: 1. 如何查看当前已有的 SSH 密钥? 2. SSH 免密登录的安全性如何保障? 3. 如何检查服务器上的 SSH 是否启用并监听?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值