一.远程连接介绍
在对服务器进行管理时,需要通过网络在远程主机中开启 shell 来进程操作
远程主机必须提供这样的开启 shell 的方式。
以上的需求由 openssh 服务来提供,远程主机中开启了openssh这个程序,就可以通过网络来连接这台主机了
1.SSH协议
SSH是一种网络协议,用于计算机之间的加密登录。
SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。
2.openssh服务
OpenSSH 是 SSH (Secure SHell) 协议的免费开源软件。
- 软件安装名称
客户端:ssh
服务端:openssh-server - 服务端配置文件:/etc/ssh/sshd_conf
- 默认端口:22
- 客户端命令:ssh
二.远程连接实现
1.环境准备
配置两台主机IP:
client:172.25.254.10
server: 172.25.254.20
# 关闭firewalld防火墙
# 临时关闭
systemctl stop firewalld
# 关闭开机自启动
systemctl disable firewalld
# 关闭selinux
# 临时关闭
setenforce 0
# 修改配置文件 永久关闭
vim /etc/selinux/config
SELINUX=disabled
Tips:
使用w -i命令可以查看当前登入该主机的用户
2.使用client登陆server主机
# 在client端:
ssh root@172.25.254.20 # 以root用户
# 第一次登陆会出现以下情况:
[root@client ~ ]# ssh root@172.25.254.20
The authenticity of host '172.25.254.20 (172.25.254.20)' can't be established.
ECDSA key fingerprint is
SHA256 :+ 5Ikt8ZExrRdLzOg19xsL5PhAYPt8GB7umvLcSF / dlM.
# 你确定要建立连接吗?直接填yes
Are you sure you want to continue connecting (yes / no)? yes
Warning: Permanently added '172.25.254.20' (ECDSA) to the list of known hosts.
# 输入server端的密码
root@172.25.254.20's password: *****
Activate the web console with : systemctl enable -- now cockpit.socket
Last login: Sat Apr 15 10:06:39 2021
# 看到主机名切换到server 表示远程登陆成功
[root@server ~ ]#
3.免密登陆的实现
最终实现client端可以免密登陆到server端
☆普通用户常用
① 在client主机针对某个用户账号生成公钥与私钥
ssh-keygen
[root@centos ~]# ssh-keygen
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:jZED2HNfFLTe2S4ehSd2f96aETc4lOqKocWYvFK60jU root@centos.cn
The key's randomart image is:
+---[RSA 2048]----+
| o. o+. |
| . o... ... |
| o+. ..o |
| =..o..+ |
| S ...oB.*|
| .E+ . ..Oo|
| . ++.+ . + +|
| . + + o . . *o|
| ..oo . . +.o|
+----[SHA256]-----+
一路确认即可
② 使用ssh-copy-id把公钥文件中的内容传输到服务器端的~/.ssh/authorized_keys文件中
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.20
③ 在client端测试免密登录是否成功
ssh root@172.25.254.20
☆集群常用
① 生成公钥与私钥
ssh-keygen
② 把id_rsa.pub文件,scp到server服务器端
scp ~/.ssh/id_rsa.pub root@172.25.254.20:/home/root/
③ 在server服务器端,把id_rsa.pub文件中的内容追加到~/.ssh/authorized_keys文件中
cd ~
cat id_rsa.pub >> ~/.ssh/authorized_keys
注意事项:以上配置也比较简单,但是实际应用时要注意文件的权限
④ 测试免密是否成功
ssh root@172.25.254.20
4.Openssh服务的安全配置
☆例:修改默认端口
# 在server端:
vim /etc/ssh/sshd_config
17 port 1111 # sshd默认端口号为22 此时修改为1111
systemctl restart sshd
或
systemctl reload sshd
测试:
# 在client端:
# 1.使用默认端口连接被拒绝
ssh 172.25.254.20 -l root
# 2.使用指定端口连接成功
ssh root@172.25.254.20 -p 1111
配置文件: /etc/ssh/sshd_config
☆其他常用配置参数
常用配置参数 | 含义 |
---|---|
Port 22 | 监听端口 |
Protocol2 | 指定协议版本 |
ListenAddress | 绑定IP |
HostKey | 设定HostKey密钥路径 |
PermitRootLogin | 设定超级用户是否能登录 |
PubkeyAuthentication | 公钥认证开关 |
PasswordAuthentication | 密码认证开关 |
AllowUsers | 用户白名单 |
DenyUsers | 用户黑名单 |