To list the spaces you have access to in the Cloud Foundry CLI, use the command cf spaces. This command retrieves a list of all spaces within your currently targeted organization, identifying which one is currently active and which others are available for your applications to be deployed into.
Cloud Foundry organizes resources in a hierarchy: an Account contains Organizations, and Organizations contain Spaces. A Space is the actual environment where your applications live, containing the buildpacks, environment variables, and routing rules necessary for your code to run. Knowing how to list and switch between these spaces is fundamental for managing development, staging, and production environments without accidentally deploying code to the wrong target.
What You'll Need
- The Cloud Foundry Command Line Interface (cf CLI) installed on your local machine.
- An active account with a Cloud Foundry foundation (such as SAP BTP, Tanzu, or IBM Cloud).
- Valid login credentials (username and password) for your CF account.
- The API endpoint URL for your specific foundation.
- An existing Organization already created or assigned to your user account.
Step-by-Step
- Open your terminal or command prompt. Launch the command-line interface native to your operating system (such as Terminal on macOS, PowerShell or CMD on Windows, or Bash on Linux).
- Authenticate with the CF API. Before you can list spaces, you must be logged in. Use the command
cf login. The CLI will prompt you for your API endpoint if it is not already configured. Enter the URL provided by your cloud provider. - Enter your credentials. When prompted, provide your email address and password. Ensure you use the credentials associated with the account that has permissions to view the organizations and spaces.
- Select your Organization. If your account belongs to multiple organizations, the CLI will present a numbered list. Type the number corresponding to the organization you wish to target and press Enter. If you only belong to one organization, the CLI will select it automatically.
- Execute the list command. Type
cf spacesand press Enter. The CLI will communicate with the Cloud Foundry controller and return a table of all spaces within that organization. - Identify the active space. In the output table, look for the asterisk (*) symbol. The space marked with the asterisk is your currently targeted space. Any subsequent commands, such as
cf pushorcf apps, will be executed within this specific space. - Verify the space name. The output will list the names of all available spaces. Note the exact spelling and capitalization of the space name you intend to use, as CF CLI commands are case-sensitive.
Common Mistakes
One of the most frequent errors is attempting to run cf spaces before running cf login. The CLI cannot query the API for space names without a valid session token. If you see an error stating that you are not authenticated, you must complete the login process first.
Another common mistake is forgetting that spaces are nested within organizations. Users often try to find a space that exists in a different organization than the one they currently have targeted. If cf spaces returns a list that does not include the space you are looking for, you likely need to change your targeted organization using cf target -o ORG_NAME before listing the spaces again.
Case sensitivity is also a recurring issue. While cf spaces lists the names, users often mistype the name when trying to target that space (e.g., using cf target -s dev-space when the actual name is Dev-Space). Always copy the name exactly as it appears in the output of the list command.
Finally, some users confuse the cf apps command with the cf spaces command. cf apps lists the applications within the current space, whereas cf spaces lists the available environments within the organization. If you see a list of app names instead of environment names, you have used the wrong command.
Tips
To speed up your workflow, you can combine targeting and listing. If you know you need to move to a different space, use cf target -s SPACE_NAME. After doing this, run cf spaces again to confirm the asterisk has moved to the new space, providing a visual confirmation that your target has changed.
For users managing a large number of spaces, the output of cf spaces can be long. You can pipe the output to a search tool to find a specific space quickly. On Linux or macOS, you can use cf spaces | grep "keyword" to filter the list. On Windows PowerShell, you can use cf spaces | Select-String "keyword".
If you frequently switch between different environments (like dev, test, and prod), consider creating small shell scripts or aliases in your .bashrc or .zshrc file. For example, an alias like alias cf-dev="cf target -o my-org -s dev-space" allows you to switch and verify your environment with a single short command rather than typing the full target string every time.
Always check your target before running destructive commands. A good habit is to run cf target (which shows the current Org and Space) immediately before running cf delete-app or cf push to ensure you are not accidentally modifying a production environment when you intended to work in development.
Troubleshooting
If the cf spaces command returns an empty list or an error, first check your connectivity. Ensure you have a stable internet connection and that any corporate VPNs or proxies required to reach the Cloud Foundry API are active. If the API is unreachable, the CLI will timeout or return a connection error.
If you are logged in but cannot see a space you know exists, check your permissions. In Cloud Foundry, access to spaces is managed via roles (e.g., Space Developer, Space Manager). If your user account has not been granted a role within that specific space, the API will not return that space in the list for security reasons. Contact your organization manager to verify that your account has been added to the space with the appropriate permissions.
If the CLI behaves unexpectedly or hangs, try clearing your local session. Use cf logout to terminate the current session and then log back in. This refreshes the OAuth token and ensures that any changes to your organization or space memberships are synchronized with your local CLI state.
In rare cases, the CLI version may be outdated, leading to formatting issues or command failures. Check your version using cf version. If it is significantly behind the current release, download the latest version of the CF CLI from the official provider to ensure compatibility with the API features of your foundation.
Frequently Asked Questions
Can I list spaces across all organizations at once?
No, the cf spaces command only lists spaces within the organization you are currently targeting. To see spaces in another organization, you must first change your target organization using cf target -o ORG_NAME.
How do I change the space I am currently using?
Use the command cf target -s SPACE_NAME. Replace SPACE_NAME with the exact name of the space you found when running the cf spaces command.
What does the asterisk next to a space name mean?
The asterisk indicates the "current target." Any application-level commands you run will be applied to the space marked with the asterisk.
Can I create a new space from the CLI?
Yes, if you have the Space Manager or Org Manager role, you can use cf create-space SPACE_NAME to create a new environment within your current organization.
Why is the space name showing up as empty?
This usually happens if you are targeted to an organization but have not yet been assigned to any spaces within that organization. You will need an administrator to grant you access to at least one space.
Is there a way to see all organizations I belong to?
Yes, you can use the cf orgs command to list all organizations associated with your account, similar to how cf spaces lists spaces within an organization.