Skip to content

Commit bef3aaa

Browse files
henrybarretogustavosbarreto
authored andcommitted
feat(ssh): set SSH_CLIENT environment variable for remote sessions
Add support for populating the SSH_CLIENT environment variable on incoming SSH connections. This allows shells and scripts running on the server to detect the client IP, source port, and server port for the session, improving compatibility with remote shell behaviors (e.g., Bash reading ~/.bashrc for non-interactive sessions). This change aligns with standard SSH behavior and helps scripts and monitoring tools identify remote clients reliably.
1 parent f48dedf commit bef3aaa

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

agent/server/modes/host/command/command_docker.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@ func NewCmd(u *osauth.User, shell, term, host string, envs []string, command ...
1616
nscommand, _ := nsenterCommandWrapper(u.UID, u.GID, u.HomeDir, command...)
1717

1818
cmd := exec.Command(nscommand[0], nscommand[1:]...) //nolint:gosec
19+
// TODO: There are other environment variables we could set like SSH_CONNECTION, SSH_TTY, SSH_ORIGINAL_COMMAND, etc.
20+
// We need to check which ones are relevant and set them accordingly.
21+
// https://en.wikibooks.org/wiki/OpenSSH/Client_Applications
1922
cmd.Env = []string{
2023
"TERM=" + term,
2124
"HOME=" + u.HomeDir,
2225
"SHELL=" + shell,
2326
"USER=" + u.Username,
2427
"LOGNAME=" + u.Username,
2528
"SHELLHUB_HOST=" + host,
29+
// NOTE: We need to set the SSH_CLIENT because some applications (like bash) check for it to enable some
30+
// features or load some files (like .bashrc). Currently, we don't have this information, so we set a fake one.
31+
// TODO: Set the real SSH_CLIENT value.
32+
// Format: "<ip> <source-port> <destination-port>"
33+
// https://en.wikibooks.org/wiki/OpenSSH/Client_Applications
34+
"SSH_CLIENT=127.0.0.1 0 0",
2635
}
2736
cmd.Env = append(cmd.Env, envs...)
2837

agent/server/modes/host/command/command_native.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,20 @@ func NewCmd(u *osauth.User, shell, term, host string, envs []string, command ...
2929
}
3030

3131
cmd := exec.Command(command[0], command[1:]...) //nolint:gosec
32+
// TODO: There are other environment variables we could set like SSH_CONNECTION, SSH_TTY, SSH_ORIGINAL_COMMAND, etc.
33+
// We need to check which ones are relevant and set them accordingly.
34+
// https://en.wikibooks.org/wiki/OpenSSH/Client_Applications
3235
cmd.Env = []string{
3336
"TERM=" + term,
3437
"HOME=" + u.HomeDir,
3538
"SHELL=" + shell,
3639
"SHELLHUB_HOST=" + host,
40+
// NOTE: We need to set the SSH_CLIENT because some applications (like bash) check for it to enable some
41+
// features or load some files (like .bashrc). Currently, we don't have this information, so we set a fake one.
42+
// TODO: Set the real SSH_CLIENT value.
43+
// Format: "<ip> <source-port> <destination-port>"
44+
// https://en.wikibooks.org/wiki/OpenSSH/Client_Applications
45+
"SSH_CLIENT=127.0.0.1 0 0",
3746
}
3847
cmd.Env = append(cmd.Env, envs...)
3948

0 commit comments

Comments
 (0)