远程连接服务器

1、什么是远程连接服务器 远程连接服务器通过文字或图形接口方式来远程登录系统,让你在远程终端前 登录 linux 主机以取得可操作主机接口(shell),而登录后的操作感觉就像是坐 在系统前面一样。

2、远程连接服务器的功能 分享主机的运算能力 服务器类型:有限度开放连接 工作站类型:只对内网开放

3、远程连接服务器的类型(以登录的连接界面来分类) 文字接口 明文传输:Telnet、RSH 等,目前非常少用 加密传输:SSH 为主,已经取代明文传输 图形接口:XDMCP、VNC、XRDP 等

4、文字接口连接服务器 SSH(Secure Shell Protocol,安全的壳程序协议),它可以通过数据包加密技 术将等待传输的数据包加密后再传输到网络上。ssh 协议本身提供两个服务器 功能:一个是类似 telnet 的远程连接使用 shell 的服务器;另一个就是类似 ftp 服务的 sftp-server,提供更安全的 ftp 服务。

5、连接加密技术简介 目前常见的网络数据包加密技术通常是通过“非对称密钥系统”来处理的。主要 通过两把不一样的公钥与私钥来进行加密与解密的过程。 公钥(public key):提供给远程主机进行数据加密的行为,所有人都可获得你 的公钥来将数据加密。 私钥(private key):远程主机使用你的公钥加密的数据,在本地端就能够使 用私钥来进行解密。私钥只有自己拥有。 SSH 工作过程:在整个通讯过程中,为实现 SSH 的安全连接,服务端与客户端 要经历如下五个阶段:

                          过程                                         说明


 配置文件

[root@localhost ~]# cd /etc/ssh
[root@localhost ssh]# vim sshd_config
ssh 【默认配置文件】
#Port 22 监听端口,默认监听 22 端口 【默认可修改】
#AddressFamily any         IPV4 和 IPV6 协议家族用哪个,any 表示二者均
有
#ListenAddress 0.0.0.0     指明监控的地址,0.0.0.0 表示本机的所有地址
【默认可修改】
#ListenAddress ::          指明监听的 IPV6 的所有地址格式

# The default requires explicit activation of protocol 1 
#Protocol 2 使用 SSH 第二版本
# HostKey for protocol version 1       一版的 SSH 支持以下一种秘钥形式
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2       使用第二版本发送秘钥,支持以下四种秘钥认证的存放位 
                                        置
HostKey /etc/ssh/ssh_host_rsa_key rsa         私钥认证 【默认】
#HostKey /etc/ssh/ssh_host_dsa_key dsa         私钥认证
HostKey /etc/ssh/ssh_host_ecdsa_key ecdsa         私钥认证
HostKey /etc/ssh/ssh_host_ed25519_key ed25519        私钥认证
#ServerKeyBits 1024 主机秘钥长度

# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV                  当有人使用 ssh 登录系统的时候,SSH 会记
                                    录信息,信息保存在/var/log/secure 里面
#LogLevel INFO                           日志的等级
# Authentication:
#LoginGraceTime 2m                       登录的宽限时间,默认 2 分钟没有输入密
码,则自动断开连接
#PermitRootLogin yes                     是否允许管理员远程登录,'yes'表示允许
#StrictModes yes                         是否让 sshd 去检查用户主目录或相关文件的
                                      权限数据
#MaxAuthTries 6                          最大认证尝试次数,最多可以尝试 6 次输入密码。
                                      之后需要等待某段时间后才能再次输入密码
#MaxSessions 10                           允许的最大会话数
AuthorizedKeysFile .ssh/authorized_keys    选择基于密钥验证时,客户端生
成一对公私钥之后,会将公钥放到.ssh/authorizd_keys里面
PasswordAuthentication yes                  是否允许支持基于口令的认证
ChallengeResponseAuthentication no          是否允许使用键盘输入身份验证,
也就是 xshell 的第三个登录方式
#UseDNS yes                               是否反解 DNS,如果想让客户端连接服务器端快一些, 
                                          这个可以改为 no
Subsystem sftp /usr/libexec/openssh/sftp-server   支持 SFTP ,如果注释掉,则不支持 sftp 
                                                   连接
AllowUsers user1 user2        登录白名单(默认没有这个配置,需要自己手动添加),允许远 
                           程登录的用户。如果名单中没有的用户,则提示拒绝登录

1.修改端口号

[root@localhost ~]# cd /etc/ssh
[root@localhost ssh]# vim sshd_config

修改为2222

Port 2222

保存退出,重启

[root@localhost ssh]# systemctl restart sshd
[root@localhost ssh]# 

使用xshell连接


 

 

连接成功

注意:如果执行该命令时出现以下提示,请换个端口再试: ValueError: Port tcp/4444 already defined Setenforce 0

第一步: 关闭防火墙: systemctl stop firewalld

第二步: 设置 selinux: setenforce 0

第三步重启服务: systemctl restart sshd

2. 拒绝root用户远程登陆

[root@localhost ~]# cd /etc/ssh
[root@localhost ssh]# vim sshd_config
PermitRootLogin no

把yes修改为no,重启服务systemctl restart sshd

使用xshell连接

 

 root用户无法登录

新建窗口,使用redhat普通用户连接

     

连接成功             

3.虚拟机之间通过密钥链接

创建密钥对,将密钥保存在对应目录

/root/.ssh/id_rsa 私钥文件         /root/.ssh/id_rsa.pub 公钥文件

[root@localhost ssh]# 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:rXwQvajcpmc7Z1xE8PR/fOEUk7pXbcPktfNFzFGVU9Y root@bogon
The key's randomart image is:
+---[RSA 3072]----+
|          ...  *&|
|         . o.. *E|
|        . ... *+=|
|         + ...oO*|
|        S o.  .o@|
|     . + o  .. .+|
|      o =...  .  |
|       o+.+      |
|      .o.=       |
+----[SHA256]-----+

切换到cd ~/.ssh/目录

[root@localhost ssh]# cd ~/.ssh/
[root@localhost .ssh]# ll
total 8
-rw-------. 1 root root 2590 Dec  7 02:41 id_rsa
-rw-r--r--. 1 root root  564 Dec  7 02:41 id_rsa.pub

  可以看到私钥文件,和公钥文件           

复制该公钥文件到服务端的该目录下:

[root@localhost .ssh]# scp /root/.ssh/id_rsa.pub
usage: scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]
            [-J destination] [-l limit] [-o ssh_option] [-P port]
            [-S program] source ... target

连接192.168.179.130

[root@localhost .ssh]# ssh root@192.168.179.130
root@192.168.179.130's password: 
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last failed login: Wed Dec  8 10:42:46 EST 2021 from 192.168.179.121 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Wed Dec  8 10:42:24 2021
[root@rhce ~]# 

连接成功

  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值