授权密钥列表是私钥还是公钥,ssh怎么用的授权密钥列表,请示例
时间: 2023-08-18 07:07:45 浏览: 40
授权密钥列表是公钥,用于授权用户访问远程服务器。SSH使用授权密钥列表来验证用户身份,而不是传统的用户名和密码。
在SSH中使用授权密钥列表的步骤如下:
1. 生成密钥对:使用ssh-keygen命令生成密钥对,包括公钥和私钥。
```
ssh-keygen -t rsa
```
2. 将公钥添加到远程服务器的授权密钥列表中:将公钥文件(通常是`~/.ssh/id_rsa.pub`)的内容添加到远程服务器的`~/.ssh/authorized_keys`文件中。
```
ssh-copy-id username@remote_host
```
3. 连接到远程服务器:使用ssh命令连接到远程服务器,此时SSH会自动使用授权密钥进行身份验证。
```
ssh username@remote_host
```
示例:
1. 在本地机器上生成密钥对:
```
ssh-keygen -t rsa
```
2. 将公钥添加到远程服务器的授权密钥列表中:
```
ssh-copy-id username@remote_host
```
3. 连接到远程服务器:
```
ssh username@remote_host
```
相关问题
ssh公钥私钥换算器
SSH公钥和私钥是一对密钥,它们被用于在SSH协议下进行身份验证和加密通信。这里提供一个简单的SSH公钥和私钥换算器的Python代码示例,可以将一个格式正确的SSH私钥转换为公钥或者将公钥转换为私钥。
```python
import base64
import hashlib
def generate_public_key(private_key):
"""
从SSH私钥生成SSH公钥
"""
key_type, _, private_key = private_key.split(' ', 2)
private_key = base64.b64decode(private_key)
if key_type == 'ssh-rsa':
# RSA公钥格式: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...
exponent_length = int.from_bytes(private_key[0:4], byteorder='big')
exponent = int.from_bytes(private_key[4:4+exponent_length], byteorder='big')
modulus_length = int.from_bytes(private_key[4+exponent_length:8+exponent_length], byteorder='big')
modulus = int.from_bytes(private_key[8+exponent_length:8+exponent_length+modulus_length], byteorder='big')
public_key = 'ssh-rsa ' + base64.b64encode(
b'\x00\x00\x00\x07ssh-rsa' + exponent_length.to_bytes(4, byteorder='big') + exponent.to_bytes((exponent.bit_length() + 7) // 8, byteorder='big') + modulus_length.to_bytes(4, byteorder='big') + modulus.to_bytes((modulus.bit_length() + 7) // 8, byteorder='big')
).decode('ascii')
return public_key
elif key_type == 'ssh-ed25519':
# Ed25519公钥格式: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMcDa...
public_key = 'ssh-ed25519 ' + base64.b64encode(private_key[32:]).decode('ascii')
return public_key
else:
raise ValueError('Unsupported key type')
def generate_private_key(public_key, private_key=None):
"""
从SSH公钥生成SSH私钥
"""
key_type, _, public_key = public_key.split(' ', 2)
public_key = base64.b64decode(public_key)
if key_type == 'ssh-rsa':
# RSA私钥格式: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQ...
if private_key is None:
raise ValueError('Private key is required for RSA key type')
exponent_length = private_key.find(b'\x00\x00\x00\x07ssh-rsa') + 11
exponent_length = int.from_bytes(private_key[exponent_length:exponent_length+4], byteorder='big')
exponent = int.from_bytes(private_key[exponent_length+4:exponent_length+4+exponent_length], byteorder='big')
modulus_length = int.from_bytes(private_key[exponent_length+4+exponent_length:exponent_length+4+exponent_length+4], byteorder='big')
modulus = int.from_bytes(private_key[exponent_length+4+exponent_length+4:exponent_length+4+exponent_length+4+modulus_length], byteorder='big')
private_key = b''.join([
b'\x00\x00\x00\x07ssh-rsa',
exponent_length.to_bytes(4, byteorder='big'),
exponent.to_bytes((exponent.bit_length() + 7) // 8, byteorder='big'),
modulus_length.to_bytes(4, byteorder='big'),
modulus.to_bytes((modulus.bit_length() + 7) // 8, byteorder='big'),
private_key[public_key.__len__():]
])
private_key = key_type.encode('ascii') + b' ' + base64.b64encode(private_key).replace(b'\n', b'') + b' '
return private_key
elif key_type == 'ssh-ed25519':
# Ed25519私钥格式: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMcDa...
if private_key is None:
# 使用公钥生成随机私钥
private_key = hashlib.sha256(public_key).digest()
private_key = key_type.encode('ascii') + b' ' + base64.b64encode(private_key).replace(b'\n', b'') + b' '
return private_key
else:
raise ValueError('Unsupported key type')
```
使用示例:
```python
# 生成RSA密钥对
from cryptography.hazmat.primitives.asymmetric import rsa
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
public_key = private_key.public_key()
ssh_private_key = generate_private_key('ssh-rsa ' + base64.b64encode(public_key.public_bytes(encoding=serialization.Encoding.DER, format=serialization.PublicFormat.PKCS1)).decode('ascii'), private_key=private_key.private_bytes(encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()))
ssh_public_key = generate_public_key(ssh_private_key)
# 生成Ed25519密钥对
from cryptography.hazmat.primitives.asymmetric import ed25519
private_key = ed25519.Ed25519PrivateKey.generate()
public_key = private_key.public_key()
ssh_private_key = generate_private_key('ssh-ed25519 ' + base64.b64encode(public_key.public_bytes(encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw)).decode('ascii'))
ssh_public_key = generate_public_key(ssh_private_key)
```
注意:这个示例代码仅适用于支持PKCS#1和PKCS#8格式的RSA和Ed25519密钥。如果要使用其他格式的密钥,代码需要进行相应的修改。此外,为了安全起见,不应该在生产环境中使用此代码,而应该使用已经被广泛测试和验证的SSH密钥生成库。
使用RSA进行ssh登录的原理,请示例
RSA是一种公钥加密算法,用于保护SSH连接的安全。其原理如下:
1. 服务端生成公钥和私钥。公钥被发送给客户端,私钥被用于解密客户端发送的信息。
2. 客户端使用公钥加密用户名和密码,并将其发送给服务端。
3. 服务端使用私钥解密客户端发送的信息,并验证用户名和密码是否正确。
4. 如果验证通过,服务端会生成一个会话密钥,使用公钥加密并发送给客户端。
5. 客户端使用私钥解密服务端发送的会话密钥,并使用该密钥加密后续的通讯数据。
示例:
1. 服务端生成公钥和私钥:
```
ssh-keygen -t rsa
```
2. 服务端将公钥发送给客户端,客户端保存到本地:
```
scp user@server:/path/to/public/key ~/.ssh/id_rsa.pub
```
3. 客户端使用公钥加密用户名和密码,并将其发送给服务端:
```
ssh -i ~/.ssh/id_rsa user@server
```
4. 服务端使用私钥解密客户端发送的信息,并验证用户名和密码是否正确。
5. 如果验证通过,服务端生成一个会话密钥,使用公钥加密并发送给客户端。
6. 客户端使用私钥解密服务端发送的会话密钥,并使用该密钥加密后续的通讯数据。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)