Ubuntu及Ubuntu Docker中echo $USER输出空白问题咨询
Let's break this down step by step—your blank echo $USER output boils down to the USER environment variable not being properly initialized in your shell context, whether on your Ubuntu host or inside the Docker container. Here's what you're missing, and how to fix it to get your new user setup working:
Why echo $USER returns blank
On your Ubuntu host
- You might be using a non-login shell that skips loading standard profile scripts (like
.profileor.bashrc) which set theUSERvariable. For example, if you switched users withsu usernameinstead ofsu - username, the latter triggers a login shell that loads the target user's full environment (includingUSER). - Check your default shell with
echo $SHELL—if it's/bin/shinstead of/bin/bash, it might not initializeUSERthe same way, even if the user exists.
Inside the Docker container
- Most minimal Ubuntu Docker images start with a non-interactive shell by default, which doesn't initialize environment variables like
USERunless explicitly told to. - If you tried creating a user without setting a proper shell or home directory, switching to that user won't load the necessary environment variables.
Fixes & Step-by-Step Guide to Create and Switch to a New User
1. On your Ubuntu host
First, create the user with the right flags to ensure environment setup:
sudo useradd -m -s /bin/bash your-new-user sudo passwd your-new-user # Optional, but sets a password for the user
The -m flag creates a home directory, and -s /bin/bash sets bash as the default shell—both are critical for initializing variables like USER.
Then switch to the user correctly:
su - your-new-user
Now run echo $USER—it should output your-new-user as expected.
2. Inside a Docker container (two easy approaches)
Approach 1: Interactive user creation inside a running container
- Start an interactive bash session in the Ubuntu container:
docker run -it --rm ubuntu:latest bash - Create the new user with proper shell and home directory:
useradd -m -s /bin/bash docker-user passwd docker-user # Optional, but helpful if you need to switch back later - Switch to the new user with a login shell:
Nowsu - docker-userecho $USERwill showdocker-user.
Approach 2: Build a custom image with the user pre-configured
If you want the user to exist by default every time you run the container, create a Dockerfile:
FROM ubuntu:latest # Create the new user with home directory and bash shell RUN useradd -m -s /bin/bash docker-user # Set the default user to our new user USER docker-user # Set working directory to the user's home folder WORKDIR /home/docker-user
Build and run the image:
docker build -t ubuntu-custom-user . docker run -it --rm ubuntu-custom-user
You'll be automatically logged in as docker-user, and echo $USER will return the correct username immediately.
Key Takeaways
- Always use
su - username(not justsu username) to switch users—this ensures you load the full login environment, including variables likeUSER,HOME, andPATH. - When creating users (on host or container),
-m(home directory) and-s /bin/bash(default shell) are non-negotiable for proper environment initialization. - In Docker, if you use
--userto run a container without pre-configuring the user, you'll likely get a blankUSERbecause the shell isn't a login shell. Usingsu -inside the container or definingUSERin the Dockerfile fixes this.
内容的提问来源于stack exchange,提问作者max

