---
title: "tar 解压造成目录权限发生变更"
author: "itoday"
date: "2013-04-12T23:47:19Z"
tags: []
categories: ["操作系统"]
slug: "tar"
draft: false
lastmod: "2013-04-12T23:47:19Z"
id: 1846685
url: "https://itoday.iteye.com/blog/1846685"
---

一个比较有趣的现象：

在做集群管理的时候偶尔会发现目录权限出现被更改，没人去操作怎么目录属性自己就变了？

首先来做个测试，找出问题原因：

 

可以看到当前目录和压缩文件所属都是root

我们进行解压文件会发现这个情况：

![](http://dl.iteye.com/upload/attachment/0083/0841/6e03b37e-48a6-3d62-b609-3aa800213e2e.png)

解压出来的文件和当前目录权限都变了，其实503和505就是进行文件压缩所在机器，进行操作的user id 和 group id

 

其实这个和解压参数有关系：

使用 tar --no-same-owner -zxf 指定 --no-same-owner可以解决这个问题：

![](http://dl.iteye.com/upload/attachment/0083/0843/b914bd23-15bd-3c7e-a60a-4f7077cc1ae2.png)  
 

从图中可以看到，指定--no-same-owner，目录权限会使用当前操作用户的权限作为文件的所属

 

tar命令在解压时会默认指定参数--same-owner，所以以后解压文件一定要注意这个情况呀