Add users to an SFTP server

This document shows how to add users to a Cloud FTP server. Users such as external partners and internal stakeholders can use the server to securely transfer files to and from Cloud Storage.

At a high level, these are the steps to add a user to an SFTP server:

  1. Configure permissions for the user.
  2. Create an SFTP user and map their directories to Cloud Storage buckets.

For steps to create a server, see Create an external SFTP server and Create an internal SFTP server.

Considerations

  • You can grant a user access to a maximum of 10 buckets.

  • A user can have a maximum of 10 public keys.

Before you begin

  1. Create one or more Cloud Storage buckets to store the data that the user will work with, if the buckets don't already exist.

  2. Get the user's public key from each SSH key pair that they will use to connect to the server.

    If the user doesn't have an SSH key pair, use the following steps to generate one.

    Generate an SSH key pair

    Which key format do you need?

    The key format that you need depends on which client you use to connect to the SFTP server. For more information, see Supported SFTP clients.

    PEM format

    1. To create an OpenSSH key pair (PEM format), use the ssh-keygen utility.

      On the client machine that will connect to the SFTP server, run the following command:

      ssh-keygen -t rsa -b 4096 -f ~/.ssh/KEY_PAIR_NAME

      Replace KEY_PAIR_NAME with a name for the key pair, such as sftp_user_key.

    2. Extract the public key:

      cat ~/.ssh/KEY_PAIR_NAME.pub

    PPK format

    1. To create a PuTTY key pair (PPK format), use the PuTTYgen tool.

      On the client machine that will connect to the SFTP server, run the following command:

      puttygen -t rsa -b 4096 -o KEY_PAIR_NAME.ppk

      Replace KEY_PAIR_NAME with a name for the key pair, such as sftp_user_key.

    2. Extract the public key:

      puttygen -L KEY_PAIR_NAME.ppk

Required roles

To get the permission that you need to add SFTP users, ask your administrator to grant you the FTP Admin (roles/ftp.admin) IAM role on your project. For more information about granting roles, see Manage access to projects, folders, and organizations.

This predefined role contains the ftp.users.create permission, which is required to add SFTP users.

You might also be able to get this permission with custom roles or other predefined roles.

Configure permissions for a user

  1. Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:

    gcloud init

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  2. Set the project:

    gcloud config set project PROJECT_ID

    Replace PROJECT_ID with the ID of the project that contains the SFTP server.

  3. Create a service account for the user, if a service account doesn't already exist. The service account accesses Cloud Storage resources on behalf of the user.

    gcloud iam service-accounts create USERNAME-sa \
        --description="USERNAME SFTP Service Account" \
        --display-name="USERNAME SFTP Service Account"

    Replace USERNAME with a unique username for the SFTP user. The username must start with a lowercase letter and can include lowercase letters, numbers, or hyphens. Maximum length is 32 characters.

    On success, a message like Created service account [example-userid-sa] is displayed.

  4. Grant IAM roles to the administrator who creates the SFTP user:

    1. Grant yourself the Service Account User (roles/iam.serviceAccountUser) role:

      gcloud iam service-accounts add-iam-policy-binding USERNAME-sa@PROJECT_ID.iam.gserviceaccount.com \
          --member="user:ADMINISTRATOR_EMAIL" \
          --role="roles/iam.serviceAccountUser"

      Replace ADMINISTRATOR_EMAIL with the email address of the principal who is creating the SFTP user. If you're creating the user (as opposed to an application), then this value is the email address that you use to access Google Cloud.

      On success, a message like the following is displayed:

      Updated IAM policy for serviceAccount [example-userid-sa@example-project.iam.gserviceaccount.com].
      bindings:
      - members:
      - user:admin@example.com
      role: roles/iam.serviceAccountUser
      etag: BwZJk7OiSzw=
      version: 1
      
    2. Grant yourself the Storage Bucket Viewer (roles/storage.bucketViewer) role on the bucket:

      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="user:ADMINISTRATOR_EMAIL" \
          --role="roles/storage.bucketViewer"

      Replace BUCKET_NAME with the name of the bucket.

      Repeat this step for each bucket that the user needs to access.

    3. Grant yourself the Storage Object Viewer (roles/storage.objectViewer) role on the bucket:

      gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
          --member="user:ADMINISTRATOR_EMAIL" \
          --role="roles/storage.objectViewer"

      Repeat this step for each bucket that the user needs to access.

  5. Grant the user's service account the IAM role that's necessary to access the bucket:

    gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
        --member="serviceAccount:USERNAME-sa@PROJECT_ID.iam.gserviceaccount.com" \
        --role="ROLE"

    Replace ROLE with one of the following IAM roles:

    • For read-only access, use the roles/storage.objectViewer role.
    • For read and write access, use the roles/storage.objectAdmin role.

    Repeat this step for each bucket that the user needs to access.

  6. Authorize the Cloud FTP Service Agent to generate tokens for the user's service account:

    1. Get the email address of the server's service agent. For steps, see Get details about a server.

    2. Authorize the Cloud FTP Service Agent:

      gcloud iam service-accounts add-iam-policy-binding USERNAME-sa@PROJECT_ID.iam.gserviceaccount.com \
          --member="serviceAccount:SERVICE_AGENT_EMAIL" \
          --role="roles/iam.serviceAccountTokenCreator"

      Replace SERVICE_AGENT_EMAIL with the service agent's email address.

Next, create a user.

Create an SFTP server user

After you configure permissions for a user, you create the user and map their directories to one or more Cloud Storage buckets.

gcloud

  1. To create a user for an SFTP server, run the gcloud alpha storage ftp users create command.

    Before using any of the command data below, make the following replacements:

    • CREDENTIAL_NAME: a unique name to identify the user credentials.
    • SSH_PUBLIC_KEY: the body of the user's SSH public key, in OpenSSH format. For example, ssh-rsa AAAAB3NzaC1ycRexample....
    • USERNAME: the SFTP user's username.
    • SERVICE_ACCOUNT: the user's service account. For example, username-sa@example-project.iam.gserviceaccount.com.
    • LOCATION_ID: the location of the server, such as us-west1.
    • SERVER_ID: the server ID.
    • BUCKET_NAME: the name of a bucket that the SFTP user is granted access to, such as example-bucket. Omit the gs://.
    • (Optional) BUCKET_PREFIX: the path of a folder within the bucket to set as the root directory for this directory mapping. If you omit the bucket_prefix property, then Cloud FTP uses the bucket root.
    • DIRECTORY: the logical landing directory path that is presented to the SFTP user. For example, /home/uploads.

      If you map multiple buckets or folders, then provide a unique directory path for each mapping.

      Nested logical directories aren't supported. If you provide multiple mappings for a directory, then provide the directories in a flat structure instead of a nested structure. For example, use /dir1 and /dir2 instead of /dir1 and /dir1/dir2.

    • SFTP_PERMISSION: the access level for the directory. For read-only access, set this value to READ_ONLY. For read and write access, set this value to READ_WRITE.

    Note the following:

    • To grant a user access to multiple buckets, provide the --storage-directory-mapping flag multiple times, with a directory mapping for each bucket.
    • To configure multiple public keys for a user, provide multiple credentials in the credentials.json file.

    Save the following content in a file called credentials.json:

    [
      {
        "credentialName": "CREDENTIAL_NAME",
        "credentialType": "PUBLIC_KEY",
        "sshPublicKeyBody": "SSH_PUBLIC_KEY"
      }
    ]

    Execute the following command:

    Linux, macOS, or Cloud Shell

    gcloud alpha storage ftp users create USERNAME \
        --customer-service-account=SERVICE_ACCOUNT --location=LOCATION_ID \
        --server=SERVER_ID \
        --storage-directory-mapping=bucket=BUCKET_NAME,bucket_prefix=BUCKET_PREFIX,directory=DIRECTORY,permission=SFTP_PERMISSION \
        --user-credentials-from-file=credentials.json

    Windows (PowerShell)

    gcloud alpha storage ftp users create USERNAME `
        --customer-service-account=SERVICE_ACCOUNT --location=LOCATION_ID `
        --server=SERVER_ID `
        --storage-directory-mapping=bucket=BUCKET_NAME,bucket_prefix=BUCKET_PREFIX,directory=DIRECTORY,permission=SFTP_PERMISSION `
        --user-credentials-from-file=credentials.json

    Windows (cmd.exe)

    gcloud alpha storage ftp users create USERNAME ^
        --customer-service-account=SERVICE_ACCOUNT --location=LOCATION_ID ^
        --server=SERVER_ID ^
        --storage-directory-mapping=bucket=BUCKET_NAME,bucket_prefix=BUCKET_PREFIX,directory=DIRECTORY,permission=SFTP_PERMISSION ^
        --user-credentials-from-file=credentials.json
    It takes a few seconds for the user to be created.

  2. Give the user the following information, which they need to connect to the server:

    • The user's SFTP username.

    • The server's access configuration, which depends on the server type:

      • For an external server, the server's IP address.

      • For an internal server, the URI of the server's service attachment.

      For steps to get the server's access configuration, see Get details about a server.

    • (Optional) The key fingerprint of the server.

REST

  1. To create a user for an SFTP server, use the servers.users.create method.

    Before using any of the request data, make the following replacements:

    • PROJECT_ID: the Google Cloud project ID of the server.
    • LOCATION_ID: the location of the server, such as us-west1.
    • SERVER_ID: the server ID.
    • USERNAME: the SFTP user's username.
    • BUCKET_NAME: the name of a bucket that the SFTP user is granted access to, such as example-bucket. Omit the gs://.
    • (Optional) BUCKET_PREFIX: the path of a folder within the bucket to set as the root directory for this directory mapping. If you omit this value, then Cloud FTP uses the bucket root.
    • DIRECTORY: the logical landing directory path that is presented to the SFTP user. For example, /home/uploads. If you omit this value, then Cloud FTP sets the landing directory to /.

      If you map multiple buckets or folders, then provide a unique directory path for each mapping.

      Nested logical directories aren't supported. If you provide multiple mappings for a directory, then provide the directories in a flat structure instead of a nested structure. For example, use /dir1 and /dir2 instead of /dir1 and /dir1/dir2.

    • SFTP_PERMISSION: the access level for the directory. For read-only access, set this value to READ_ONLY. For read and write access, set this value to READ_WRITE.
    • SERVICE_ACCOUNT: the user's service account. For example, username-sa@example-project.iam.gserviceaccount.com.
    • CREDENTIAL_NAME: a unique name to identify the user credentials.
    • SSH_PUBLIC_KEY: the body of the user's SSH public key, in OpenSSH format. For example, ssh-rsa AAAAB3NzaC1ycRexample....

    Note the following:

    • To grant a user access to multiple buckets, provide multiple bucket mappings in the storageDirectoryMappings list.
    • To configure multiple public keys for a user, provide multiple credentials in the userCredentials list.

    HTTP method and URL:

    POST https://ftp.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/servers/SERVER_ID/users?userId=USERNAME

    Request JSON body:

    {
      "storageDirectoryMappings": [
        {
          "bucket": "BUCKET_NAME",
          "bucketPrefix": "BUCKET_PREFIX",
          "directory": "DIRECTORY",
          "permission": "SFTP_PERMISSION"
        }
      ],
      "customerServiceAccount": "SERVICE_ACCOUNT",
      "userCredentials": [
        {
          "credentialName": "CREDENTIAL_NAME",
          "credentialType": "PUBLIC_KEY",
          "sshPublicKeyBody": "SSH_PUBLIC_KEY"
        }
      ]
    }
    

    To send your request, expand one of these options:

    The response identifies a long-running operation. It takes a few seconds for the user to be created.

  2. Give the user the following information, which they need to connect to the server:

    • The user's SFTP username.

    • The server's access configuration, which depends on the server type:

      • For an external server, the server's IP address.

      • For an internal server, the URI of the server's service attachment.

      For steps to get the server's access configuration, see Get details about a server.

    • (Optional) The key fingerprint of the server.

What's next