<X>远程登录服务


一、ssh

1.ssh服务的用途

作用:可以实现通过网络在远程主机中开启安全shell的操作
Secure SHell -->ssh #客户端,连接其他人的
Secure SHell daemon -->sshd #服务端,被连接的
安装包:openssh-server
主配置文件:/etc/ssh/sshd_conf
默认端口:22
客户端命令:ssh

2.基本用法

ssh [-l 远程主机用户]<ip|hostname>

3.ssh 服务的 key 认证

建立公钥(key文件)操作
需要两个快照,一个ip是10.4.17.241(客户端),10.4.17.242(服务端)。242建立的密钥(公钥)给(别人)241,然后自己ssh连别人(241)就不需要密码,直接登陆。
在服务端242快照操作:
1,生成密钥的方式两个,一个有交互界面,一个没有,-f文件类型,-P密码,生成的密钥在ls ~/.ssh里查看
[root@localhost ~]# ssh-keygen    # 生成密钥,有交互界面
/root/.ssh/id_rsa,,,回车,回车(密码为空)
[root@localhost ~]# ssh-keygen -f /root/.ssh/id_rsa -P  ""    # 生成密钥,指定地址和密码,无交互界面
2,给客户端主机上锁(上传密钥)
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
3, [root@localhost ~]# ssh [email protected]  #有密钥的不需要密码,可以直接登陆103
4,在客户端242操作:
[root@localhost .ssh]# ls
%完成

在这里插入图片描述

%操作,加密操作
1,在ssh服务器上作:
[root@localhost ~]#  vim /etc/ssh/sshd_config
 17 Port 2222  #改为2222,端口
 [root@localhost ~]# setenforce 0 
[root@localhost ~]# systemctl restart sshd

在客户端操作:
[root@localhost .ssh]# ssh [email protected] #访问不了
[root@localhost .ssh]# ssh [email protected] -p 2222  #能访问-p 2222,指定端口为2222。之后改回22。

2,在ssh服务器上作:
[root@localhost ~]#  vim /etc/ssh/sshd_config
PermitRootLogin no  #改为no,
[root@localhost ~]# systemctl restart sshd
在客户端操作:
[root@localhost ~]# ssh [email protected] #访问不了超级用户
[root@localhost ~]# ssh [email protected]  #能访问其他用户
3,在ssh服务器上作:                                                                  -
PasswordAuthentication no  #改为no
在客户端操作:
[root@localhost ~]# ssh [email protected] #直接拒绝

4,在ssh服务器上作:
DenyUsers lee westos  #增加黑名单,多个黑名单用空格隔开
在客户端操作:
[root@localhost ~]# ssh [email protected]  #不能访问
5,在ssh服务器上作:  
DenyUsers lee westos  #增加白名单,多个白名单用空格隔开。此时只能访问白名单里的用户
[root@localhost ~]# useradd lee
[root@localhost ~]# echo westos | passwd --stdin lee

在客户端操作:
[root@localhost ~]# ssh [email protected] #可以访问

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

二、文件传输

1.实验环境

需要 2 台主机并且保证这两台主机是可以通信的
node1 : 10.4.17.241
node2 : 10.4.17.242:
systemctl disable firewalld
systemctl stop firewalld

2.scp 命令

scp 本地文件 远程主机用户@远程主机 ip:远程主机目录的绝对路径
scp 远程主机用户@远程主机 ip:远程主机文件的绝对路径 本地文件

3.rsync命令

rsync 文件 远程用户@远程主机 ip:远程主机目录
rsync 远程用户@远程主机 ip:远程主机目录 文件路径

rsync指令 作用
-r 复制目录
-l 复制链接
-p 复制权限
-t 复制时间戳
-o 复制拥有者
-g 复制拥有组
-D 复制设备文件
实验步骤:
1,在 node1(接受方) 中
[root@localhost ~]# watch -n 1 ls -lR /root/Desktop
2.在 node2(发送方) 建立实验素材
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
[root@localhost mnt]# touch westosfile{1..3}
[root@localhost mnt]# mkdir westosdir
[root@localhost mnt]# touch westosdir/lee{1..3}  
[root@localhost mnt]# ln -s /mnt/westosfile1 /mnt/westoslink  #把他做成链接
[root@localhost mnt]# chmod 777 * -R
[root@localhost mnt]# chown westos.westos * -R
[root@localhost mnt]# ll
3.在node2中用scp进行传输,相应的在node1 的监测窗口查看传输的文件各属性
a)把本地文件复制到远程主机 (上传)
scp westosfile1 [email protected]:/root/Desktop  #传输文件
scp -r westosdir1 [email protected]:/root/Desktop ## -r 表示复制目录
scp -qr westosdir1 [email protected]:/root/Desktop## -q 传输文件时不显示进度
[root@localhost ~]# time scp -r /boot/grub2 [email protected]:/root/Desktop/
#time 传输文件时显示时间
b)把远程文件复制到本地(下载)
scp [email protected]:/root/Desktop/westos_rhel8/root/Desktop/

4,在node2中用rsync传输,相应的在node1 的监测窗口查看传输的文件各属性
[root@localhost mnt]# rsync -r /mnt/ [email protected]:/root/mnt    #复制目录
[root@localhost mnt]# rsync -lr /mnt/ [email protected]:/root/mnt    #复制链接
[root@localhost mnt]# rsync -lrp /mnt/ [email protected]:/root/mnt   #复制权限
[root@localhost mnt]# rsync -lrpog /mnt/ [email protected]:/root/mnt  #复制拥有者、复制拥有组
[root@localhost mnt]# rsync -lrpogt /mnt/ [email protected]:/root/mnt   #复制时间戳
[root@localhost mnt]# ll /dev/pts/   
[root@localhost mnt]# rsync -Dr /dev/pts/ [email protected]:/root/mnt  #D传输配置文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

三、文件的归档与压缩

1.文件归档

用man tar查看参数

tar指令 作用
c 创建
f 指定文件名称
x 解档
v 显示打包过程
t 查看
r 向归档文件中添加文件
–get 解档指定文件
–delete 删除指定文件
-C 指定解档路径
-P 不把“/”根路径删掉,打包或解压都还会放到原来路径中,不带-P就是当前路径
[root@localhost ~]# tar cf etc.tar /etc/  #tar cf 压缩后的名字 要压缩的文件,压缩后的文件默认放在当前目录下
[root@localhost ~]# tar tf etc.tar  #查看
[root@localhost ~]# tar rf etc.tar westos_rhel8  #向归档文件中添加文件westos_rhel8,但westos_rhel8要事先存在
[root@localhost ~]# tar xf etc.tar
[root@localhost ~]# tar f etc.tar --get westos_rhel8
[root@localhost ~]# tar f etc.tar --delete westos_rhel8
[root@localhost ~]# tar xf etc.tar -C /root/Desktop

在这里插入图片描述

在这里插入图片描述

2.文件的压缩

zip
	[root@localhost mnt]# zip -r etc.tar.zip etc.tar#zip 格式压缩
	[root@localhost mnt]# unzip   etc.tar.zip#zip 格式解压缩
	
gzip
	[root@localhost mnt]# gzip etc.tar #gzip 格式压缩
	[root@localhost mnt]# gunzip etc.tar.gz #gzip 格式压缩
	
[root@localhost mnt]# bzip2   etc.tar   #bzip2 格式压缩
[root@localhost mnt]# bunzip2 etc.tar.bz2 #bzip2 格式压缩
[root@localhost mnt]# xz  etc.tar.xz #xz格式压缩
[root@localhost mnt]# du -sh *  #查看大小

在这里插入图片描述
在这里插入图片描述

3.tar+压缩

gzip
	[root@localhost mnt]# tar zcf etc.tar.gz /etc    #tar+压缩
	[root@localhost mnt]# tar zxf etc.tar.gz      #解档
bzip2
	[root@localhost mnt]# tar jcf etc.tar.bz2 /etc
	[root@localhost mnt]# tar jxf etc.tar.bz2
xz
	[root@localhost mnt]# tar Jcf etc.tar.xz /etc
	[root@localhost mnt]# tar Jxf etc.tar.xz

在这里插入图片描述

四、日志

1.journalctl

默认日志存放路径: /run/log

[root@localhost ~]# journalctl -n 3  ##日志的最新 3 条
[root@localhost ~]# journalctl --since "2020-11-05 20:10:00" ##显示 20:10:00 后的日志
[root@localhost ~]# journalctl --until "2020-11-05 21:10:00"##显示日志到 21:10:00
[root@localhost ~]# journalctl --since "2020-11-05 20:10:00" --until "2020-11-05 20:20:00"
[root@localhost ~]# journalctl -o short     #经典模式显示日志
[root@localhost ~]# journalctl -o verbose #显示日志的全部字节
[root@localhost ~]# journalctl -o export #适合传出和备份的二进制格式
[root@localhost ~]# journalctl -o json #js 格式显示输出

[root@localhost ~]# journalctl -p 0   ## emerg 系统的严重问题日志
[root@localhost ~]# journalctl -p 1 #alert 系统中立即要更改的信息
[root@localhost ~]# journalctl -p 2 #crit 严重级别会导致系统软件不能正常工作
[root@localhost ~]# journalctl -p 3 #error 程序报错
[root@localhost ~]# journalctl -p 4 #warning 程序警告
[root@localhost ~]# journalctl -p 5 #notice 重要信息的普通日志
[root@localhost ~]# journalctl -p 6 #info 普通信息
[root@localhost ~]# journalctl -p 7 #debug 程序拍错信息
[root@localhost ~]# journalctl -F PRIORITY ##查看可控日志级别
[root@localhost ~]# journalctl -u sshd ##指定查看服务
[root@localhost ~]# journalctl --disk-usage ##查看日志大小占用多大的文件系统journ
[root@localhost ~]# journalctl --vacuum-size=1G ##设定日志存放最大为1G 
[root@localhost ~]# journalctl --vacuum-time=1W ##日志在系统中最长存放时间为一周,可以使用设置以上两个参数来设置回滚
[root@localhost ~]# journalctl -f ##监控日志  journalctl -f
	journalctl _PID=10924 _SYSTEMD_UNIT=sshd.service

2.用 journald 服务永久存放日志

服务名称:systemd-journald.service
系统中默认日志在:/run/log/journal 中,默认方式在系统重启后日志会被清理,要永久保存日志请完成以下操作:

[root@localhost ~]# mkdir /var/log/journal
[root@localhost ~]# ls /var/log/journal
[root@localhost ~]# chgrp systemd-journal /var/log/journal
[root@localhost ~]# chmod 2755 /var/log/journal
[root@localhost ~]# ls /var/log/journal
[root@localhost ~]# systemctl restart systemd-journald
[root@localhost ~]# ls /var/log/journal

设置完成后重启系统,会发现重启之前的日志是被保存下来的

在这里插入图片描述

3.自定义日志采集路径


[root@localhost ~]# cat /var/log/messages
[root@localhost ~]# vim /etc/rsyslog.conf
*.*    /var/log/westosall #把系统中所有级别的日志存放到westos 中

[root@localhost ~]# systemctl restart rsyslog.service #重启服务
[root@localhost ~]# vim /etc/rsyslog.conf  
*.*;authprive.none     /var/log/westosall #把系统中所有级别的日志存放到westos 中,##但是 authpriv 不存放到 westos 中
[root@localhost ~]# systemctl restart rsyslog.service #重启服务
[root@localhost ~]# > /var/log/westosall  #晴空

在这里插入图片描述在这里插入图片描述

4.监控其他主机的日志

1,其他主机的操作:
vim /etc/rsyslog.conf 
编辑内容
*.*                                                     @10.4.17.242
19和20行去掉注释
编辑完成
  
systemctl disable --now firewalld   #关掉火墙
systemctl restart rsyslog  #重启服务
2,本主机内的操作:
vim /etc/rsyslog.conf 
编辑内容
19和20行去掉注释
编辑完成
systemctl disable --now firewalld   #关掉火墙
systemctl restart rsyslog  #重启服务
> /var/log/messages
tail -f /var/log/messages   #此时,再10.4.17.242中操作时,10.4.17.241就能时事监控到10.4.17.242的日志

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qiao_qing/article/details/109479499