RHCE第五周(网络客户端)
一:浏览网页和下载
curl和wget和elinks工具
1:curl工具
1:选项
| -o | 将要浏览的网页另存为 |
| -O | 将浏览的网页下载 |
| -i | 查看服务信息以及状态码 |
| -x | 远程代理,要加上端口号,服务的安全性,隐藏了原来的端口号 |
2:案例
1)查看服务的信息以及状态码
状态码:200(能够访问),301表示网址重定向(就是访问这个网页的时候,就会去另外的一个网页),404(资源不存在),403(权限被拒绝)
一般来说,权限被拒绝,应该是selinux或者防火墙的权限,文件的权限这几个方面
[root@server /]# curl -i www.baidu.com | head -20
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 11502 0 --:--:-- --:--:-- --:--:-- 11502
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2381
Content-Type: text/html
Date: Mon, 05 Feb 2024 13:57:02 GMT
Etag: "588604dc-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:56 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
#可以看到这个服务跑在哪个服务上面,也还可以看到状态码200相关的信息
2)代理服务器的操作
3)下载的操作
[root@client opt]# curl -O www.baidu.com/index.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 8503 0 --:--:-- --:--:-- --:--:-- 8503
[root@client opt]# ls
index.html
[root@client opt]# curl -o /opt/html www.baidu.com/index.html
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2381 100 2381 0 0 8689 0 --:--:-- --:--:-- --:--:-- 8689
[root@client opt]# ls
html index.html
[root@client opt]#
#小写的o就是另存为,另存为写在前头
3:网页访问时的日志文件
/var/log/httpd这个就是存放浏览过的相关信息,使用什么命令,或者得到了什么结果
[root@server httpd]# pwd /var/log/httpd [root@server httpd]# cat access_log 192.168.20.10 - - [05/Feb/2024:10:36:23 +0800] "GET / HTTP/1.1" 200 2 "-" "curl/7.76.1" 192.168.20.10 - - [05/Feb/2024:10:36:52 +0800] "GET /index.html HTTP/1.1" 200 2 "-" "curl/7.76.1" 192.168.20.10 - - [05/Feb/2024:10:37:36 +0800] "GET /index.html HTTP/1.1" 200 2 "-" "Wget/1.21.1"
2:wget工具
wget就是下载的工具,非常的便利,专门下载文件的
1:选项
| -O(大写的) | 另存为 |
| -b | 放在后台执行 |
| -P | 指定下载的路径,路径不存在,就创建路径,不能改名 |
| --spider | 测试下载,检测是否存在,有的话则表示可以下载 |
2:案例
1)下载
[root@server opt]# wget -O /opt/qq www.baidu.com/index.html --2024-02-05 22:16:48-- http://www.baidu.com/index.html Resolving www.baidu.com (www.baidu.com)... 36.155.132.76, 36.155.132.3, 2409:8c20:6:1135:0:ff:b027:210c, ... Connecting to www.baidu.com (www.baidu.com)|36.155.132.76|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2381 (2.3K) [text/html] Saving to: ‘/opt/qq’ /opt/qq 100%[=================================================>] 2.33K --.-KB/s in 0.001s 2024-02-05 22:16:48 (1.95 MB/s) - ‘/opt/qq’ saved [2381/2381] [root@server opt]# ls qq [root@server opt]#
2)-P的使用
[root@server opt]# wget -P /opt/22 www.baidu.com/index.html
--2024-02-05 22:19:45-- http://www.baidu.com/index.html
Resolving www.baidu.com (www.baidu.com)... 36.155.132.3, 36.155.132.76, 2409:8c20:6:1d55:0:ff:b09c:7d77, ...
Connecting to www.baidu.com (www.baidu.com)|36.155.132.3|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘/opt/22/index.html’
index.html 100%[=================================================>] 2.33K --.-KB/s in 0.05s
2024-02-05 22:19:46 (44.0 KB/s) - ‘/opt/22/index.html’ saved [2381/2381]
[root@server opt]# ls
22 qq
[root@server opt]# cd 22
[root@server 22]# ls
index.html
[root@server 22]# vim index.html
[root@server 22]# pwd
/opt/22
#会创建一个目录出来用于存放下载的文件
3)测试下载
[root@server 22]# wget --spider www.baidu.com/index.html Spider mode enabled. Check if remote file exists. --2024-02-05 22:22:32-- http://www.baidu.com/index.html Resolving www.baidu.com (www.baidu.com)... 36.155.132.76, 36.155.132.3, 2409:8c20:6:1d55:0:ff:b09c:7d77, ... Connecting to www.baidu.com (www.baidu.com)|36.155.132.76|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2381 (2.3K) [text/html] Remote file exists and could contain further links, but recursion is disabled -- not retrieving. [root@server 22]#
二:远程访问linux系统
通过ssh远程访问linux系统,之前是拿着显示屏,键盘插入到服务器上面,进行管理
现在通过网络远程管理服务器,不需要一对一面对服务器
windows中也能进行ssh的访问
1:ssh协议
ssh协议是一种加密的协议,用于系统之间的通信和远程登陆的,有openssh这个包提供支持ssh协议的实现
提供一下工具,ssh,sshd,scp,sftp,ssh-keygen,ssh-copy-id这几个工具
openssh-client和openssh-server这2个包提供
2:架构
c/s架构:就是客户端和服务器的架构,谁先发起连接谁就是客户端
b/s架构:浏览器和服务器的架构
3:ssh的操作
1)登录远程主机(基于密码)
ssh 用户名@ip地址|主机名 第一次登录需要确认,再来输入密码,下次登录的时候,就不需要确认了
如果是主机名的话,要在本地做好域名解析
[root@server ~]# ssh root@192.168.20.20 The authenticity of host '192.168.20.20 (192.168.20.20)' can't be established. ED25519 key fingerprint is SHA256:jiMr80iLbYa5dT6g+K4OSSebfbmvDASP33XwS2rmvmo. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.20.20' (ED25519) to the list of known hosts. root@192.168.20.20's password: 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: Wed Feb 7 12:11:30 2024 from 192.168.20.10 [root@client ~]#
2)远程执行命令
ssh 用户名@主机名|ip地址 命令操作
[root@server ~]# ssh root@192.168.20.20 touch /opt/11 root@192.168.20.20's password: [root@server ~]#
3)远程打开图形软件,支持转发图形协议
ssh -X 用户名@ip地址|主机名 qq
[root@client ~]# ssh -X root@192.168.20.10 qq --no-sandbox
4:ssh的原理
对称加密和非对称加密
对称加密:加密和解密都使用同一个密钥,将密码发送过去的时候,进行加密,传送到服务器的时候,又来进行解密,这个密码在网络中是以加密后出现的;缺点,在遭受网络攻击的时候容易抓取到这些加密后的密码和密钥对,不安全
非对称加密:加密用公钥,解密用私钥
客户端建立请求的时候,服务端发送公钥到客户端上面(known_host这个文件),然后输入密码被服务端的公钥进行加密传送到服务端上面,服务端利用自己的私钥进行解密,验证密码是否与自己一致性
1:基于密码认证
基于非对称密钥的操作
1)操作
[root@client ~]# ssh root@192.168.20.100 The authenticity of host '192.168.20.100 (192.168.20.100)' can't be established. ED25519 key fingerprint is SHA256:i4CspWRBxeTejyFC+glLzsk3znlHVr0leYC2Y0p7Qf4. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.20.100' (ED25519) to the list of known hosts. root@192.168.20.100's password: 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: Tue Feb 27 21:47:06 2024 from 192.168.20.100 [root@server ~]# logout Connection to 192.168.20.100 closed. [root@client ~]# cd .ssh [root@client .ssh]# ls known_hosts known_hosts.old [root@client .ssh]#
每次登陆的时候都需要输入密码才能远程的登录上面去
2)流程
客户端发送请求,然后服务端发送一个公钥给客户端,客户端输入密码进行公钥加密发送过去,服务器使用私钥解密,然后比对密码,正确,登陆上去
3)总结
其实每次登陆的时候都会验证公钥是否一致,是的话,就能进行下一步,不是的话,就要重新传入了
2:基于密钥认证
也是基于非对称密钥的操作
1)操作
[root@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:XEN8iZ1kZvLetyA9nk8zr9B6IKTb9Q8TQWSMKiqy8tk root@client The key's randomart image is: +---[RSA 3072]----+ | .o+=*+ | | .o**o. | | oo. . | | ...oo.. . | | .S+ ..+o .| | . . . . . =.+o.| | o . o o.=+= | |. .o . . o=o+| | oo E ...+o| +----[SHA256]-----+ [root@client ~]# ssh-copy-id root@192.168.20.100 #将公钥发送给服务器,然后服务器也发送公钥给客户端 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.20.100 (192.168.20.100)' can't be established. ED25519 key fingerprint is SHA256:i4CspWRBxeTejyFC+glLzsk3znlHVr0leYC2Y0p7Qf4. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])? 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.20.100's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.20.100'" and check to make sure that only the key(s) you wanted were added. [root@client ~]# cd .ssh [root@client .ssh]# ls id_rsa id_rsa.pub known_hosts known_hosts.old [root@client .ssh]#
服务端会创建一个authorized_keys这个文件存放客户端的公钥,
如果多个公钥要发送的话,使用-i这个选项指定公钥发送过去就可以了
2)流程
客户端生成密钥对,将自己的公钥发送过去,然后服务端将客户端的公钥随机加密一个随机的字符串发送给客户端,同时,也要发送服务端的公钥给客户端,客户端接收后,使用自己的私钥进行解密,使用服务端的公钥进行加密发送给服务器,服务器使用自己的私钥进行解密,比对是否为那随意的字符串,验证通过
3:总结
1:每次登陆的时候还是会比较客户端的公钥是否一致的,如果不一致,就不能实现免密登录了,就要重新传输客户端的公钥文件
2:客户端想要登录到任意的机器上面去的话,将自己的公钥发送指定的机器上面,使用自己的私钥文件就能登录成功 ssh -i 私钥的路径 用户名@主机名或者ip地址
3:将私钥文件导入出来,百度的解释就是使用私钥加密,公钥解密,就能登录上去了
4:加密的算法可以指定 ssh-keygen -t 加密的算法
4)ssh私钥的建议
1:.ssh这个权限是700尽量不要改动
2:谁用私钥进行登录,私钥的拥有人最好是谁,只有私钥的拥有人拥有读的权限,如果其他也有的话,那就可以直接登录上去了,不安全,8之前的版本,私钥的文件的权限为777的话,会报错权限太大,但是9的话,可以正常的使用
5)ssh私钥代理
代理就是在传输公钥的时候,设置了密码,每次使用私钥的时候,需要输入密码,才能登录上去,安全性很高,使用了代理,就不需要输入解锁私钥的密码了
但是为了临时给别人登录上去的话,就需要设置私钥代理
步骤如下:
1:ssh-agent bash,只在子shell中执行,退出去了就要输入密码
2:ssh-add 如果有多个私钥的话,就输入指定的路径即可
私钥代理总结):
1:不使用私钥代理输入密码的话,他会有一个私钥代理的进程,会存在,下次登录的时候,就不需要输入密码了,把这个进程干掉即可以ps -ef | grep agent ,
2:在子shell中密码保存到/tmp目录下,root的权限都看不了,安全性极高,使用套结字来完成通信的
3:在子shell中将私钥拿走,也是需要输入密码的,安全性高
5:ssh的配置文件
permitrootlogin:控制root能否远程登录,以什么样的方式进行登录
yes:密码和密钥文件都能进行登录
prohbit-password:禁止使用密码进行登录,只能使用密钥文件进行登录
no:密码和密钥文件都不能进行登录
PasswordAuthentication:控制是否使用密码这个选项,默认是允许的
PermitEmptyPasswords:是否允许使用空密码来进行登录,若2个都是yes的话,还要删除用户密码的站位符才行
PubkeyAuthentication:是否允许密钥文件进行登录,默认是允许的
MaxAuthTries:最大连接的连接的次数,超过了,就断开
MaxSessions:最大的会话数
DenyUsers:拒绝某个主机上面的ip登录 *@ip地址
AllowUsers:允许某个主机上面的ip登录 *@ip地址
当二者起冲突的时候,以拒绝的优先级优先
总结:ssh的连接有密码和密钥进行连接,其中密钥的连接用的最普遍
三:文件传输工具
1:scp工具
在windows和linux中都能进行文件传输,但是有个bug,就是在变量文件中有命令或者程序的话,会传输失败
2:scp操作
1)将远程的主机拉取到本地主机上面
[root@server mnt]# scp root@192.168.20.20:/opt/11 . root@192.168.20.20's password: [root@server mnt]# ls 11
2)将本地主机上面拉取到远程主机上面
[root@server mnt]# scp ./11 root@192.168.20.20:/mnt/ root@192.168.20.20's password: 11
3)-r能够传输目录,-p能保存源文件的属性,大写的P就是端口号
[root@server mnt]# scp -rp root@192.168.20.20:/tmp/ ./
2:sftp工具
通过ssh进行ftp的上传和下载,?能看到命令的帮助
常用的选项就是get下载,put上传
sftp 用户名@主机名|ip地址
3:rsync工具
非常好用的工具,可以做文件传输和文件同步
增量同步:就是2个一样的文件,在原文件上面新添加一些内容,在来同步的话,就不是覆盖的同步,而是只添加新的内容,如果是scp的话,那就是覆盖的写入
1:rsync选项和动作
rsync 选项 动作 本地主机 远程主机
| -n | 测试同步的,并不会真正的同步,只检测原路径是否正常 |
| -v | 就是显示详细的信息 |
| -a | 递归的传输,并且保留文件的属性 |
| -D | 传输块设备 |
| -r | 拷贝目录 |
| -z | 传输时进行压缩提高效率 |
| --delete | 多则删之,少则补之,针对的目标端 |
| --existing | 只更新目标端已经存在的文件,不存在的不会更新 |
| --ignore-existing | 只更新目标端不存在的文件 |
| --ignore-existing --delete | 值删除目标端多余的文件 |
| --remove-source-files | 删除原路径,移动到目标端主机上面 |
2: 操作
常见的选项:avz
1)avz的使用
[root@server mnt]# ls a b c [root@server mnt]# rsync -avz /mnt/ 192.168.20.20:/opt/ root@192.168.20.20's password: sending incremental file list ./ a b c sent 217 bytes received 76 bytes 83.71 bytes/sec total size is 0 speedup is 0.00
2)--delete
如果目标端的文件多的话,删除,少的话,添加,对比的是原路径,delete是传输文件方面的
#在目标路径创建文件,多则删之 [root@client opt]# touch d [root@client opt]# ls a b c d [root@server mnt]# rsync -a --delete /mnt/ 192.168.20.20:/opt [root@client opt]# ls a b c #删除目标路径下的文件,少则补之 [root@client opt]# rm -f c [root@client opt]# ls a b [root@server mnt]# rsync -a --delete /mnt/ 192.168.20.20:/opt [root@client opt]# ls a b c
3)--existing
更新目标端已经存在的文件,更新文件的内容
[root@server mnt]# echo mnta > a [root@server mnt]# echo mntb > b [root@server mnt]# echo mntc > c [root@server mnt]# ls a b c [root@client opt]# echo opta > a [root@client opt]# echo optb > b [root@client opt]# echo optc > c [root@client opt]# ls a b c [root@server mnt]# rsync -a --existing /mnt/ 192.168.20.20:/opt [root@client opt]# cat a mnta [root@client opt]# cat b mntb [root@client opt]# cat c mntc
4)--ignore-existing
更新目标端不存在的文件
[root@client opt]# rm -f c [root@client opt]# ls a b [root@server mnt]# rsync -a --ignore-existing /mnt/ 192.168.20.20:/opt [root@client opt]# ls a b c
5)--ignore-existing --delete
只会删除目标端多余的文件
[root@client opt]# touch d [root@client opt]# ls a b c d [root@server mnt]# rsync -a --ignore-existing --delete /mnt/ 192.168.20.20:/opt [root@client opt]# ls a b c
6)--remove-source-files
将原来路径移动到目标路径
[root@client opt]# rm -rf * [root@client opt]# ls [root@server mnt]# ls a b c [root@server mnt]# rsync -a --remove-source-files /mnt/ 192.168.20.20:/opt/ [root@server mnt]# ls [root@server mnt]# [root@client opt]# ls a b c
3:rsync总结
在同步上非常的好用,要对比,对比的是原路径
在备份文件的时候,如果服务器上面有文件误删了,可以以备份的服务器为原路径,服务器上面的文件少了,就添加,选好参照物,就很容易的理解了
常用的操作的--existing,更新目标端已经存在的文件,--delete:多则删之,少则增之
也有从本地到远程,从远程到本地,对象要弄清楚

浙公网安备 33010602011771号