openssh

本文详细介绍如何在Linux环境下配置SSH无密码登录,包括生成公钥-私钥对、将公钥复制到远程主机、使用scp方法连接以及验证登录过程。通过本教程,读者可以学会如何简化SSH远程登录流程。

手动配置ssh

创建公钥-私钥对(客户机)

[root@kehuji ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:3Wv4KZmgFhs5dBqoS/1RzsX2xs5R/CB80a1tPfVhvO8 root@kehuji
The key's randomart image is:
+---[RSA 2048]----+
|              ...|
|              .++|
|     .   . . ..+*|
|    . o o.+.o *o=|
|   o . OSo.o.+ +o|
|  o . B +  .=.  o|
| . . . B ..*o. . |
|  .   =   +oo.  E|
|     .     .o    |
+----[SHA256]-----+

将公钥复制到远程主机上

[root@kehuji ~]# cd .ssh/
[root@kehuji .ssh]# ls
id_rsa  id_rsa.pub
[root@kehuji .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.194.129
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
The authenticity of host '192.168.194.129 (192.168.194.129)' can't be established.
ECDSA key fingerprint is SHA256:iYay6JB/dzmh28KVizE/kWaQsikP5hAmeH7hKkFRfts.
ECDSA key fingerprint is MD5:0a:d2:3d:59:98:3b:7e:37:3f:be:98:7d:65:de:0c:3d.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.194.129's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.194.129'"
and check to make sure that only the key(s) you wanted were added.

远程连接服务器

[root@kehuji .ssh]# ssh root@192.168.194.129
Last login: Tue Oct 22 14:10:01 2019 from 192.168.194.1
[root@service ~]# 

使用scp方法连接

创建公钥-私钥对

[root@kehuji ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LSe8hoP2IXRIoCRhMQ0W2BWCitIvofKtIwntjBEkcbk root@kehuji
The key's randomart image is:
+---[RSA 2048]----+
|=&Ooo.           |
|O+++             |
|*. ..            |
|= E. . . .       |
|.+ oo . S o      |
|= o..o . =       |
|oB o+ + o        |
|+ =..o +         |
| ..o  .          |
+----[SHA256]-----+

将公钥发送个服务端

[root@kehuji ~]# cd .ssh/
[root@kehuji .ssh]# 
[root@kehuji .ssh]# ls
id_rsa  id_rsa.pub  known_hosts
[root@kehuji .ssh]# scp id_rsa.pub root@192.168.194.129:/root/.ssh/authorized_keys
root@192.168.194.129's password: 
id_rsa.pub                               100%  393   175.7KB/s   00:00    
[root@kehuji .ssh]# 

在服务器端修改文件权限为600 目录权限为700

[root@service .ssh]# ll
总用量 4
-rw-r--r--. 1 root root 393 10月 22 14:28 authorized_keys
[root@service .ssh]# chmod 600 authorized_keys 
[root@service .ssh]# ll
总用量 4
-rw-------. 1 root root 393 10月 22 14:28 authorized_keys
[root@service .ssh]# 

测试远程登录

[root@kehuji .ssh]# ssh root@192.168.194.129
Last login: Tue Oct 22 14:31:46 2019 from 192.168.194.128
[root@service ~]# 
06-13
### OpenSSH 配置与使用教程 OpenSSH 是一种用于安全连接远程服务器的工具,支持加密通信和身份验证。以下是关于 OpenSSH 的配置与使用的详细说明。 #### 1. 安装 OpenSSH 在 Windows 和 Linux 系统中,安装 OpenSSH 的方法有所不同。以下分别介绍两种系统的安装方式。 - **Windows 上的安装** 在 Windows 中,可以通过「设置」或 PowerShell 安装 OpenSSH 客户端和服务器[^1]。 - 方法 1:通过「设置」安装 打开「设置」->「应用」->「可选功能」,然后选择「添加功能」,搜索并安装 OpenSSH 客户端或服务器。 - 方法 2:使用 PowerShell 安装 以管理员权限运行 PowerShell,并执行以下命令: ```powershell Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0 Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0 ``` - **Linux 上的安装** 在 Linux 系统中,可以使用包管理器安装 OpenSSH。例如,在基于 Debian 的系统中,可以运行以下命令: ```bash sudo apt update sudo apt install openssh-server openssh-client ``` #### 2. 配置 OpenSSH 服务器 OpenSSH 服务器的核心配置文件为 `/etc/ssh/sshd_config`。以下是几个关键配置项及其作用: - `Port`: 指定 SSH 服务监听的端口,默认为 22。为了提高安全性,建议更改默认端口[^3]。 ```bash Port 2222 ``` - `PermitRootLogin`: 控制是否允许 root 用户直接登录。推荐设置为 `no` 或 `prohibit-password`。 ```bash PermitRootLogin no ``` - `PasswordAuthentication`: 控制是否允许密码认证。推荐设置为 `no` 并使用密钥认证。 ```bash PasswordAuthentication no ``` - `PubkeyAuthentication`: 启用公钥认证。 ```bash PubkeyAuthentication yes ``` - `AuthorizedKeysFile`: 指定存储公钥的文件路径。默认为 `.ssh/authorized_keys`。 ```bash AuthorizedKeysFile .ssh/authorized_keys ``` 完成配置后,重启 SSH 服务以使更改生效: ```bash sudo systemctl restart sshd ``` #### 3. 使用 OpenSSH 客户端连接远程服务器 使用 OpenSSH 客户端可以通过以下命令连接到远程服务器: ```bash ssh username@remote_host -p port_number ``` 其中: - `username`: 远程服务器上的用户名。 - `remote_host`: 远程服务器的 IP 地址或主机名。 - `port_number`: SSH 服务监听的端口号(如果未更改,则省略 `-p` 参数)。 #### 4. 密钥认证的配置 为了提高安全性,建议使用密钥认证代替密码认证。以下是生成密钥对并配置的过程: - **生成密钥对** 使用 `ssh-keygen` 命令生成密钥对: ```bash ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 默认情况下,私钥将保存在 `~/.ssh/id_rsa`,公钥将保存在 `~/.ssh/id_rsa.pub`。 - **将公钥复制到远程服务器** 使用 `ssh-copy-id` 命令将公钥复制到远程服务器: ```bash ssh-copy-id username@remote_host ``` - **禁用密码认证** 修改 `/etc/ssh/sshd_config` 文件,将 `PasswordAuthentication` 设置为 `no`,然后重启 SSH 服务。 #### 5. 典型生态工具的使用 OpenSSH 提供了许多辅助工具,可以增强其功能和便利性[^2]: - **ssh-keygen**: 用于生成、管理和转换 SSH 密钥。 - **ssh-agent**: 用于存储私钥,以便在多个会话中重复使用。 - **scp**: 用于安全地复制文件。 - **sftp**: 提供安全的文件传输协议。 例如,使用 `scp` 复制本地文件到远程服务器: ```bash scp /path/to/local/file username@remote_host:/path/to/remote/directory ``` #### 6. 自动化工具的结合 OpenSSH 可以与自动化工具结合使用,例如 Ansible、Puppet 等,用于远程服务器的配置管理和自动化部署[^2]。这有助于简化大规模服务器环境中的管理任务。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值