RHCE---配置NTP时间服务器--配置SSH免密登陆

目录

一,NTP协议

        1.1ntp简介

        1.2ntp服务安装

        1.3配置时间服务器客户端

        1.4配置时间服务器服务端

二,SSH服务

        2.1远程连接服务器

        2.2连接加密技术简介

        2.3 ssh服务配置

        2.4用户登录ssh服务器

三,案例

        (1)配置ntp时间服务器,确保客户端主机能和服务主机同步时间

        (2)配置ssh免密登陆,能够通过客户端主机通过redhat用户和服务端主机基于公钥验证方式进行远程连接

一,NTP协议

1.1ntp简介

NTP 是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步。Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软件。它能让计算机保持系统时钟与时 钟服务器(NTP)同步,因此让你的计算机保持精确的时间,Chrony也可以作为服务端软件为其他计算 机提供时间同步服务。

Chrony由两个程序组成,分别是chronyd和chronyc。

chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算 机增减时间的比率,并对此进行补偿。

chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机 上工作,也可以在一台不同的远程计算机上工作。

1.2ntp服务安装

#设置当前时区

[root@localhost ~]# timedatectl set-timezone Asia/Shanghai

[root@localhost ~]# yum install -y chrony

[root@localhost ~]# systemctl enable --now chronyd

[root@server1 ~]# cat /etc/chrony.conf

#设置时间服务器的服务端名字
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst    
# Allow NTP client access from local network.
#设置允许哪个客户端可以访问该服务器
#allow 192.168.0.0/16

1.3配置时间服务器客户端

[root@server1 ~]# vim /etc/chrony.conf
#此处服务器可以写国内的

server ntp.aliyun.com iburst
[root@localhost ~]# systemctl restart chronyd

[root@server1 ~]# chronyc sources

210 Number of sources = 2

MS Name/IP address         Stratum Poll Reach LastRx Last sample

===============================================================================

^* 203.107.6.88                  2   6     7     1   -705us[ -5240s] +/-   28ms
^? ntp1.flashdance.cx            0   6     0     -     +0ns[   +0ns] +/-   0ns

* 表示chronyd当前同步到的源

+ 表示可接受的信号源,与选定的信号源组合在一起 ? 指示已失去连接性或其数据包未通过所有测试的源。它也显示在启动时,直到从中至少收集了3

个样本为止

1.4配置时间服务器服务端

#需要两个服务器,一个服务端(服务端ip:192.168.168.253),一个客户端测试(客户端主机ip网段
为192.168.168.0/24)

#服务端的配置

[root@localhost ~]# systemctl disable --now firewalld

[root@localhost ~]# grep allow /etc/chrony.conf

allow 192.168.168.0/24

#allow 0.0.0.0/0

[root@localhost ~]# systemctl restart chronyd
#客户端访问,先ping时间服务器

[root@localhost ~]# ping 192.168.168.253

[root@localhost ~]# grep iburst /etc/chrony.conf

server 192.168.168.253 iburst
[root@localhost ~]# systemctl restart chronyd

[root@localhost ~]# chronyc sources

二,SSH服务

2.1远程连接服务器

(1)什么是远程连接服务器 远程连接服务器通过文字或图形接口方式来远程登录系统,让你在远程终端前登录linux主机以取得可操 作主机接口(shell),而登录后的操作感觉就像是坐在系统前面一样。 (2)远程连接服务器的功能 分享主机的运算能力 (3)远程连接服务器的类型(以登录的连接界面来分类) 文字接口 明文传输:Telnet、RSH等,目前非常少用 加密传输:SSH为主,已经取代明文传输 图形接口:XDMCP、VNC、XRDP等 (4)文字接口连接服务器

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

2.2连接加密技术简介

目前常见的网络数据包加密技术通常是通过“非对称密钥系统”来处理的。主要通过两把不一样的公钥与 私钥来进行加密与解密的过程 。 公钥(public key):提供给远程主机进行数据加密的行为,所有人都可获得你的公钥来将数据加密。 私钥(private key):远程主机使用你的公钥加密的数据,在本地端就能够使用私钥来进行解密。私钥 只有自己拥有。

2.3 ssh服务配置

#ssh服务安装包openssh-server

[root@server1 ~]# vim /etc/ssh/sshd_config

17.#Port 22 #监听端口,默认监听22端口 【默认可修改】

18.#AddressFamily any #IPV4和IPV6协议家族用哪个,any表示二者均有

19.#ListenAddress 0.0.0.0 #指明监控的地址,0.0.0.0表示本机的所有地址 【默认可修改】

20.#ListenAddress :: #指明监听的IPV6的所有地址格式

28.HostKey /etc/ssh/ssh_host_rsa_key # rsa私钥认证 【默认】

29.#HostKey /etc/ssh/ssh_host_dsa_key # dsa私钥认证

30.HostKey /etc/ssh/ssh_host_ecdsa_key # ecdsa私钥认证

31.HostKey /etc/ssh/ssh_host_ed25519_key # ed25519私钥认证

43.SyslogFacility AUTHPRIV #当有人使用ssh登录系统的时候,SSH会记录信息,信息保存
在/var/log/secure里面

48.#LoginGraceTime 2m #登录的宽限时间,默认2分钟没有输入密码,则自动断开连接

49.#PermitRootLogin yes #是否允许管理员远程登录,'yes'表示允许

51.#MaxAuthTries 6 #最大认证尝试次数,最多可以尝试6次输入密码。之后需要等待某段时间后才能再
次输入密码

52.#MaxSessions 10 #允许的最大会话数

59.AuthorizedKeysFile .ssh/authorized_keys #选择基于密钥验证时,客户端生成一对公私钥之
后,会将公钥放到.ssh/authorizd_keys里面

79.PasswordAuthentication yes #是否允许支持基于口令的认证

83.ChallengeResponseAuthentication no #是否允许使用键盘输入身份验证,也就是xshell的第三
个登录方式

129.#UseDNS yes #是否反解DNS,如果想让客户端连接服务器端快一些,这个可以改为no

146.Subsystem sftp /usr/libexec/openssh/sftp-server #支持 SFTP ,如果注释掉,则不支
持sftp连接

154.AllowUsers user1 user2 #登录白名单(默认没有这个配置,需要自己手动添加),允许远程登录
的用户。如果名单中没有的用户,则提示拒绝登录

2.4用户登录ssh服务器

实验1:修改ssh服务的端口号

#修改ssh服务的端口号

[root@server1 ~]# vim /etc/ssh/sshd_config

Port 2222

[root@server1 ~]# systemctl disable firewalld

[root@server1 ~]# setenforce 0

[root@server1 ~]# getenforce

Permissive
[root@server1 ~]# systemctl restart sshd

实验2:拒绝root用户远程登陆

#请保持服务器上至少有一个可以远程登陆的普通远程账号

#修改配置文件如下:

#打开该选项并修改参数为no

PermitRootLogin no

#重启服务后生效,使用时使用普通用户进行登陆,需要使用root用户时再切换

实验3:允许特定用户ssh登陆,其他用户都无法登陆

#编辑配置文件,在最后添加如下内容:

AllowUsers USERNAME

#USERNAME为你允许登陆的账号【需要在系统中useradd该账号】,如果是多个用户,中间用空格隔开

#重启ssh服务后生效

实验4:linux客户端通过秘钥登录linux服务端root用户

#创建密钥对

[root@web ~]# ssh-keygen -t rsa

ssh-keygen 生成、管理和转换认证密钥 -t制定类型 RSA
/root/.ssh/id_rsa私钥文件

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

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

[root@web ~]# ssh-copy-id root@192.168.220.130
#在本地服务器上登陆对端服务器

[root@web ~]# ssh root@192.168.220.130

三,案例

(1)配置ntp时间服务器,确保客户端主机能和服务主机同步时间

    1)安装chrony

   2)服务器主机从阿里云同步时间---编辑配置文件---重启chrony服务---查看服务器主机是否同步

[root@server ~]# vim /etc/chrony.conf

[root@server ~]# systemctl restart chronyd
[root@server ~]# chronyc sources -v

   3)配置时间服务器服务端---编辑配置文件允许访问的IP,并重启服务

vim /etc/chrony.conf

[root@server ~]# systemctl restart chronyd

   4)配置时间服务器客户端---编辑配置文件---重启服务

[root@server ~]# vim /etc/chrony.conf

 

    5)测试---ping服务端主机---重启服务---查看是否成功

# ping 192.168.220.130
# grep iburst /etc/chrony.conf
# systemctl restart chronyd  
# chronyc sources 查看测试配置成功是否

(2)配置ssh免密登陆,能够通过客户端主机通过redhat用户和服务端主机基于公钥验证方式进行远程连接

  1)创建新用户redhat

[root@client ~]# useradd redhat  创建Redhat用户
[root@client ~]# passwd redhat  修改Redhat密码
Changing password for user redhat.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@client ~]# 

  2)创建新的密钥对,并且发送到指定目录

[redhat@client root]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/redhat/.ssh/id_rsa):      
Created directory '/home/redhat/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/redhat/.ssh/id_rsa.
Your public key has been saved in /home/redhat/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:IxndMy0nurA+2BWvuGRUL+z9ymhRP2TxsBAEzo0gCjc redhat@client
The key's randomart image is:
+---[RSA 3072]----+
|. E . . .oo.     |
| o o . = +..o    |
|  .   . * B.o=   |
|       =.o.*+ .  |
|      = S+.+     |
|     . =o=. o    |
|     o+ooo.  .   |
|    .++ oo .     |
|      o+. o..    |
+----[SHA256]-----+
[redhat@client root]$ 
[redhat@client root]$ ssh-copy-id root@192.168.220.130
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/redhat/.ssh/id_rsa.pub"
The authenticity of host '192.168.220.130 (192.168.220.130)' can't be established.
ECDSA key fingerprint is SHA256:gRgmeI75s84Srl95ggGPz4IbS4OD1ekHo0s8WRjM7wA.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: 
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
The authenticity of host '192.168.220.130 (192.168.220.130)' can't be established.
ECDSA key fingerprint is SHA256:gRgmeI75s84Srl95ggGPz4IbS4OD1ekHo0s8WRjM7wA.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: y
Please type 'yes', 'no' or the fingerprint: yes
/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.220.130's password: 输入密码
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@192.168.220.130'"
and check to make sure that only the key(s) you wanted were added.

  3)客户端测试

[redhat@client root]$ ssh root@192.168.220.130
Activate the web console with: systemctl enable --now cockpit.socket
Register this system with Red Hat Insights: insights-client --register
Create an account or view all your systems at https://red.ht/insights-dashboard
Last login: Mon Apr 17 23:50:13 2023
[root@server ~]# 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值