Linux——sshd 认证总结、文件在系统中的传输、远程同步、文件的归档rsync工具介绍

3 篇文章 0 订阅
1 篇文章 0 订阅

一、sshd 认证

1.生成ssh-keygen
[root@server2 ~]# cd /root/.ssh
[root@server2 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@server2 .ssh]# rm -rf *
[root@server2 .ssh]# ls
[root@server2 .ssh]# ssh-keygen 	#生成密钥的命令
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #指定保存加密字符的文件(使用默认) 
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:
9e:9b:3b:d8:3a:7a:f1:c6:c1:b0:16:0f:8e:67:f8:0c root@server2
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|      +          |
|     + BS        |
|    E B.+.       |
|     B *o.       |
|      * *o       |
|    .o.++o       |
+-----------------+
2.挂锁
[root@server2 .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.60.2
The authenticity of host '172.25.60.2 (172.25.60.2)' can't be established.
ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65.
Are you sure you want to continue connecting (yes/no)? 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@172.25.60.2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@172.25.60.2'"
and check to make sure that only the key(s) you wanted were added.

[root@server2 .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
     ^
   此文件出现标示加密(挂锁)完成



3.分发钥匙
[root@server2 .ssh]# scp /root/.ssh/id_rsa root@172.25.60.1:/root/.ssh/
The authenticity of host '172.25.60.1 (172.25.60.1)' can't be established.
ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.60.1' (ECDSA) to the list of known hosts.
root@172.25.60.1's password: 
id_rsa                                                                                       100% 1679     1.6KB/s   00:00  
[root@server2 .ssh]# scp /root/.ssh/id_rsa student@172.25.60.1:/home/student/.ssh/
student@172.25.60.1's password: 
id_rsa                                                                                       100% 1679     1.6KB/s   00:00 
4.测试
[root@desktop1 ~]# ssh root@172.25.60.2
Last login: Sat Jan 12 15:16:42 2019 from 172.25.60.1
[root@server2 ~]# 
desktop1免密登陆了server2
[student@desktop1 .ssh]$ ssh root@172.25.60.2
The authenticity of host '172.25.60.2 (172.25.60.2)' can't be established.
ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.60.2' (ECDSA) to the list of known hosts.
Last login: Sat Jan 12 15:22:44 2019 from 172.25.60.1
5.sshd的安全设定

sshd的配置文件为 /etc/ssh/sshd_config,
vim /etc/ssh/sshd_config,修改完后一定要
systemctl restart sshd.service 重启sshd的配置

1.是否允许用户通过登陆系统密码作为sshd的认证
78行 PasswordAuthentication yes/no  #当no时只能用ssh认证登陆,有密码也不行
[root@server2 .ssh]# vim /etc/ssh/sshd_config 
[root@server2 .ssh]# systemctl restart sshd.service 

[root@desktop1 ~]# su - test
Last login: Sat Jan 12 15:32:36 EST 2019 on pts/2
[test@desktop1 ~]$ ssh root@172.25.60.2
The authenticity of host '172.25.60.2 (172.25.60.2)' can't be established.
ECDSA key fingerprint is 65:4d:ac:8a:c9:58:82:b5:0c:91:c4:ef:a5:e6:f6:65.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/home/test/.ssh/known_hosts).
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
2.root用户的家门管理
 48行 PermitRootLogin no/yes  root家门修改处,若为no,则不能以root身份ssh进入,一般都注释起来
[root@desktop1 ~]# ssh root@172.25.60.2
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
#不能以root用户访问172.25.60.2
3.sshd用户的白名单、黑名单 (该用户存在,且该用户有公钥(锁子))
 52行 allowusers username(student)   #sshd用户的白名单,只有名
 单内的用户可以作为家门登陆,其他人(包括root)都不行
[root@desktop1 ~]# ssh student@172.25.60.2
student@172.25.60.2's password: 
[student@server2 ~]$ exit
logout
Connection to 172.25.60.2 closed.
[root@desktop1 ~]# ssh root@172.25.60.2
root@172.25.60.2's password: 
Permission denied, please try again.
root@172.25.60.2's password: 
Permission denied, please try again.
root@172.25.60.2's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
#root用户不在白名单内,被拒绝访问

 同理,也在52行,Denyusers username(黑名单)名单内的禁止以家门访问,关闭家门
修改后在客户端的student无法通过密码连接服务器(88[root@vm138 ~]# su - student
[student@vm138 ~]$ ssh root@172.25.254.88
The authenticity of host '172.25.254.88 (172.25.254.88)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.254.88' (ECDSA) to the list of known hosts.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
#student在黑名单内,被拒绝访问
6.添加sshd登陆信息
[root@server2 .ssh]# vim /etc/motd
[root@server2 .ssh]# cat /etc/motd
Welcome 弟弟~

测试:

[root@desktop1 ~]# ssh root@172.25.60.2
Last login: Sat Jan 12 15:47:22 2019 from 172.25.60.1
Welcome 弟弟~
7.用户登陆审计
w  #查看正在使用当前系统的用户
13:37:03 up  4:27,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0        09:09   ?xdm?  26.97s  0.12s gdm-session-worker [pam/gdm-passwo
root     pts/0     09:09    4:27m  0.02s  0.02s /bin/bash
root     pts/1     13:36    7.00s  0.03s  0.01s w

w -f #查看使用来源
[root@server2 .ssh]# w -f
 15:55:24 up  1:05,  4 users,  load average: 0.01, 0.04, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               14:52   ?xdm?   1:08   0.12s gdm-session-worker [pam/gdm-password]
root     pts/0    :0               14:52   43:00   0.07s  0.07s /bin/bash
root     pts/1    172.25.60.1      15:54   28.00s  0.01s  0.01s -bash
root     pts/2    172.25.60.1      15:16    4.00s  0.07s  0.00s w -f
w -i #显示IP,存储在/var/run/utmp中
[root@server2 .ssh]# w -i
 15:55:45 up  1:05,  4 users,  load average: 0.01, 0.04, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               14:52   ?xdm?   1:08   0.12s gdm-session-worker [pam/gdm-password]
root     pts/0    :0               14:52   43:21   0.07s  0.07s /bin/bash
root     pts/1    172.25.60.1      15:54   49.00s  0.01s  0.01s -bash
root     pts/2    172.25.60.1      15:16    1.00s  0.07s  0.00s w -i
last #查看使用过并退出的用户信息  存储在/var/log/wtmp
[root@server2 .ssh]# last
root     pts/1        172.25.60.1      Sat Jan 12 15:54   still logged in   
root     pts/1        172.25.60.1      Sat Jan 12 15:47 - 15:52  (00:04)    
student  pts/1        172.25.60.1      Sat Jan 12 15:45 - 15:45  (00:00)    
root     pts/1        172.25.60.1      Sat Jan 12 15:32 - 15:32  (00:00)    
root     pts/1        172.25.60.1      Sat Jan 12 15:25 - 15:27  (00:02)    
root     pts/1        172.25.60.1      Sat Jan 12 15:22 - 15:24  (00:01)    
root     pts/2        172.25.60.1      Sat Jan 12 15:16   still logged in   
root     pts/1        172.25.60.250    Sat Jan 12 15:14 - 15:22  (00:07)    
root     pts/1        172.25.60.1      Sat Jan 12 14:59 - 14:59  (00:00)    
root     pts/1        172.25.60.1      Sat Jan 12 14:57 - 14:58  (00:01)    
root     pts/0        :0               Sat Jan 12 14:52   still logged in   
root     :0           :0               Sat Jan 12 14:52   still logged in   
(unknown :0           :0               Sat Jan 12 14:50 - 14:52  (00:01)    
reboot   system boot  3.10.0-123.el7.x Sat Jan 12 14:50 - 15:56  (01:06)    
reboot   system boot  3.10.0-123.el7.x Tue Jan  6 22:20 - 22:38  (00:17)    

wtmp begins Tue Jan  6 22:20:41 2015
lastb  # 试图登陆但没有成功的用户 存储在/var/log/btmp

[root@server2 .ssh]# lastb
root     ssh:notty    172.25.60.1      Sat Jan 12 15:45 - 15:45  (00:00)    
root     ssh:notty    172.25.60.1      Sat Jan 12 15:45 - 15:45  (00:00)    
root     ssh:notty    172.25.60.1      Sat Jan 12 15:45 - 15:45  (00:00)    

btmp begins Sat Jan 12 15:45:31 2019

二、文件在系统中的传输

1.文件的传输(scp)
scp file(存在的) user@ip:/dir  
#上传,从这台电脑传送到另一台,ip为另一台电脑
[root@desktop1 Desktop]# scp /root/Desktop/file root@172.25.60.2:/root/Desktop
file          100%    0     0.0KB/s   00:00    
scp user@ip:/dir|/dir/filename  /dir(存放的位置)
 #下载,从另一台电脑下载到这台电脑,ip为另一台电脑
[root@desktop1 Desktop]# scp root@172.25.60.2:/root/Desktop/Download .
Download      100%    0     0.0KB/s   00:00    

scp上传和下载中,用户(user),ip,文件路径,存放路径都必须存在且正确,用户和路径也必须匹配,不能用student进入/root,否则会报错
报错举例:

[root@desktop mnt]# scp file* root@172.25.254.89:/mnt/
file1                100%    0     0.0KB/s   00:00    
file2                100%    0     0.0KB/s   00:00    
file3                100%    0     0.0KB/s   00:00    
file4                100%    0     0.0KB/s   00:00    
file5                100%    0     0.0KB/s   00:00 


[root@desktop mnt]# scp file* student@172.25.254.89:/root
scp: /root/file1: Permission denied
scp: /root/file2: Permission denied
scp: /root/file3: Permission denied
scp: /root/file4: Permission denied
scp: /root/file5: Permission denied
#student用户进不了/root

[root@desktop mnt]# scp file* root@172.25.254.89:/pppub
scp: /pppub: No such file or directory
#不存在/pppub,无法存放

[root@desktop mnt]# scp fileeeee* root@172.25.254.89:/mnt
fileeeee*: No such file or directory
#不存在fileeeee,无法发送

[root@desktop mnt]# scp file* roottt@172.25.254.89:/mnt
roottt@172.25.254.89's password: 
Permission denied, please try again.
#用户roottt不存在

[root@desktop mnt]# scp file* root@172.25.254.189:/mnt
ssh: connect to host 172.25.254.189 port 22: No route to host
lost connection
#ip172.25.254.189 不存在

[root@desktop mnt]# scp root@172.25.254.89:/mnt/file* .
file1            100%    0     0.0KB/s   00:00    
file2            100%    0     0.0KB/s   00:00    
file3            100%    0     0.0KB/s   00:00    
file4            100%    0     0.0KB/s   00:00    
file5            100%    0     0.0KB/s   00:00    

[root@desktop mnt]# scp root@172.25.254.89:/mnttt/file* linux/
scp: /mnttt/file*: No such file or directory
#上传文件/mnttt/file不存在

[root@desktop mnt]# scp root@172.25.254.89:/mnt/westos linux/
scp: /mnt/westos: not a regular file
#没加-r上传不了目录
2.目录的传输(scp -r)

目录要用scp -r

上传  
#从server2上传到desktop1
[root@server2 Desktop]# ls
Download  file  test
[root@server2 Desktop]# cd test/
[root@server2 test]# ls
file1  file2  file3  file4  file5
[root@server2 test]# cd ..
[root@server2 Desktop]# scp -r test/ root@172.25.60.1:/root/Desktop/
root@172.25.60.1's password: 
file1                                                                                        100%    0     0.0KB/s   00:00    
file2                                                                                        100%    0     0.0KB/s   00:00    
file3                                                                                        100%    0     0.0KB/s   00:00    
file4                                                                                        100%    0     0.0KB/s   00:00    
file5                                                                                        100%    0     0.0KB/s   00:00    

[root@desktop1 Desktop]# ls
Download  file  test
[root@desktop1 Desktop]# ls test/
file1  file2  file3  file4  file5  #上传成功
下载
[root@server2 Desktop]# mkdir test1
[root@server2 Desktop]# cd test1/
[root@server2 test1]# touch file{1..9}
[root@server2 test1]# ls
file1  file2  file3  file4  file5  file6  file7  file8  file9
#从server2下载到desktop1
[root@desktop1 Desktop]# scp -r root@172.25.60.2:/root/Desktop/test1/ .
file1       100%    0     0.0KB/s   00:00    
file2       100%    0     0.0KB/s   00:00    
file3       100%    0     0.0KB/s   00:00    
file4       100%    0     0.0KB/s   00:00    
file5       100%    0     0.0KB/s   00:00    
file6       100%    0     0.0KB/s   00:00    
file7       100%    0     0.0KB/s   00:00    
file8       100%    0     0.0KB/s   00:00    
file9       100%    0     0.0KB/s   00:00    
[root@desktop1 Desktop]# ls
Download  file  redhat  test  test1
[root@desktop1 Desktop]# ls test1/
file1  file2  file3  file4  file5  file6  file7  file8  file9

三、远程同步

rsync #远程同步 (速度快,默认会忽略文件属性 链接文件 设备文件)
 -r #同步目录
 -p #同步权限
 -o #同步文件的拥有者
 -g #同步文件的所属组
 -l #同步链接文件
 -t #同步文件的时间戳
 -D #同步设备文件
[root@desktop1 Desktop]# ln -s file Linux  #Linux链接向file
[root@desktop1 Desktop]# ll
total 4
-rw-r--r--. 1 root root    0 Jan 12 16:05 Download
-rwxrwxrwx. 1 root root    0 Jan 12 15:59 file
lrwxrwxrwx. 1 root root    4 Jan 12 17:03 Linux -> file 
#添加链接成功,相当于变量的引用,两个的内存地址是一块,系统只记录一次
drwxr-xr-x. 2 root root    6 Jan 12 17:03 redhat
drwxr-xr-x. 2 root root   66 Jan 12 16:15 test
drwxr-xr-x. 2 root root 4096 Jan 12 16:19 test1

例1:从desktop1远程传输file到server1并同步权限

[root@desktop1 Desktop]# chmod 777 file
[root@desktop1 Desktop]# ll
total 8
-rw-r--r--. 1 root root    0 Jan 12 16:05 Download
-rwxrwxrwx. 1 root root    0 Jan 12 15:59 file
drwxr-xr-x. 2 root root 4096 Jan 12 16:17 redhat
drwxr-xr-x. 2 root root   66 Jan 12 16:15 test
drwxr-xr-x. 2 root root 4096 Jan 12 16:19 test1
[root@desktop1 Desktop]# rsync /root/Desktop/file root@172.25.60.2:/root/Desktop/

server查看权限
[root@server2 Desktop]# ll
total 4
-rw-r--r--. 1 root root    0 Jan 12 16:05 Download
-rw-r--r--. 1 root root    0 Jan 12 16:00 file
#此时只用了rsync,没有-p同步权限


[root@desktop1 Desktop]# rsync -p /root/Desktop/file root@172.25.60.2:/root/Desktop/
server查看权限
[root@server2 Desktop]# ll
total 4
-rw-r--r--. 1 root root    0 Jan 12 16:05 Download
-rwxrwxrwx. 1 root root    0 Jan 12 16:59 file
#同步权限成功
drwxr-xr-x. 2 root root   66 Jan 12 16:13 test
drwxr-xr-x. 2 root root 4096 Jan 12 16:18 test1

例子2:从desktop1远程传输test文件夹到server1,并同步传输拥有者和所属组

[root@desktop1 Desktop]# cd test/
[root@desktop1 test]# ls
file1  file2  file3  file4  file5
[root@desktop1 test]# ll
total 0
-rw-r--r--. 1 student student 0 Jan 12 16:15 file1
-rw-r--r--. 1 student student 0 Jan 12 16:15 file2
-rw-r--r--. 1 student student 0 Jan 12 16:15 file3
-rw-r--r--. 1 student student 0 Jan 12 16:15 file4
-rw-r--r--. 1 student student 0 Jan 12 16:15 file5

[root@desktop1 test]# rsync -rog /root/Desktop/test/ root@172.25.60.2:/root/Desktop/  
#有/传输test目录下的文件


[root@server2 Desktop]# ll
total 0
-rw-r--r--. 1 student student 0 Jan 12 17:08 file1
-rw-r--r--. 1 student student 0 Jan 12 17:08 file2
-rw-r--r--. 1 student student 0 Jan 12 17:08 file3
-rw-r--r--. 1 student student 0 Jan 12 17:08 file4
-rw-r--r--. 1 student student 0 Jan 12 17:08 file5

tips:

[root@desktop1 test]# rsync -rog /root/Desktop/test/ root@172.25.60.2:/root/Desktop/  
#有/传输test目录下的文件
[root@desktop1 test]# rsync -rog /root/Desktop/test root@172.25.60.2:/root/Desktop/  
#没有/传输test目录

四、文件的归档

定义:就是把多个文件变成一个归档文件
目的:加快数据的传输速度

1.tar (打包)
c 		 ---> 建立一个压缩文件的参数指令(create :创建)
f 		 ---> 指定归档文件名称
x 		 ---> 解开
t		 ---> 查看
r		 ---> 向归档文件中添加文件/目录  (tip:向归档文件中添加目录,会添加目录本身和目录底下的文件)
--delete ---> 删除归档文件中的单个文件或目录
--get 	 ---> 在归档文件中取出单个文件或目录
-C 		 --->指定解档文件的存储路径
tar xf etc.tar -C /mnt  #指定解开etc.tar的存储路径为/mnt
#各选项要组合起来用
tar cf 指定归档文件名称  源文件
c ---> 建立一个压缩文件的参数指令(create :创建)
f ---> 指定归档文件名称
[root@server2 Desktop]# cp -r /etc .  		#拷贝/etc到当前目录
[root@server2 Desktop]# ls
etc
[root@server2 Desktop]# du -sh etc/  		#查看文件大小
34M	etc/
[root@server2 Desktop]# tar cf etc.tar etc/ #打包etc/ 为etc.tar
[root@server2 Desktop]# ls
etc  etc.tar
[root@server2 Desktop]# du -sh etc.tar
30M	etc.tar
[root@server2 Desktop]# scp /root/Desktop/etc.tar root@172.25.60.1:/root/Desktop/   
root@172.25.60.1's password: 
etc.tar      100%   30MB  29.8MB/s   00:00 
#把etc.tar从server2上传到desktop1中

desktop中解开一个归档文件的指令参数
tar xf etc.tar #解开etc.tar

[root@desktop1 Desktop]# ls
etc.tar
[root@desktop1 Desktop]# tar xf etc.tar  #解开
[root@desktop1 Desktop]# ls
etc  etc.tar
#解开etc.tar生成etc  

[root@desktop1 Desktop]# tar tf etc.tar #查看归档文件中的内容

etc/at.deny
etc/nanorc
etc/wgetrc
etc/updatedb.conf
etc/hostname 
#etc.tar中末尾的内容

[root@desktop1 Desktop]# touch file
[root@desktop1 Desktop]# tar rf etc.tar file   
#etc.tar中添加了file

etc/at.deny
etc/nanorc
etc/wgetrc
etc/updatedb.conf
etc/hostname
file
#etc.tar中末尾的内容多了file
[root@desktop1 Desktop]# mkdir test
[root@desktop1 Desktop]# cd test/
[root@desktop1 test]# touch test1
[root@desktop1 test]# touch test2
[root@desktop1 test]# ls
test1  test2
[root@desktop1 test]# cd ..
[root@desktop1 Desktop]# tar rf etc.tar test/  
#会把test/ 以及该目录苦下的  test/test1   test/test2  
三个都添加进入etc.tar
[root@desktop1 Desktop]# tar f etc.tar --delete test/		
#会把test/ 以及该目录苦下的  test/test1   test/test2  
三个都从etc.tar删除

[root@desktop1 Desktop]# tar f etc.tar --get test/test1   
#从etc.tar中单独取出test/test1,没有取test/test2

[root@desktop1 Desktop]# tar f etc.tar --get test   
#从etc.tar中取出test/目录,test/test1和test/test2也会一起取出

[root@desktop1 Desktop]# ls
etc  etc.tar  file  test
[root@desktop1 Desktop]# cd test/
[root@desktop1 test]# ls
test1  test2

例:

[root@server87 Desktop]# cp -r /etc/ .
[root@server87 Desktop]# ls
etc
[root@server87 Desktop]# tar cf etc.tar etc #etc打包成 etc.tar
[root@server87 Desktop]# ls
etc  etc.tar
[root@server87 Desktop]# rm -rf etc
[root@server87 Desktop]# ls
etc.tar
[root@server87 Desktop]# tar xf etc.tar 	#etc.tar 解开成etc
[root@server87 Desktop]# ls
etc  etc.tar

[root@server87 Desktop]# touch file
[root@server87 Desktop]# tar rf etc.tar file #向etc.tar添加文件file

[root@server87 Desktop]# mkdir test
[root@server87 Desktop]# ls
etc  etc.tar  file  test
[root@server87 Desktop]# tar rf etc.tar test 
			#会把test和test目录下的file1 file2都添加进etc.tar

[root@server87 Desktop]# tar f etc.tar --delete file 
			#删除file文件
[root@server87 Desktop]# tar f etc.tar --delete test 
			#删除test和test目录下的file1 file2

[root@server87 Desktop]# ls
etc  etc.tar  file  test
[root@server87 Desktop]# rm -rf test
[root@server87 Desktop]# tar f etc.tar --get test/   
			#得到test目录
[root@server87 Desktop]# ls
etc  etc.tar  file  test

2.压缩

压缩是一种通过特定的算法来减少计算机文件大小的机制
使文件能够通过较慢的互联网实现更快的传输
还可以减少文件的磁盘占用空间

注意:zip这种格式的压缩方式稳定性很差,只适合压缩纯文本文件
1).zip (会生成新文件 .zip后缀)
zip:
[root@server87 Desktop]# du -sh etc.tar
30M	etc.tar
[root@server87 Desktop]# ls
etc  etc.tar
[root@server87 Desktop]# zip -r etc.tar.zip etc.tar  
	#压缩生成etc.tar.zip
  adding: etc.tar (deflated 72%)
[root@server87 Desktop]# ls
etc  etc.tar  etc.tar.zip
[root@server87 Desktop]# du -sh etc.tar.zip
8.4M	etc.tar.zip

[root@server87 Desktop]# ls
etc  etc.tar  etc.tar.zip
[root@server87 Desktop]# rm -rf etc.tar
[root@server87 Desktop]# ls
etc  etc.tar.zip
[root@server87 Desktop]# unzip etc.tar.zip  
	#解压etc.tar.zip生成etc.tar
Archive:  etc.tar.zip
  inflating: etc.tar                 
[root@server87 Desktop]# ls
etc  etc.tar  etc.tar.zip

2).gzip(不会生成新文件,.gz后缀)
[root@server87 Desktop]# ls
etc  etc.tar  
[root@server87 Desktop]# gzip etc.tar 
	#在etc.tar基础上生成etc.tar.gz
[root@server87 Desktop]# ls
etc  etc.tar.gz  

[root@server87 Desktop]# gunzip etc.tar.gz  
	#解压etc.tar.gz生成etc.tar
[root@server87 Desktop]# ls
etc  etc.tar 

3).xzip (不会重新生成文件 .xz后缀)
[root@server87 Desktop]# ls
etc  etc.tar  
[root@server87 Desktop]# xz etc.tar  
	#在etc.tar基础上生成etc.tar.xz
[root@server87 Desktop]# ls
etc  etc.tar.xz  
[root@server87 Desktop]# unxz etc.tar.xz	
	#解压etc.tar.xz生成etc.tar
[root@server87 Desktop]# ls
etc  etc.tar  
4).bzip2 (只适合压缩纯文本文件,不会生成新文件)

bunzip2 解压后仍然还原成以前的归档文件

[root@server87 Desktop]# ls
etc  etc.tar  
[root@server87 Desktop]# bzip2 etc.tar	
	#bzip2压缩,在etc.tar基础上生成etc.tar.bz2
[root@server87 Desktop]# ls
etc  etc.tar.bz2  
[root@server87 Desktop]# bunzip2 etc.tar.bz2 
	#解压etc.tar.bz2
[root@server87 Desktop]# ls
etc  etc.tar  
3.打包压缩一步完成
1).gzip
tar zcf  指定名称必须以.gz结尾  源文件  #打包并添加成gz压缩
tar zxf	 .gz文件					  ##解开打包并解开压缩
[root@server87 Desktop]# ls
etc
[root@server87 Desktop]# tar -zcf etc.tar.gz etc/  
#打包并添加成gz压缩
[root@server87 Desktop]# ls
etc  etc.tar.gz

[root@server87 Desktop]# ls
etc.tar.gz
[root@server87 Desktop]# tar zxf etc.tar.gz   
#解开打包并解开压缩
[root@server87 Desktop]# ls
etc  etc.tar.gz

2) xz (大写J)
tar Jcf etc.tar.xz etc/  #大写Jcf,打包、xz压缩etc/
tar Jxf etc.tar.xz  	 #大写Jcf,解开打包、解压xz 
[root@server87 Desktop]# ls
etc
[root@server87 Desktop]# tar Jcf etc.tar.xz etc/  #大写Jcf
[root@server87 Desktop]# ls
etc  etc.tar.xz
[root@server87 Desktop]# du -sh etc.tar.xz
5.7M	etc.tar.xz
[root@server87 Desktop]# rm -rf etc/
[root@server87 Desktop]# ls
etc.tar.xz
[root@server87 Desktop]# tar Jxf etc.tar.xz  #大写Jcf
[root@server87 Desktop]# ls
etc  etc.tar.xz

3) bz2 (小写的j)
tar jcf etc.tar.bz2 etc/	#打包bz2压缩
tar jxf etc.tar.bz2 		#解开打包解开bz2压缩
[root@server87 Desktop]# ls
etc
[root@server87 Desktop]# tar jcf etc.tar.bz2 etc/
[root@server87 Desktop]# ls
etc  etc.tar.bz2
[root@server87 Desktop]# du -sh etc.tar.bz2
7.0M	etc.tar.bz2
[root@server87 Desktop]# rm -rf etc/
[root@server87 Desktop]# tar jxf etc.tar.bz2 #解开打包解开压缩
[root@server87 Desktop]# ls
etc  etc.tar.bz2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值