Contexts
About 1111 words
A context names three things: an account (how to authenticate), a controller (what to talk to) and a default site. If you know kubectl contexts, you know these; unifi ctx is the counterpart of kubectl config.
$ unifi ctx get
NAME ACCOUNT TYPE CONTROLLER SITE
→ demo demo local demo Default
demo-lake demo local demo Lake Houseunifi login creates contexts. This guide is about everything after that: switching, pointing a single command somewhere else, and keeping several controllers and accounts apart.
The three parts
The configuration file keeps accounts, controllers and contexts in separate lists, and a context refers to the other two by name:
accounts:
- name: me
type: cloud # a Site Manager API key from unifi.ui.com
email: [email protected]
- name: udm-local
type: local # an API key created on the console
controllers:
- name: udm
consoleId: 0123456789ABCDEF # reached through the cloud connector
- name: udm-lan
address: https://192.168.1.1 # reached directly
caFile: ~/.config/unifi-cli/udm.pem
contexts:
- name: home
account: me
controller: udm
site: Default
- name: home-lan
account: udm-local
controller: udm-lan
site: DefaultThe account type decides the path. A cloud account needs a controller with a consoleId; a local account needs one with an address. API keys aren't in this file: each account's key lives in the OS keychain, or in the credentials file next to the configuration on machines without one, under the account's name.
Switch the current context
unifi ctx use home # by name
unifi ctx use # pick from a list, on a terminal
unifi ctx current # print the name, for scriptsunifi ctx current prints the bare name and exits non-zero when no context is selected. unifi ctx get --output wide adds what each context points at: the controller's address, or the console ID for a cloud context.
Point one command somewhere else
--context and --site override the current context and its default site for a single command, without changing anything on disk:
unifi --context office devices get --offline
unifi clients get --site "Lake House"
unifi --context office --site Warehouse ports getBoth flags work in any position and on every command. In scripts and in anything that changes the network, pass them explicitly, so that the target never depends on whichever context you last switched to in another terminal.
Work with several sites
One controller can host several sites, and a context has one of them as its default.
$ unifi sites get
NAME REFERENCE ID
→ Default default e897c8c3-ca84-453e-a592-20616ab0e2c4
Lake House lakehouse cf32427a-02f8-426a-a56c-8494938d0a08A site answers to its name, its internal reference or its ID, so --site "Lake House" and --site lakehouse mean the same. The arrow marks the site commands act on.
To change the default site of the current context:
unifi sites use "Lake House"
unifi sites use # pick from a list, on a terminalThat is shorthand for unifi ctx set --site "Lake House". unifi-cli checks the name against the controller and stores it the way the controller spells it.
If you move between two sites all day, give each its own context instead. The same account and controller can back any number of contexts; add the second one by hand with unifi config edit, following the demo-lake entry in examples/config.yaml, or sign in again with --site and a new --name.
Work with several consoles and accounts
Each unifi login adds a context and leaves the others alone:
unifi login --console "Dream Machine Pro Max" --name home
unifi login --console "Lake House Gateway" --name lake
unifi login --local --address https://10.20.0.1 --ca-file office.pem --name officeunifi login --all-consoles creates one context per console of the account in a single step. For a cloud account, unifi consoles get lists what the account can reach and marks the console the current context talks to:
$ unifi consoles get --output wide
NAME MODEL VERSION STATUS IP ACCESS LAST SEEN ID
→ Dream Machine Pr… UDM Pro Max 4.3.6 online 203.0.113.10 owner 263d ago demo-console-home
Lake House Gatew… Cloud Gateway Ul… 4.3.6 online 198.51.100.24 invited 263d ago demo-console-lakeConsoles exist for cloud accounts only. A local context knows exactly one controller, and unifi consoles get says so when you ask it.
Two UI accounts, say a private one and a customer's, are two sign-ins with two keys. Their contexts sit side by side in the same file, each bound to its own account.
Inspect and repair a context
unifi ctx describe is the first thing to run when a command can't connect. It shows what the context resolves to and, unless you pass --offline, asks the controller for its version and sites. It doubles as a health check: it exits 1 when the controller can't be reached or rejects the key, after showing what it found.
$ unifi ctx describe
demo ● current ● reachable
Context
Name demo
Default site Default
Account
Name demo
Type local (API key of the controller, direct connection)
API key stored in the keyring credential store
Controller
Name demo
Address https://localhost:8443
TLS certificate is not verified
Connectivity
Status reachable
UniFi Network 10.0.160
Sites Default (default)
Lake Houseunifi ctx set changes a context in place:
unifi ctx set --site "Lake House" # new default site
unifi ctx set home --name house # rename the context
unifi ctx set lab --controller udm --account local --offlineunifi ctx rename <old> <new> renames a context as well, and goes one step further. unifi login names the account and the controller of a context after it; when they still carry the old name and nothing else uses them, rename offers to rename them along, and scripts pass --entries for that. The stored API key moves with the account.
unifi ctx rename 192.168.1.1 home --entriesThe account and controller you name with ctx set must already exist in the configuration. --offline skips the check of the site against the controller, for when the controller is unreachable.
Remove a context
unifi ctx delete labThe account and the controller go with the context when nothing else uses them, and so do the stored API key and the certificate pinned for the controller. unifi logout lab is the same operation under a name people look for.
Show the context in your prompt
Once you have more than one context, put the current one where you can see it. unifi prompt prints it without touching the network or the credential store; the prompt guide has snippets for starship, oh-my-posh and the common shells.
