Ubuntu Linux for Robotics · Lesson 39 of 96
ROS 2 environment variables
ROS_DISTRO, ROS_DOMAIN_ID, RMW_IMPLEMENTATION, ROS_LOCALHOST_ONLY and logging variables.
Estimated time · 30–45 min
Learning goals
What you will understand
- Explain ros 2 environment variables in your own words
- Use it in a small workstation example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
ROS 2 is configured by environment variables. ROS_DISTRO names the distribution. ROS_DOMAIN_ID selects the DDS domain (0–101). RMW_IMPLEMENTATION picks the middleware. ROS_LOCALHOST_ONLY=1 restricts communication to the local machine, which is useful on a shared network. ROS_LOG_DIR and RCUTILS_CONSOLE_OUTPUT_FORMAT control logs.
Set them per terminal for experiments, and in a launch script or service file for a deployed robot. Print them all with printenv | grep -E 'ROS|RMW|AMENT'. When two machines behave differently the cause is very often a different value of one of these variables.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the ros 2 environment variables commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
printenv | grep -E 'ROS|RMW|AMENT'
# Show every ROS-related variable in this terminal.
export ROS_DOMAIN_ID=5
# Use domain 5 for this terminal.
export ROS_LOCALHOST_ONLY=1
# Keep all traffic on this computer.
export RCUTILS_COLORIZED_OUTPUT=1
# Colourize log output.
export RCUTILS_CONSOLE_OUTPUT_FORMAT="[{severity}] [{time}] [{name}]: {message}"
# Choose what each log line contains.
ros2 run demo_nodes_cpp talker
# Run a node with these settings.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 3.Comments beginning with # explain safety checks and are not executed by the shell.
Watch it run · Build and run
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for ROS 2 environment variables. Expected result: 5
Practice code
Now type it yourself
Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.
# TODO: set ROS_DOMAIN_ID to 5 and print itExpected output
5Stuck? Show the full solution (try it yourself first)
export ROS_DOMAIN_ID=5
echo "$ROS_DOMAIN_ID"Expected result
How you know it worked
- Each command completes without an unexplained error.
- Your notes contain the command, the important output, and what it proves.
Practical exercise
Apply the lesson
Run a talker in domain 5 and a listener in domain 6 and show they do not connect; then set ROS_LOCALHOST_ONLY=1 and confirm that a second computer cannot see the talker.
Before you continue
- You recorded the starting state
- Every command and output is understood
- The change can be repeated from your notes
If something goes wrong
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.