Home / OCI

OCICheat sheetBeginner

OCI instance metadata cheat sheet: IMDSv2 queries for OCID, region, shape, tags and VNICs in scripts

Copy-paste IMDSv2 curl and PowerShell queries that let scripts on an OCI instance find their own OCID, region, compartment, shape, tags and VNIC details without API keys.

Deepak Bhardwaj6 min read

Applies toOCI Compute VM and bare metal instances (Oracle Linux, Ubuntu, Windows), Instance Metadata Service v2, OCI CLI 3.x

When to use this

A boot script, monitoring agent or backup job running on an instance often needs to know "who am I": the instance OCID, its compartment, region, availability domain, private IP or a tag value. You don't need an API key or the OCI CLI for that. The Instance Metadata Service (IMDS) answers on a link-local address from inside the instance.

Use IMDS v2 for everything new. If your tenancy has turned off v1 (see the IMDSv1 hardening post), any /opc/v1/ call returns 404.

The two rules of IMDSv2

  1. Base URL: http://169.254.169.254/opc/v2/
  2. Every request must send the header Authorization: Bearer Oracle. Without it, the call is rejected.
IMDS=http://169.254.169.254/opc/v2
H='Authorization: Bearer Oracle'
curl -s -H "$H" -L $IMDS/instance/ | jq .

On Windows:

$h = @{ Authorization = 'Bearer Oracle' }
Invoke-RestMethod -Headers $h -Uri 'http://169.254.169.254/opc/v2/instance/'

Tip

Append a field name to the path to get a single value as plain text (/instance/id, /instance/region). That avoids a jq dependency in minimal images.

Instance identity queries

You need Path under /opc/v2/ Example output
Full instance document instance/ JSON
Instance OCID instance/id ocid1.instance.oc1.iad...
Display name instance/displayName web-01
Compartment OCID instance/compartmentId ocid1.compartment.oc1..
Region (short key) instance/region iad
Region (full name) instance/canonicalRegionName us-ashburn-1
Region and realm details instance/regionInfo/ JSON with realm and region keys
Availability domain instance/availabilityDomain Uocm:US-ASHBURN-AD-1
Fault domain instance/faultDomain FAULT-DOMAIN-2
Shape instance/shape VM.Standard.E5.Flex
OCPUs and memory instance/shapeConfig/ JSON
Image OCID instance/image ocid1.image.oc1...
Launch time instance/timeCreated epoch milliseconds

Field names are case sensitive. If a path returns 404, dump instance/ and check the exact spelling for your instance; a few fields appear only on some shapes or platform versions.

# Common variables for a bootstrap script
INSTANCE_ID=$(curl -s -H "$H" $IMDS/instance/id)
COMPARTMENT_ID=$(curl -s -H "$H" $IMDS/instance/compartmentId)
REGION=$(curl -s -H "$H" $IMDS/instance/canonicalRegionName)
AD=$(curl -s -H "$H" $IMDS/instance/availabilityDomain)
echo "$INSTANCE_ID in $REGION / $AD"

Tags and custom metadata

Tags are useful to drive configuration: one image, different behaviour per Environment or Role tag.

# Freeform tags
curl -s -H "$H" $IMDS/instance/freeformTags | jq .

# Defined tags: namespace -> key -> value
curl -s -H "$H" $IMDS/instance/definedTags | jq -r '.Operations.Environment'

# Custom metadata you passed at launch (--metadata / --extended-metadata)
curl -s -H "$H" $IMDS/instance/metadata/ | jq 'del(.user_data, .ssh_authorized_keys)'
curl -s -H "$H" $IMDS/instance/metadata/app_role

If a tag value in IMDS doesn't match what the console shows, use the API (oci compute instance get) as the source of truth.

user_data comes back base64-encoded:

curl -s -H "$H" $IMDS/instance/metadata/user_data | base64 -d

To add or change custom metadata from outside the instance:

oci compute instance get --instance-id $INSTANCE_ID --query 'data.metadata'
oci compute instance update --instance-id $INSTANCE_ID \
  --metadata '{"ssh_authorized_keys":"<existing value>","app_role":"web"}'

Caution

--metadata on update sets the whole map. Fetch the current values first and include every key you want to keep, or you can lose ssh_authorized_keys from the metadata record. Check oci compute instance update --help on your CLI version for what can be changed after launch. Changing user_data later does not re-run cloud-init on an existing instance.

Network (VNIC) queries

curl -s -H "$H" $IMDS/vnics/ | jq .

Returns one entry per attached VNIC, primary first. The fields scripts use most:

Field Use it for
vnicId Calling oci network vnic get or attaching NSGs
privateIp Registering the host in DNS, a load balancer or a config file
macAddr Mapping a secondary VNIC to its Linux interface name
subnetCidrBlock Building policy routes for secondary VNICs
virtualRouterIp Default gateway for that VNIC's subnet
vlanTag Bare metal secondary VNIC VLAN configuration
# Primary private IP
curl -s -H "$H" $IMDS/vnics/ | jq -r '.[0].privateIp'

# Find which Linux interface carries each VNIC
for row in $(curl -s -H "$H" $IMDS/vnics/ | jq -r '.[] | "\(.macAddr)=\(.privateIp)"'); do
  mac=${row%%=*}; ip=${row#*=}
  dev=$(ip -o link | awk -v m="${mac,,}" 'tolower($0) ~ m {print $2}' | tr -d ':')
  echo "$ip -> ${dev:-not configured}"
done

IMDS does not return the public IP. Get it from the API:

VNIC_ID=$(curl -s -H "$H" $IMDS/vnics/ | jq -r '.[0].vnicId')
oci network vnic get --vnic-id $VNIC_ID --query 'data."public-ip"' --raw-output --auth instance_principal

Using IMDS with the OCI CLI and SDKs

When you run the CLI with --auth instance_principal, the CLI and SDKs fetch the instance's identity certificates from IMDS for you. You don't call those endpoints yourself. Two practical points:

  • Set the region from IMDS so scripts work unchanged in every region:
export OCI_CLI_AUTH=instance_principal
export OCI_CLI_REGION=$(curl -s -H "$H" $IMDS/instance/canonicalRegionName)
oci os ns get
  • Older SDK and CLI releases call v1 endpoints. If instance principal auth fails only after v1 was disabled, upgrade the CLI/SDK before anything else.

Quick troubleshooting

Symptom Likely cause Fix
404 on every /opc/v1/ call IMDSv1 disabled on the instance Move the caller to /opc/v2/ with the header
401 or rejected on /opc/v2/ Missing Authorization: Bearer Oracle header Add the header
Connection timeout to 169.254.169.254 Host firewall or a custom route sending link-local traffic elsewhere; proxy env vars Allow 169.254.0.0/16 locally; set NO_PROXY=169.254.169.254
Works on the host, fails in a container Container network or proxy settings Test with curl inside the container; check NO_PROXY and egress rules
Tag value differs from the console Value changed after the script cached it Read via oci compute instance get
# Is a proxy in the way?
env | grep -i _proxy
curl -sv --noproxy '*' -H "$H" $IMDS/instance/id

Prevent it next time

  • Put the IMDS and header variables in one shared shell function or module and reuse it in all bootstrap scripts.
  • Use canonicalRegionName and compartmentId from IMDS instead of hard-coding them in images.
  • Test new images with IMDSv1 disabled (--instance-options '{"areLegacyImdsEndpointsDisabled": true}' at launch) so v1 callers fail in testing, not in production.
  • Keep NO_PROXY=169.254.169.254 in the default environment of any image that uses an outbound proxy.