Centos基础-远程控制服务

环境说明

1.需要两台centos7   cpu  2   2    内存4G   硬盘20   适量缩小也可

2.两台设备必须打开网络,一台作为server,一台作为client,大家可以修改hostname来区分

3.首先需要删除Centos7的原有所有yum源,

cd   /etc/yum.repos.d    

然后   rm   -rf    *  

接下来配置阿里云的Centos7的软件仓库

curl  -o    /etc/yum.repos.d/Centos-7.repo  http://mirrors.aliyun.com/repo/Centos-7.repo

建立软件仓库cache

[root@centos7-server yum.repos.d]# yum clean all 

Loaded plugins: fastestmirror

Cleaning repos: base extras updates

Cleaning up list of fastest mirrors

[root@centos7-server yum.repos.d]# yum repolist all 

Loaded plugins: fastestmirror

Determining fastest mirrors

 * base: mirrors.aliyun.com

 * extras: mirrors.aliyun.com

 * updates: mirrors.aliyun.com

base                                                                                | 3.6 kB  00:00:00     

extras                                                                              | 2.9 kB  00:00:00     

updates                                                                             | 2.9 kB  00:00:00     

(1/4): base/7/x86_64/group_gz                                                       | 153 kB  00:00:00     

(2/4): extras/7/x86_64/primary_db                                                   | 243 kB  00:00:00     

(3/4): base/7/x86_64/primary_db                                                     | 6.1 MB  00:00:01     

(4/4): updates/7/x86_64/primary_db                                                  |  13 MB  00:00:02     

repo id                             repo name                                               status

base/7/x86_64                       CentOS-7 - Base - mirrors.aliyun.com                    enabled: 10,072

centosplus/7/x86_64                 CentOS-7 - Plus - mirrors.aliyun.com                    disabled

contrib/7/x86_64                    CentOS-7 - Contrib - mirrors.aliyun.com                 disabled

extras/7/x86_64                     CentOS-7 - Extras - mirrors.aliyun.com                  enabled:    500

updates/7/x86_64                    CentOS-7 - Updates - mirrors.aliyun.com                 enabled:  3,413

repolist: 13,985

 

  1. 接下来可以安装一下日常一直要使用的工具

首先下载自动补齐命令的插件  yum  install  -y  bash-completion

为了能使用ifconfig命令我们可以安装  yum install  -y  net-tools

我们还可以安装vim编辑器     yum install  -y   vim

为了下载方便 我们还可以安装wget工具   yum  install -y  wget

5.测试系统没问题并且软件仓库都正常 关机打快照 记得关机前修改hostname这样可以区分server端和client端

 

 

远程控制服务和堡垒机

配置sshd服务 

sshd服务是基于SSH协议开发的一款用于远程管理服务程序,不仅使用起来方便快捷,而且能够提供两种安全验证的方法:

  1. 基于口令的验证-用账户密码来验证登录
  2. 基于密钥的验证:需要在本地生成密钥对,然后把秘钥对中的公钥上传至服务器,并且与服务器中的公钥进行比对,这种方法是相对来说更安全的做法

 

sshd服务的配置文件保存在 /etc/ssh/sshd_config文件中。

运维人员一般会把保存着最主要的配置信息的文件称为主配置文件,而配置文件中有许多以#开头的注释行,如果想要让这些参数配置生效,需要在修改参数后,再把前面的#给删掉

ssh服务配置文件中包含的参数以及作用:

参数

作用

Port 22

默认的sshd服务端口

ListenAddress 0.0.0.0

设定sshd服务器监听的IP地址

Protocol 2

SSH协议的版本号

HostKey /etc/ssh/ssh_host_key

SSH协议版本为1时,DES私钥存放的位置

HostKey /etc/ssh/ssh_host_rsa_key

SSH协议版本为2时,RSA私钥存放的位置

HostKey /etc/ssh/ssh_host_dsa_key

SSH协议版本为2时,DSA私钥存放的位置

PermitRootLogin yes

设定是否允许root管理员直接登录

StrictModes yes

当远程用户的私钥改变时直接拒绝连接

MaxAuthTries 6

最大密码尝试次数

MaxSessions 10

最大终端数

PasswordAuthentication yes

是否允许密码验证

PermitEmptyPasswords no

是否允许空密码登录(很不安全)

 

 

 

 

 

安全密钥验证:

  1. 在客户端主机中生成密钥对

[root@centos7-client ~]# ssh-keygen 

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:REBeUBtA6l0o0VoHAezBGLBruimHIPlBTtF++kW0r5I root@centos7-client

The key's randomart image is:

+---[RSA 2048]----+

|...*o=OB=        |

| .o =+o+oo       |

|.  +o+oo+.       |

| .o.+o.oo        |

|.*  .o..S.       |

|* o .   . .      |

|+o . . o .       |

|ooo   E .        |

|+.     .         |

+----[SHA256]-----+

 

  1. 把客户端主机中生成的公钥文件传送至远程主机

[root@centos7-client ~]# ssh-copy-id 192.168.40.204

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"

The authenticity of host '192.168.40.204 (192.168.40.204)' can't be established.

ECDSA key fingerprint is SHA256:rgt6DR6pw0ZKIIrjQLoSGYGGbBBkiNtw4p12grQjgE4.

ECDSA key fingerprint is MD5:8b:41:29:af:dc:96:37:f7:24:e5:40:4d:32:96:b4:65.

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.40.204's password: 

 

Number of key(s) added: 1

 

Now try logging into the machine, with:   "ssh '192.168.40.204'"

and check to make sure that only the key(s) you wanted were added.

 

 

  1. 对服务器进行设置,使其只允许秘钥验证,拒绝传统的口令验证方式。在修改完配置文件后,必须要重启服务才能生效

vim   /etc/ssh/sshd_config

..................

65 PasswordAuthentication no

...................

systemctl restart sshd

 

  1. 在客户端尝试登录到服务器,此时无需输入密码也可以成功登录

[root@centos7-client ~]# ssh  192.168.40.204

Last login: Tue Feb 15 20:39:37 2022 from 192.168.40.1

[root@centos7-server ~]# 

如何退出

[root@centos7-server ~]# exit

logout

Connection to 192.168.40.204 closed.

 

不间断会话服务

当与远程主机会话被关闭时,在远程主机上运行的命令也会随之被中断

screen是一款能够实现多窗口远程控制的开源服务程序,简单来说就是为了解决网络异常中断或者为了同时控制多个远程终端而设计的程序,用户可以使用screen服务程序同时在多个远程绘画中自由切换,能够实现如下功能:

  1. 会话恢复:即便网络终端,也可以会话随时恢复,确保用户不会时区对远程会话的控制
  2. 多窗口:每个会话都是独立运行的。拥有各自独立的输入输出终端窗口,终端窗内显示过的信息。也会被分开隔离保存,以便下次依然能看到之前的操作记录
  3. 会话共享,当多个用户同时登录到远程服务器时,可以使用会话共享让用户之间输入输出信息共享

服务安装:

root@centos7-client ~]# yum install   -y   screen

 

管理远程会话:

screen命令能做的事情:

-S  创建会话窗口

-d  将指定会话窗口离线处理

-x   一次性恢复会有的会话

-ls   查看当前已有的会话

-wipe  把目前无法使用的会话删除

 

尝试进行screen会话:

1.screen  -S  windows

虽然大家看起来和刚刚没什么不同,但是已经进入screen会话当中

screen  ls

 

[root@centos7-client ~]# screen   -ls

There are screens on:

10448.windows (Attached)

1 Sockets in /var/run/screen/S-root.

 

想要退出关闭会话也十分简单,只需要在命令行当中执行exit命令就可以

 

在日常生产环境当中,其实并不是需要先创立会话,然后再开始工作。可以直接使用screen命令执行你所要运行的命令,这样在命令中一切操作都会被记录下来,当命令执行完成后,screen会话也会自动结束。

screen   ping  -c  20   www.baidu.com

[screen is terminating]

 

会话共享功能

在第一个终端上  [root@centos7-client ~]# screen  -S    linux1  创建会话 

接下来我们可以输入相关命令

在另一台终端上  我们只需要  [root@centos7-client ~]# screen -x  linux1   恢复会话  

两边命令敲了什么都可以共享  当一边exit退出  两边相同的会话都会结束

[screen is terminating]

posted @ 2022-02-21 19:49  终究还是避免不了遗憾  阅读(267)  评论(0编辑  收藏  举报