一、常见的服务管理方式

  • SSH:SSH(Secure Shell)是一种安全的远程登录协议,可以通过SSH协议在不安全的网络中安全地传输数据。
  • Telnet:是一种远程登录协议,可以通过Telnet协议在网络中传输数据。Telnet是一种不加密的协议,数据传输是明文的,安全性较差。
  • VNC:是一种远程桌面协议,可以在不同计算机之间共享屏幕和控制计算机。

二、SSH服务

安装:yum install openssh openssh-clients openssh-server -y

软件安装生产的文件:rpm -ql openssh

OpenSSH配置文件

OpenSSH常用配置文件有两个/etc/ssh/ssh_config和/etc/sshd_config

ssh_config为客户端配置文件

sshd_config为服务器端配置文件

sshd服务配置和管理

配置文件:/etc/ssh/sshd_config

Port 22  设置sshd监听端口号

SSH 预设使用22这个port,也可以使用多个port,即重复使用 port 这个设定项目

ssh调优

**打开/etc/ssh/sshd_config**

```ABAP
LoginGraceTime 2m    #grace 优雅

当使用者连上 SSH server 之后,会出现输入密码的画面,在该画面中, # 在多久时间内没有成功连上 SSH server 就强迫断线!若无单位则默认时间为秒! 可以根据实际情况来修改实际

# PermitRootLogin yes

#是否允许 root 登入!预设是允许的,但是建议设定成 no !

真实的生产环境服务器,是不允许root账号登陆的!!!

PasswordAuthentication yes 

#密码验证当然是需要的!所以这里写 yes,也可以设置为no

#在真实的生产服务器上,根据不同安全级别要求,有的是设置不需要密码登陆的,通过认证的秘钥来登陆

PermitEmptyPasswords no 

若上面那一项如果设定为 yes 的话,这一项就最好设定为 no ,这个项目在是否允许以空的密码登入!当然不许!

PrintMotd yes 

登入后是否显示出一些信息呢?例如上次登入的时间、地点等等,预设是 yes ,亦即是打印出 /etc/motd这个文档的内容。 给sshd服务添加一些警告信息

 echo 'Warning ! From now on, all of your operation has been record!'> /etc/motd
PrintLastLog yes   # 显示上次登入的信息!预设也是 yes

SSH远程连接

ssh客户端配置文件:`/etc/ssh/ssh_config`;SSH首次连接服务端,需要确认接收服务端公钥文件,可以配置文件中设置默认自动接收公钥文件

[root@ssh-client ~ ]# cat /etc/ssh/ssh_config
#   StrictHostKeyChecking ask

# 修改
sed -i.bak '/StrictHostKeyChecking/s/.*/StrictHostKeyChecking no/' /etc/ssh/ssh_config

SSH远程连接

格式:
    ssh <user>@<host_IP> <command>

user:指定连接登录服务端的用户,不指定则以客户端当前用户连接
host_IP: 服务端IP地址
command:连接SSH服务端执行命令,不指定默认开启连接终端


范例 : ssh cat@192.168.1.100
参数说明
-p 指定连接服务端SSH端口
-b客户端可能有多块网卡,指定哪块连接服务端
-v调试模式
-c压缩方式
-XLinux系统之间X11转发
-t临时分配伪终端,
-o 临时设置ssh连接参数,覆盖配置文件设置; 如:ssh -o stricthostkeychecking=no 10.0.0.8
-i 指定私钥文件路径,实现基于key验证

图形化连接

#安装Linux系统上图形化工具,如:firefox浏览器
yum install mesa-libGLU.x86_64 firefox -y

#指定图形化界面运行目标服务端地址, 10.0.0.2为Windows的net8地址
export DISPLAY=10.0.0.66:0.0

#运行firefox浏览器
firefox

图形化创建用户

yum install -y system-config-users
export DISPLAY=10.0.0.66:0.0
system-config-users

MobaXterm 工具实现X11连接

yum install xorg-x11-xauth xorg-x11-fonts-* xorg-x11-font-utils xorg-x11-fonts-Type1 -y
exit
firefox

远程主机运行本地shell脚本

[root@centos7 ~ ]# cat IP.sh
#!/bin/bash
hostname -I
[root@centos7 ~ ]# ssh -o stricthostkeychecking=no 10.0.0.8 bash < IP.sh
root@10.0.0.8's password:
10.0.0.8
    
[root@centos7 ~ ]# ssh -o stricthostkeychecking=no 10.0.0.8  < IP.sh
root@10.0.0.8's password:
Last failed login: Thu Jul  8 17:26:06 CST 2021 from 10.0.0.17 on ssh:notty
There were 3 failed login attempts since the last successful login.
10.0.0.8

基于密钥登录

# 在客户端创建密钥对
ssh-keygen -t <rsa 秘钥算法> [-p <秘钥加密口令>] [ -f ~/.ssh/id_rsa 指定秘钥存放位置]

# 把创建秘钥对的公钥传输给服务端对应登录用户的家目录下 ~/.ssh/authorized_keys 文件,向服务端上传公钥文件时也需要输入登录用户密码,再次登录基于秘钥对登录目标主机
ssh-copy-id [-i [identity_file]] [user@]host
    
# 已创建密钥对设置口令
ssh-genken -p

实现批量基于ssh的key部署

[root@ssh-client ~ ]# cat push_ssh_key.sh
#!/bin/bash
PASS=1
rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null
rm -rf .ssh/id_rsa .ssh/id_rsa.pub
ssh-keygen  -t rsa -P "" -f /root/.ssh/id_rsa &> /dev/null && echo "ssh key is created"
host="10.0.0.8 10.0.0.18 10.0.0.28"
for IP in $host ;do
        {
        sshpass -p$PASS ssh-copy-id -i /root/.ssh/id_rsa.pub root@$IP -o StrictHostKeyChecking=no &>/dev/null
        echo $IP is ready
        }&
done
wait

[root@ssh-client ~ ]# bash push_ssh_key.sh
ssh key is created
10.0.0.6 is ready
10.0.0.8 is ready

SCP命令

scp [参数] [user@]host:/sourcefile /destpath

/sourcefile #本地文件路径
/destpath	#远程主机路径
    
**把远程目标主机下载到本地**

scp [参数] /destpath [user@]host:/sourcefile 
参数说明
-c压缩传输
-r递归拷贝目录下所有数据
-p保留拷贝数据文件属性
-q静默执行
-P 指定ssh服务端口

rsync命令

rsync  -av /etc 10.0.0.8:/tmp 	#复制目录和目录下文件
rsync  -av /etc/ 10.0.0.8:/tmp 	#只复制目录下文件
    
# 命令参数
    -n 模拟复制过程
    -v 显示详细过程
    -r 递归复制目录树
    -p 保留权限
    -t 保留修改时间戳
    -g 保留组信息
    -o 保留所有者信息
    -l 将软链接文件本身进行复制(默认)
    -L 将软链接文件指向的文件复制
    -u 如果接收者的文件比发送者的文件较新,将忽略同步
    -z 压缩,节约网络带宽
    -a 存档,相当于–rlptgoD,但不保留ACL(-A)和SELinux属性(-X)
    --delete 源数据删除,目标数据也自动同步删除
    
# 拷贝本地文件
rsync -p /etc/hosts /data/
rsync -r /etc/ssh /data

# 远程备份文件
rsync -a /etc/hostname 10.0.0.7:/data
rsync -a 10.0.0.7:/etc/hostname /data/
    
# 远程查看目录属性信息
rsync 10.0.0.7:/data/

# 远程删除目录文件
rsync -av --delete /data/ 10.0.0.7:/tmp

sftp命令

[root@centos7 ~ ]# sftp 10.0.0.7
Connected to 10.0.0.7.
#查看远程连接的主机当前目录下文件
sftp> ls
hosts.txt        push_ssh_key.sh  self-signed.crt  self-signed.pem  test.pem
#查看本地主机当前目录下文件
sftp> !ls
anaconda-ks.cfg  IP.sh	reset.sh