CentOS 开启SSH服务,并使用Xshell 连接

由于本机是win10,在本地安装了Docker,打算部署一个开发环境,使用cmd方式进入docker 使用感受不佳,因此使用Xshell进行连接,效果优于使用VM虚拟机,关于如何配置docker镜像,查看以前的笔记,现在对这个配置过程进行记录……

win10

Docker

image: CentOS8

 

首先,确保CentOS8 已经安装了openssh-server, 使用yun list installed | grep openssh-server 命令来查看是否看装。

[root@192eee0845e0 ~]# yum list installed | grep openssh-server
openssh-server.x86_64               8.0p1-4.el8_1                     @BaseOS

没有安装的话,运行:yum install openssh-server

确保已经安装完毕后,使用find命令,查找/etc/ssh/目录下的sshd_config服务配置文件

[root@192eee0845e0 ~]# find / -name sshd_config
/etc/ssh/sshd_config

编辑sshd_config

[root@192eee0845e0 ~]# vi /etc/ssh/sshd_config

修改sshd_config 如下:

1. 去掉#,开启22端口,监听地址
Port 22
#AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

2.允许远程登陆
#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

3.开启使用用户名和密码作为连接验证
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

保存退出后,即可开启ssh服务:

systemctl start sshd.service

使用Xshell 连接时候,需要提供用户名和密码

设置用户名,密码时候需要先安装passwd

[root@192eee0845e0 ~]# yum install passwd
Last metadata expiration check: 0:18:58 ago on Tue 03 Mar 2020 08:32:11 AM UTC.
Dependencies resolved.
========================================================================================================================
Package                    Architecture              Version                           Repository                 Size
========================================================================================================================
Installing:
passwd                     x86_64                    0.80-2.el8                        BaseOS                    114 k
Installing dependencies:
libuser                    x86_64                    0.62-23.el8                       BaseOS                    417 k


Transaction Summary
========================================================================================================================
Install  2 Packages


Total download size: 531 k
Installed size: 2.4 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): passwd-0.80-2.el8.x86_64.rpm                                                     197 kB/s | 114 kB     00:00
(2/2): libuser-0.62-23.el8.x86_64.rpm                                                   667 kB/s | 417 kB     00:00
------------------------------------------------------------------------------------------------------------------------
Total                                                                                   422 kB/s | 531 kB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : libuser-0.62-23.el8.x86_64                                                                     1/2
  Running scriptlet: libuser-0.62-23.el8.x86_64                                                                     1/2
  Installing       : passwd-0.80-2.el8.x86_64                                                                       2/2
  Running scriptlet: passwd-0.80-2.el8.x86_64                                                                       2/2
  Verifying        : libuser-0.62-23.el8.x86_64                                                                     1/2
  Verifying        : passwd-0.80-2.el8.x86_64                                                                       2/2


Installed:
  passwd-0.80-2.el8.x86_64                                  libuser-0.62-23.el8.x86_64


Complete!

//设置root账户的密码
[root@192eee0845e0 ~]# passwd root
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.

由于宿主机是本机,win10,首先查看本机IP:

C:\Windows\system32>ipconfig
Windows IP 配置




以太网适配器 以太网 3:


   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . :


以太网适配器 以太网:


   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . :


以太网适配器 Npcap Loopback Adapter:


   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::e9fc:f717:45d1:bf5e%68
   自动配置 IPv4 地址  . . . . . . . : 169.254.191.94
   子网掩码  . . . . . . . . . . . . : 255.255.0.0
   默认网关. . . . . . . . . . . . . :


无线局域网适配器 WLAN:


   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::f4de:d9e8:2db9:751a%5
   IPv4 地址 . . . . . . . . . . . . : 192.168.1.100
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : 192.168.1.1


以太网适配器 蓝牙网络连接:


   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . :


无线局域网适配器 本地连接* 1:


   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . :


无线局域网适配器 本地连接* 10:


   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . :


无线局域网适配器 本地连接* 11:


   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . :


以太网适配器 vEthernet (Default Switch):


   连接特定的 DNS 后缀 . . . . . . . :
   本地链接 IPv6 地址. . . . . . . . : fe80::147a:7e74:eac4:3b0d%49
   IPv4 地址 . . . . . . . . . . . . : 172.17.91.177
   子网掩码  . . . . . . . . . . . . : 255.255.255.240
   默认网关. . . . . . . . . . . . . :

得到了连接的必备条件以后,就可以使用Xshell,由于我是使用docker,因此,端口处需要填写映射端口,我的映射端口是5022

CentOS 开启SSH服务,并使用Xshell 连接_第1张图片

CentOS 开启SSH服务,并使用Xshell 连接_第2张图片

点击连接,就搞定了

CentOS 开启SSH服务,并使用Xshell 连接_第3张图片


这个期间,遇到的问题如下

问题场景:使用Xshell 远程访问docker --开启ssh,遇到

1.开启服务时候:
[root@192eee0845e0 ~]# service sshd start
bash: service: command not found                       //在centos系统中,如果/sbin目录下没有service这个命令,就会出现

2.检查端口:
[root@192eee0845e0 /]# netstat -an | grep 22
bash: netstat: command not found

解决方式:

1.
[root@192eee0845e0 /]# yum list | grep initscripts      //列出可安装的的initscripts的yum版本源
initscripts.x86_64                                   10.00.4-1.el8                                     BaseOS
[root@192eee0845e0 /]# yum install initscripts -y       //安装
Last metadata expiration check: 0:48:03 ago on Tue 03 Mar 2020 08:32:11 AM UTC.
Dependencies resolved.
========================================================================================================================
Package                       Architecture             Version                          Repository                Size
========================================================================================================================
Installing:
initscripts                   x86_64                   10.00.4-1.el8                    BaseOS                   338 k


Transaction Summary
========================================================================================================================
Install  1 Package


Total download size: 338 k
Installed size: 1.0 M
Downloading Packages:
initscripts-10.00.4-1.el8.x86_64.rpm                                                    127 kB/s | 338 kB     00:02
------------------------------------------------------------------------------------------------------------------------
Total                                                                                   106 kB/s | 338 kB     00:03
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : initscripts-10.00.4-1.el8.x86_64                                                               1/1
  Running scriptlet: initscripts-10.00.4-1.el8.x86_64                                                               1/1
  Verifying        : initscripts-10.00.4-1.el8.x86_64                                                               1/1


Installed:
  initscripts-10.00.4-1.el8.x86_64


Complete!
[root@192eee0845e0 /]# service sshd start          //旧的centos版本使用这个
Redirecting to /bin/systemctl start sshd.service
[root@192eee0845e0 /]# systemctl start sshd.service  //centos7及以上,现在都使用systemctl
[root@192eee0845e0 /]#                               //表示开启成功

2. 安装net-tools
[root@192eee0845e0 /]# netstat -an | grep 22
bash: netstat: command not found
[root@192eee0845e0 /]# yum install net-tools
Last metadata expiration check: 0:46:26 ago on Tue 03 Mar 2020 08:32:11 AM UTC.
Dependencies resolved.
========================================================================================================================
Package                   Architecture           Version                                  Repository              Size
========================================================================================================================
Installing:
net-tools                 x86_64                 2.0-0.51.20160912git.el8                 BaseOS                 323 k


Transaction Summary
========================================================================================================================
Install  1 Package


Total download size: 323 k
Installed size: 1.0 M
Is this ok [y/N]: y
Downloading Packages:
net-tools-2.0-0.51.20160912git.el8.x86_64.rpm                                           657 kB/s | 323 kB     00:00
------------------------------------------------------------------------------------------------------------------------
Total                                                                                   307 kB/s | 323 kB     00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                1/1
  Installing       : net-tools-2.0-0.51.20160912git.el8.x86_64                                                      1/1
  Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64                                                      1/1
  Verifying        : net-tools-2.0-0.51.20160912git.el8.x86_64                                                      1/1


Installed:
  net-tools-2.0-0.51.20160912git.el8.x86_64

验证一下:
[root@192eee0845e0 /]# netstat -an | grep 22
unix  2      [ ]         DGRAM                    137227
unix  3      [ ]         DGRAM                    133622 

 

 

 

你可能感兴趣的:(环境配置)