Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix UID management for ubuntu users (#3937)

Robert Brennanc864715b431f
diff --git a/openhands/runtime/client/client.py b/openhands/runtime/client/client.py
index 5d5f63993a..0481a200d0 100644
--- a/openhands/runtime/client/client.py
+++ b/openhands/runtime/client/client.py
@@ -195,27 +195,19 @@ class RuntimeClient:
else:
logger.debug('Added sudoer successfully.')
- # Attempt to add the user, retrying with incremented user_id if necessary
- while True:
- command = (
- f'useradd -rm -d /home/{username} -s /bin/bash '
- f'-g root -G sudo -u {user_id} {username}'
+ command = (
+ f'useradd -rm -d /home/{username} -s /bin/bash '
+ f'-g root -G sudo -u {user_id} {username}'
+ )
+ output = subprocess.run(command, shell=True, capture_output=True)
+ if output.returncode == 0:
+ logger.debug(
+ f'Added user `{username}` successfully with UID {user_id}. Output: [{output.stdout.decode()}]'
+ )
+ else:
+ raise RuntimeError(
+ f'Failed to create user `{username}` with UID {user_id}. Output: [{output.stderr.decode()}]'
)
- output = subprocess.run(command, shell=True, capture_output=True)
- if output.returncode == 0:
- logger.debug(
- f'Added user `{username}` successfully with UID {user_id}. Output: [{output.stdout.decode()}]'
- )
- break
- elif f'UID {user_id} is not unique' in output.stderr.decode():
- logger.warning(
- f'UID {user_id} is not unique. Incrementing UID and retrying...'
- )
- user_id += 1
- else:
- raise RuntimeError(
- f'Failed to create user `{username}`! Output: [{output.stderr.decode()}]'
- )
def _init_bash_shell(self, work_dir: str, username: str) -> None:
self.shell = pexpect.spawn(
diff --git a/openhands/runtime/utils/runtime_templates/Dockerfile.j2 b/openhands/runtime/utils/runtime_templates/Dockerfile.j2
index 1b8313d227..86dbaf4a88 100644
--- a/openhands/runtime/utils/runtime_templates/Dockerfile.j2
+++ b/openhands/runtime/utils/runtime_templates/Dockerfile.j2
@@ -18,6 +18,9 @@ RUN apt-get update && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
+# Remove UID 1000 if it's called pn--this fixes the nikolaik image for ubuntu users
+RUN if getent passwd 1000 | grep -q pn; then userdel pn; fi
+
# Create necessary directories
RUN mkdir -p /openhands && \
mkdir -p /openhands/logs && \