---
title: "hadoop 多用户权限（转）"
author: "DreamOne"
date: "2014-09-03T14:54:49Z"
tags: []
categories: ["互联网"]
slug: "hadoop"
draft: false
lastmod: "2014-09-03T14:54:49Z"
id: 2112429
url: "https://dreamone.iteye.com/blog/2112429"
---

http://blog.sina.com.cn/s/blog\_605f5b4f0101897z.html

 

之前都是用root运行的，但是现在必须要解决这个问题。

 

**hdfs的权限判断十分简单，就是拿发出指令的user name和文件的user name 做比较**

  private void check(INode inode, FsAction access

      ) throws AccessControlException {

    if (inode == null) {

      return;

    }

    FsPermission mode = inode.getFsPermission();

 

    if (user.equals(inode.getUserName())) { //user class

      if (mode.getUserAction().implies(access)) { return; }

    }

    else if (groups.contains(inode.getGroupName())) { //group class

      if (mode.getGroupAction().implies(access)) { return; }

    }

    else { //other class

      if (mode.getOtherAction().implies(access)) { return; }

    }

    throw new AccessControlException("Permission denied: user=" + user

        + ", access=" + access + ", inode=" + inode);

  }

}

 

在多用户提交任务时遇到Permission denied, 的原因和任务提交的过程有关。

1**. 首先提交任务的客户端会把任务相关文件打包放在hadoop.tmp.dir中，这是本地目录，需要通过本地系统权限验证。由于是临时目录直接设置成为777就行.**

2\.  客户端会将**任务文件**打包写入**hdfs的mapreduce.jobtracker.staging.root.dir + "/" + user + "/.staging" 目录，需要经过hdfs权限验证。通常可以选择两种方法解决。**

1\) 保持**mapreduce.jobtracker.staging.root.dir为默认，将此目录chmod 777**

2) **在hdfs的/user目录下建立用户目录，并且chown为该用户，相当于在hdfs下创建一个用户。**

**然后设置mapreduce.jobtracker.staging.root.dir为/user,  这是官方推荐的，可以看到这个属性的描述是这样写的：**

**The root of the staging area for users' job files In practice, this should be the directory where users' home directories are located (usually /user)。**

3）有人说可以在启动任务时加入hadoop.job.ugi属性指定用户和群组。 我验证这样不行。看源码里也是直接获取

ugi = UserGroupInformation.getCurrentUser();不过看hdfs权限策略这操行应该可以设置。

 

还有一些如设置dfs.permissions参数关闭权限校验，改源码直接不校验之类的。。。

 

最后要注意，**任务的out put需要当前用户拥有权限，不然还是不成功。设置成为用户目录就好了**

from:[http://blog.163.com/darkness@yeah/blog/static/131774484201272155937382/](http://blog.163.com/darkness@yeah/blog/static/131774484201272155937382/)

 

另外，关于/tmp目录也要注意：

 ERROR org.apache.pig.impl.io.FileLocalizer - org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: **Permission denied: user=shashaa, access=WRITE, inode="tmp":pwang7:supergroup:rwxr-xr-x**

 **碰到这种情况，当然是使用chmod把tmp目录的权限改为777了。**

 

关于mapreduce.jobtracker.staging.root.dir

Parameter Value Description []()mapred.system.dir /var/mapr/cluster/mapred/jobTracker/system The shared directory where MapReduce stores control files. []()mapred.job.tracker.persist.jobstatus.dir /var/mapr/cluster/mapred/jobTracker/jobsInfo The directory where the job status information is persisted in a file system to be available after it drops of the memory queue and between jobtracker restarts. ***[]()mapreduce.jobtracker.staging.root.dir*** ***/var/mapr/cluster/mapred/jobTracker/staging*** ***The root of the staging area for users' job files In practice, this should be the directory where users' home directories are located (usually /user)***

**启动hadoop hdfs系统的用户即为超级用户，可以进行任意的操作。**

 

如想让leaf用户也可执行hadoop的作业，执行如下操作：

 

**用超级用户hadoop，创建文件    hadoop fs -mkdir /user/leaf**

 

**使用hadoop fs -chown 以及-chgrp 命令改变该文件的用户和用户组权限为leaf**

 

在配置文件hdfs-site-xml 中添加项  dfs.permissions.enabled的值为 true （hadoop0.21版本）

 

要保证leaf这个用户几个节点能ssh无密码相互登录，java环境变量没问题（我们配置时就忽略了这点，幸亏大飞哥的帮助才搞定）

 

这样leaf用户就可以提交任务数据到HDFS的/user/leaf目录中，并且可执行hadoop任务，**注意如果不修改staging，任务仍旧无法提交，虽然这时候可以对hdfs的/user/leaf目录进行操作**