All Products
Search
Document Center

E-MapReduce:Hadoop Auth Server Kerberos authentication

Last Updated:Jul 17, 2026

Use Hadoop Auth Server (HAS) Kerberos authentication to access HDFS from a gateway node or Java application.

Prerequisites

You have created a Hadoop cluster that runs EMR-3.40 or earlier, or EMR-4.10.1 or earlier. For more information, see Create a cluster.

Access HDFS using the hadoop command

The following steps use test as an example user.

  1. Configure the krb5.conf file on the gateway node.
    scp root@emr-header-1:/etc/krb5.conf /etc/
  2. Set the hadoop.security.authentication.use.has parameter to false.
    1. Log on to the emr-header-1 node of the cluster.
      Note For an HA cluster, you must also log on to the emr-header-1 node.

      For more information, see Log on to a cluster.

    2. Run the following command to edit the core-site.xml file.
      vim /etc/ecm/hadoop-conf/core-site.xml
    3. Check the value of hadoop.security.authentication.use.has.
      • If the value is true, change the value of hadoop.security.authentication.use.has to false.
      • If the value is false, proceed to Step 3.
  3. Add a principal.
    1. Run the following command to start the Kerberos administration tool.
      • For EMR-3.30.0 or later and EMR-4.5.1 or later:
        sh /usr/lib/has-current/bin/admin-local.sh /etc/ecm/has-conf -k /etc/ecm/has-conf/admin.keytab
      • For versions earlier than EMR-3.30.0 and EMR-4.5.1:
        sh /usr/lib/has-current/bin/hadmin-local.sh /etc/ecm/has-conf -k /etc/ecm/has-conf/admin.keytab
    2. Run the following command to add the test principal.

      In this example, the password is set to 123456.

      addprinc -pw 123456 test
    3. Run the following command to export the keytab file.
      ktadd -k /root/test.keytab test
  4. Obtain a ticket.
    Run the following commands on the client where you run HDFS commands. A gateway node is used in this example.
    1. Create a Linux account named test.
      useradd test
    2. Install the MIT Kerberos client tools.

      MIT Kerberos provides commands such as kinit and klist. For more information, see MIT Kerberos.

      yum install krb5-libs krb5-workstation -y
    3. Switch to the test account and run kinit.
      su test
      • If you do not have a keytab file, run kinit and enter the password 123456 when prompted.
      • If you have a keytab file, run the following commands.
        # Authenticate with the specified principal in the specified keytab file.
        kinit -kt test.keytab test 
        # View the ticket lifetime.
        klist
        Run klist. Sample output:
        Valid starting       Expires              Service principal
        03/30/2021 10:48:47  03/31/2021 10:48:47  krbtgt/EMR.209749.COM@EMR.209749.COM
                renew until 03/31/2021 10:48:47
    4. Optional: To set the ticket lifetime:
      1. Set the ticket lifetime.
        kinit -l 5d
      2. Run the klist command to view the ticket lifetime.
        Valid starting       Expires              Service principal
        03/30/2021 10:50:51  04/04/2021 10:50:51  krbtgt/EMR.209749.COM@EMR.209749.COM
                renew until 04/01/2021 10:50:51
  5. Run the following command on the gateway node to set the environment variable.
    export HADOOP_CONF_DIR=/etc/has/hadoop-conf
  6. Run an HDFS command.
    hadoop fs -ls /
    Sample output:
    Found 6 items
    drwxr-xr-x   - hadoop    hadoop          0 2021-03-29 11:16 /apps
    drwxrwxrwx   - flowagent hadoop          0 2021-03-29 11:18 /emr-flow
    drwxr-x---   - has       hadoop          0 2021-03-29 11:16 /emr-sparksql-udf
    drwxrwxrwt   - hadoop    hadoop          0 2021-03-29 11:17 /spark-history
    drwxr-x---   - hadoop    hadoop          0 2021-03-29 11:16 /tmp
    drwxrwxrwt   - hadoop    hadoop          0 2021-03-29 11:17 /user

Access HDFS using Java code

  • Use a local ticket cache
    Note You must run kinit beforehand to obtain a ticket. After the ticket expires, the application can no longer access HDFS.
    public static void main(String[] args) throws IOException {
       Configuration conf = new Configuration();
       // Load HDFS configurations. You need to copy the HDFS configurations from the EMR cluster.
       conf.addResource(new Path("/etc/ecm/hadoop-conf/hdfs-site.xml"));
       conf.addResource(new Path("/etc/ecm/hadoop-conf/core-site.xml"));
       // You must run kinit beforehand under the Linux account to obtain a ticket.
       UserGroupInformation.setConfiguration(conf);
       UserGroupInformation.loginUserFromSubject(null);
       FileSystem fs = FileSystem.get(conf);
       FileStatus[] fsStatus = fs.listStatus(new Path("/"));
       for(int i = 0; i < fsStatus.length; i++){
           System.out.println(fsStatus[i].getPath().toString());
       }
    }
  • Use a keytab file (Recommended)
    Note A keytab file is valid long term and does not depend on the local ticket.
    public static void main(String[] args) throws IOException {
      String keytab = args[0];
      String principal = args[1];
      Configuration conf = new Configuration();
      // Load HDFS configurations. You need to copy the HDFS configurations from the EMR cluster.
      conf.addResource(new Path("/etc/ecm/hadoop-conf/hdfs-site.xml"));
      conf.addResource(new Path("/etc/ecm/hadoop-conf/core-site.xml"));
      // Use the keytab file obtained from the emr-header-1 node of the EMR cluster.
      UserGroupInformation.setConfiguration(conf);
      UserGroupInformation.loginUserFromKeytab(principal, keytab);
      FileSystem fs = FileSystem.get(conf);
      FileStatus[] fsStatus = fs.listStatus(new Path("/"));
      for(int i = 0; i < fsStatus.length; i++){
          System.out.println(fsStatus[i].getPath().toString());
      }
      }
    The following Maven dependencies are required.
    <dependencies>
      <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>x.x.x</version>
      </dependency>
      <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>x.x.x</version>
       </dependency>
    </dependencies>
    Note Replace x.x.x with your cluster's Hadoop version.