Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use user property as the default user for SSH #2263

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/dstack/_internal/core/services/ssh/attach.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(
ssh_port: int,
container_ssh_port: int,
user: str,
container_user: str,
id_rsa_path: PathLike,
ports_lock: PortsLock,
run_name: str,
Expand All @@ -74,7 +75,7 @@ def __init__(
self.control_sock_path = FilePath(control_sock_path)
self.identity_file = FilePath(id_rsa_path)
self.tunnel = SSHTunnel(
destination=run_name,
destination=f"root@{run_name}",
identity=self.identity_file,
forwarded_sockets=ports_to_forwarded_sockets(
ports=self.ports,
Expand All @@ -91,7 +92,7 @@ def __init__(
self.host_config = {
"HostName": hostname,
"Port": ssh_port,
"User": user,
"User": user if dockerized else container_user,
"IdentityFile": self.identity_file,
"IdentitiesOnly": "yes",
"StrictHostKeyChecking": "no",
Expand All @@ -111,7 +112,7 @@ def __init__(
self.container_config = {
"HostName": "localhost",
"Port": container_ssh_port,
"User": "root", # TODO(#1535): support non-root images properly
"User": container_user,
"IdentityFile": self.identity_file,
"IdentitiesOnly": "yes",
"StrictHostKeyChecking": "no",
Expand All @@ -122,7 +123,7 @@ def __init__(
self.container_config = {
"HostName": hostname,
"Port": ssh_port,
"User": user,
"User": container_user,
"IdentityFile": self.identity_file,
"IdentitiesOnly": "yes",
"StrictHostKeyChecking": "no",
Expand All @@ -136,7 +137,7 @@ def __init__(
self.host_config = {
"HostName": hostname,
"Port": container_ssh_port,
"User": "root", # TODO(#1535): support non-root images properly
"User": container_user,
"IdentityFile": self.identity_file,
"IdentitiesOnly": "yes",
"StrictHostKeyChecking": "no",
Expand Down
8 changes: 8 additions & 0 deletions src/dstack/api/_public/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,19 @@ def attach(
if runtime_data is not None and runtime_data.ports is not None:
container_ssh_port = runtime_data.ports.get(container_ssh_port, container_ssh_port)

# TODO: get login name from runner in case it's not specified in the run configuration
# (i.e. the default image user is used, and it is not root)
if job.job_spec.user is not None and job.job_spec.user.username is not None:
container_user = job.job_spec.user.username
else:
container_user = "root"

self._ssh_attach = SSHAttach(
hostname=provisioning_data.hostname,
ssh_port=provisioning_data.ssh_port,
container_ssh_port=container_ssh_port,
user=provisioning_data.username,
container_user=container_user,
id_rsa_path=ssh_identity_file,
ports_lock=self._ports_lock,
run_name=name,
Expand Down