本章内容:scp命令、rsync命令、sftp命令、pssh命令、pslurp命令
大家好,我们又见面了! |
---|
文章目录
1.scp 命令的使⽤
scp: secure copy (remote file copy program)
两种方式:
scp [options] [user@]host:/sourcefile /destpath
scp [options] /sourcefile [user@]host:/destpath
scp 命令功能为 远程文件拷贝。
常用的选项有
-p 保留文件原有权限
-q 静默模式工作
-C 压缩数据流
-P 指定远程端口号
-r 递归复制所有内容
系统发行版 ip地址 主机名
CentOS7 192.168.26.6 node1
CentOS7 192.168.26.7 node2
CentOS7 192.168.26.8 node3
安装了openssh-clients。
查看包
[root@centos6 ~]# rpm -ql openssh-clients
/etc/ssh/ssh_config
/usr/bin/.ssh.hmac
/usr/bin/scp
/usr/bin/sftp
/usr/bin/slogin
/usr/bin/ssh
/usr/bin/ssh-add
/usr/bin/ssh-agent
/usr/bin/ssh-copy-id
/usr/bin/ssh-keyscan
/usr/libexec/openssh/ssh-pkcs11-helper
/usr/share/man/man1/scp.1.gz
/usr/share/man/man1/sftp.1.gz
/usr/share/man/man1/slogin.1.gz
/usr/share/man/man1/ssh-add.1.gz
/usr/share/man/man1/ssh-agent.1.gz
/usr/share/man/man1/ssh-copy-id.1.gz
/usr/share/man/man1/ssh-keyscan.1.gz
/usr/share/man/man1/ssh.1.gz
/usr/share/man/man5/ssh_config.5.gz
将node1服务器上的/etc/sysconfig/network-scripts/下的所有内容 拷贝到node2主机的/root/net⽬录下
[root@centos6 ~]# scp -pr -P 22 /etc/sysconfig/network-scripts/ root@192.168.26.7:/root/net
root@192.168.26.7's password:
ifup-eth 100% 12KB 11.8KB/s 00:00
ifup-isdn 100% 12KB 11.7KB/s 00:00
ifdown 100% 1510 1.5KB/s 00:00
ifup-post 100% 2527 2.5KB/s 00:00
ifup-ippp 100% 12KB 11.7KB/s 00:00
ifdown-ipv6 100% 4168 4.1KB/s 00:00
ifdown-sit 100% 1465 1.4KB/s 00:00
...
以下内容省略
验证,拷贝成功
[root@centos6 ~]# ssh 192.168.26.7
root@192.168.26.7's password:
Last login: Sun Nov 10 10:03:11 2019 from 192.168.26.6
[root@centos7 ~]# ls /root/net/
ifcfg-eth0 ifdown-isdn ifup-eth ifup-routes
ifcfg-eth1 ifdown-post ifup-ib ifup-sit
ifcfg-lo ifdown-ppp ifup-ippp ifup-tunnel
ifdown ifdown-routes ifup-ipv6 ifup-wireless
ifdown-bnep ifdown-sit ifup-isdn init.ipv6-global
ifdown-eth ifdown-tunnel ifup-plip net.hotplug
ifdown-ib ifup ifup-plusb network-functions
ifdown-ippp ifup-aliases ifup-post network-functions-ipv6
ifdown-ipv6 ifup-bnep ifup-ppp
[root@centos7 ~]#
2.rsync命令的使用
#基于ssh和rsh服务实现高效率的远程系统之间复制文件
#使用安全的shell连接做为传输方式
•rsync -av /etc server1:/tmp 复制目录和目录下文件
•rsync -av /etc/ server1:/tmp 只复制目录下文件
比scp更快,只复制不同的文件
常用选项:
-n 模拟复制过程
-v 显示详细过程
-r 递归复制目录树
-p 保留权限
-t 保留时间戳
-g 保留组信息
-o 保留所有者信息
-l 将软链接文件本身进行复制(默认)
-L 将软链接文件指向的文件复制
-a 存档,相当于–rlptgoD,但不保留ACL(-A)和SELinux属性(-X)
rsync 远程同步⽂件程序,需要安装rsync软件包,基本描述信息如下
#安装rsync
[root@centos6 ~]# yum -y install rsync
#查看基本描述信息
[root@centos6 ~]# yum info rsync
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Installed Packages
Name : rsync
Arch : x86_64
Version : 3.0.6
Release : 12.el6
Size : 682 k
Repo : installed
From repo : anaconda-CentOS-201806291108.x86_64
Summary : A program for synchronizing files over a network
URL : http://rsync.samba.org/
License : GPLv3+
Description : Rsync uses a reliable algorithm to bring remote and
: host files into sync very quickly. Rsync is fast
: because it just sends the differences in the files
: over the network instead of sending the complete
: files. Rsync is often used as a very powerful
: mirroring process or just as a more capable
: replacement for the rcp command. A technical report
: which describes the rsync algorithm is included in
: this package.
node1的/etc/sysconfig/network-scripts/下的所有⽂件拷贝到node2的/root/net⽂件夹下
[root@centos6 ~]# rsync -arv /etc/sysconfig/network-scripts/ 192.168.26.7:/root/net1
root@192.168.26.7's password:
sending incremental file list
created directory /root/net1
./
ifcfg-eth0
ifcfg-eth1
ifcfg-lo
...
以下内容省略
验证,拷贝成功
[root@centos6 ~]# ssh 192.168.26.7
root@192.168.26.7's password:
Last login: Sun Nov 10 10:03:36 2019 from 192.168.26.6
[root@centos7 ~]# ls /root/net1
ifcfg-eth0 ifdown-isdn ifup-eth ifup-routes
ifcfg-eth1 ifdown-post ifup-ib ifup-sit
ifcfg-lo ifdown-ppp ifup-ippp ifup-tunnel
ifdown ifdown-routes ifup-ipv6 ifup-wireless
ifdown-bnep ifdown-sit ifup-isdn init.ipv6-global
ifdown-eth ifdown-tunnel ifup-plip net.hotplug
ifdown-ib ifup ifup-plusb network-functions
ifdown-ippp ifup-aliases ifup-post network-functions-ipv6
ifdown-ipv6 ifup-bnep ifup-ppp
现在删除net⽬录下的network-functions和network-functions-ipv6⽂件
然后在node1服务器再次执⾏之前的同步命令
发现他只同步了刚才删除的⽂件
[root@centos6 ]# rm -rf /etc/sysconfig/network-scripts/network-functions-ipv6
[root@centos6 ]# rsync -arv /etc/sysconfig/network-scripts/ 192.168.26.7:/root/net1
root@192.168.26.7's password:
sending incremental file list
./
sent 644 bytes received 15 bytes 101.38 bytes/sec
total size is 126136 speedup is 191.41
现在在对node2中/root/net⽂件中的network-function进⾏清空操作
[root@centos6 ~]# ssh 192.168.26.7
root@192.168.26.7's password:
Last login: Sun Nov 10 10:16:13 2019 from 192.168.26.6
[root@centos7 ~]# cd /root/net
[root@centos7 net1]# echo > network-functions
然后再次在node1中执⾏刚才的同步命令,发现只对network-functions⽂件进⾏同步。
这两个实验操作证明rsync只对有差异的⽂件进⾏同步,效率⽐较⾼。
[root@centos6 ~]# rsync -arv /etc/sysconfig/network-scripts/ 192.168.26.7:/root/net1
root@192.168.26.7's password:
sending incremental file list
network-functions
sent 16047 bytes received 37 bytes 3574.22 bytes/sec
total size is 126136 speedup is 7.84
3.sftp 同样是openssh-clients软件包的⼀个命令,可以⽤ftp的⽅式访问对⽅服务器
常用方式
格式: sftp <host>
通过sftp连接<host>,端口为交替的22,用户为Linux当前登录用户。
格式:sftp -oPort = <port> <host>
通过sftp连接<host>,指定端口<port>,用户为Linux的当前登录用户。
格式:SFTP <用户> @ <主机>
通过SFTP连接<主机>,端口为默认的22,指定用户<用户>。
格式:SFTP -oPort = <端口> <用户> @ <主机>
通过sftp连接<主机>,端口为<端口>,用户为<用户>
交互式文件传输工具
用法和传统的ftp工具相似
利用ssh服务实现安全的文件上传和下载
使用ls cd mkdir rmdir pwd get put等指令,可用?或help获取帮助信息
sftp [user@]host
sftp> help
[root@centos6 data]# sftp 192.168.26.7
Connecting to 192.168.26.7...
root@192.168.26.7's password:
sftp> mget /data/file.txt
Fetching /data/file.txt to file.txt
sftp> exit
[root@centos6 data]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 10 11:18 file.txt
-rw-r--r-- 1 root root 0 Nov 10 11:00 sshkeyau.sh
4.pssh命令
pssh是⼀个python编写的远程执⾏命令⼯具,可在多个机器上同时执⾏名,和复制⽂件。
在node1上使⽤pssh命令获取node2主机的主机名
[root@centos6 data]# pssh -H "192.168.26.7" -A -i hostname
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 11:23:12 [SUCCESS] 192.168.26.7
centos7
在node1上编写主机地址⽂件ip.txt, 同时获取对应主机的hostname ⾸先使⽤node1分别登
录node2和node3,这样就不⽤输⼊验证的yes; 然后执⾏命令 同时获取node2和node3的hostname
[root@centos6 data]# cat ip.txt
192.168.26.7
192.168.26.8
[root@centos6 data]# ssh 192.168.26.7
exit
----------------------------------------
[root@centos6 data]# ssh 192.168.26.8
exit
执⾏命令,输⼊密码,成功获取node2和node3的主机名
[root@centos6 data]# pssh -h ip.txt -A -i hostname
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
[1] 11:42:05 [SUCCESS] 192.168.26.7
centos7
[2] 11:42:05 [SUCCESS] 192.168.26.8
centos8
上述中每次都要输⼊对应主机的密码,⽐较⿇烦,现在设置基于公钥验证,⾸先进⾏免密配置
[root@centos6 data]# ssh-copy-id 192.168.26.7
Now try logging into the machine, with "ssh '192.168.26.7'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
--------------------------------------------------------------------------
[root@centos6 data]# ssh-copy-id 192.168.26.8
Now try logging into the machine, with "ssh '192.168.26.8'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
然后在node1中测试⼀下免密登录node2 和 node3,最后执⾏命令,获取node2和node3主机名成功
[root@centos6 data]# ssh 192.168.26.7
Last login: Sun Nov 10 11:40:46 2019 from 192.168.26.6
[root@centos7 ~]# exit
logout
Connection to 192.168.26.7 closed.
---------------------------------------------------------
[root@centos6 data]# ssh 192.168.26.8
Activate the web console with: systemctl enable --now cockpit.socket
Last failed login: Sun Nov 10 19:40:11 CST 2019 from 192.168.26.6 on ssh:notty
There were 7 failed login attempts since the last successful login.
Last login: Sun Nov 10 17:37:00 2019 from 192.168.26.1
[root@centos8 ~]# exit
[root@centos6 data]#
[root@centos6 data]# pssh -h ip.txt -i hostname
[1] 11:44:34 [SUCCESS] 192.168.26.7
centos7
[2] 11:44:34 [SUCCESS] 192.168.26.8
centos8
在此基础上实现修改node2和node3的selinux配置⽂件功能
⾸先查看node2和node3的selinux 配置⽂件
[root@centos6 data]# grep SELINUX= /etc/selinux/config
# SELINUX= can take one of these three values:
SELINUX=disabled
然后执⾏pssh命令实现统⼀修改SELINUX=disabled
[root@centos6 data]# pssh -h ip.txt 'sed -i "s/^SELINUX=.*/SELINUX=disabled/" /etc/selinux/config'
[1] 11:55:01 [SUCCESS] 192.168.26.8
[2] 11:55:01 [SUCCESS] 192.168.26.7
查看⼀下node2和node3的配置⽂件是否修改成功
[root@centos6 data]# ssh 192.168.26.7
Last login: Sun Nov 10 11:46:44 2019 from 192.168.26.6
[root@centos7 ~]# grep SELINUX= /etc/selinux/config
# SELINUX= can take one of these three values:
SELINUX=disabled
[root@centos7 ~]# exit
logout
Connection to 192.168.26.7 closed.
--------------------------------------------------------------------
[root@centos6 data]# ssh 192.168.26.8
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Sun Nov 10 19:47:02 2019 from 192.168.26.6
[root@centos8 ~]# grep SELINUX= /etc/selinux/config
# SELINUX= can take one of these three values:
SELINUX=disabled
还可以执⾏脚本完成
[root@centos6 data]# cat test.sh
#!/bin/bash
echo $HOSTNAME
---------------------------
chmod +x test.sh
使⽤pscp拷贝到node2和node3的/root/下
[root@centos6 data]# pscp.pssh -h ip.txt test.sh /root/
[1] 12:06:39 [SUCCESS] 192.168.26.8
[2] 12:06:39 [SUCCESS] 192.168.26.7
[root@centos6 data]# pssh -h ip.txt -i /root/test.sh
[1] 12:06:43 [SUCCESS] 192.168.26.7
centos7
[2] 12:06:43 [SUCCESS] 192.168.26.8
centos8
5.使⽤pslurp 命令下载远程主机的⽂件到本地
在node1中执⾏命令,下载node2中/etc/hostname⽂件,到node的/root路径下
并起名为nodename
[root@centos6 ~]# pslurp -H 192.168.26.7 -L /root/ /etc/hostname nodename
[1] 12:09:30 [SUCCESS] 192.168.26.7
[root@centos6 ~]# ll
total 116
drwxr-xr-x 2 root root 4096 Nov 10 12:09 192.168.26.7
-rw-------. 1 root root 1437 Sep 20 20:10 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Desktop
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Documents
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Downloads
-rw-r--r-- 1 root root 883 Nov 9 16:57 id_rsa_1024_node1
-rw-r--r-- 1 root root 224 Nov 9 17:10 id_rsa_1024_node1.pub
-rw-r--r--. 1 root root 50968 Sep 20 20:10 install.log
-rw-r--r--. 1 root root 11504 Sep 20 20:08 install.log.syslog
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Music
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Pictures
-rw-r--r-- 1 root root 0 Nov 10 10:35 profile
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Public
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Templates
drwxr-xr-x. 2 root root 4096 Oct 19 20:38 Videos
[root@centos6 ~]# cd 192.168.26.7/
[root@centos6 192.168.26.7]# ll
total 4
-rw-r--r-- 1 root root 8 Nov 10 12:09 nodename
[root@centos6 192.168.26.7]# cat nodename
centos7
看到这条信息,首先谢谢您😘其次表示本次实验到这就完全结束了,欢迎下次光临!(~ ̄▽ ̄)~ |
---|