# User and workdir (/docs/template/user-and-workdir)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 814 · updated: 2026-07-30 -->
Related: [Quickstart](/docs/template/quickstart.md), [How it works](/docs/template/how-it-works.md), [Caching](/docs/template/caching.md), [Base image](/docs/template/base-image.md), [Private registries](/docs/template/private-registries.md), [Defining template](/docs/template/defining-template.md)

The default user in the template is `user` with the `/home/user` (home directory) as the working directory.
This is different from the Docker defaults, where the default user is `root` with `/` as the working directory. This is to help with tools installation, and to improve default security.

The last set user and workdir in the template is then persisted as a default to the sandbox execution.
Example of setting user and workdir in the template definition are below.

<Info>
  Requires the E2B SDK version at least 2.3.0
</Info>

## Default user and workdir in sandbox [#default-user-and-workdir-in-sandbox]

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```typescript  
      const sbx = await Sandbox.create()
      await sbx.commands.run("whoami") // user
      await sbx.commands.run("pwd") // /home/user
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      sbx = Sandbox.create()
      sbx.commands.run("whoami")  # user
      sbx.commands.run("pwd")  # /home/user
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

## Custom user and workdir template [#custom-user-and-workdir-template]

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```typescript  
      // template.ts
      const template = Template()
        .fromBaseImage()
        .runCmd("whoami") // user
        .runCmd("pwd") // /home/user
        .setUser("guest")
        .runCmd("whoami") // guest
        .runCmd("pwd") // /home/guest


      // build_dev.ts
      await Template.build(template, 'custom-user-template', {
        onBuildLogs: defaultBuildLogger()
      })


      // index.ts
      const sbx = await Sandbox.create("custom-user-template")
      await sbx.commands.run("whoami") // guest
      await sbx.commands.run("pwd") // /home/guest
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      # template.py
      template = (
          Template()
          .from_base_image()
          .run_cmd("whoami") # user
          .run_cmd("pwd") # /home/user
          .set_user("guest")
          .run_cmd("whoami") # guest
          .run_cmd("pwd") # /home/guest
      )


      # build_dev.py
      Template.build(template, 'custom-user-template',
          on_build_logs=default_build_logger()
      )


      # main.py
      sbx = Sandbox.create("custom-user-template")
      sbx.commands.run("whoami")  # guest
      sbx.commands.run("pwd")  # /home/guest
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>
