Skip to content

Commit afd01d8

Browse files
committed
Add SUDO_TTY environment variable if the user has a tty
This can be used to find the user's original tty device when sudo runs the command in its own pty. GitHub issue #447.
1 parent a925829 commit afd01d8

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

docs/sudo.man.in

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.\"
33
.\" SPDX-License-Identifier: ISC
44
.\"
5-
.\" Copyright (c) 1994-1996, 1998-2005, 2007-2024
5+
.\" Copyright (c) 1994-1996, 1998-2005, 2007-2025
66
.\" Todd C. Miller <[email protected]>
77
.\"
88
.\" Permission to use, copy, modify, and distribute this software for any
@@ -25,7 +25,7 @@
2525
.nr BA @BAMAN@
2626
.nr LC @LCMAN@
2727
.nr PS @PSMAN@
28-
.TH "SUDO" "@mansectsu@" "December 20, 2024" "Sudo @PACKAGE_VERSION@" "System Manager's Manual"
28+
.TH "SUDO" "@mansectsu@" "June 7, 2025" "Sudo @PACKAGE_VERSION@" "System Manager's Manual"
2929
.nh
3030
.if n .ad l
3131
.SH "NAME"
@@ -1436,6 +1436,9 @@ If set,
14361436
\fRPS1\fR
14371437
will be set to its value for the program being run.
14381438
.TP 17n
1439+
\fRSUDO_TTY\fR
1440+
Set to the terminal device of the user who invoked sudo, if one is present.
1441+
.TP 17n
14391442
\fRSUDO_UID\fR
14401443
Set to the user-ID of the user who invoked sudo.
14411444
.TP 17n

docs/sudo.mdoc.in

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\"
22
.\" SPDX-License-Identifier: ISC
33
.\"
4-
.\" Copyright (c) 1994-1996, 1998-2005, 2007-2024
4+
.\" Copyright (c) 1994-1996, 1998-2005, 2007-2025
55
.\" Todd C. Miller <[email protected]>
66
.\"
77
.\" Permission to use, copy, modify, and distribute this software for any
@@ -24,7 +24,7 @@
2424
.nr BA @BAMAN@
2525
.nr LC @LCMAN@
2626
.nr PS @PSMAN@
27-
.Dd December 20, 2024
27+
.Dd June 7, 2025
2828
.Dt SUDO @mansectsu@
2929
.Os Sudo @PACKAGE_VERSION@
3030
.Sh NAME
@@ -1355,6 +1355,8 @@ option was specified.
13551355
If set,
13561356
.Ev PS1
13571357
will be set to its value for the program being run.
1358+
.It Ev SUDO_TTY
1359+
Set to the terminal device of the user who invoked sudo, if one is present.
13581360
.It Ev SUDO_UID
13591361
Set to the user-ID of the user who invoked sudo.
13601362
.It Ev SUDO_USER

plugins/sudoers/env.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,13 +1123,15 @@ rebuild_env(const struct sudoers_context *ctx)
11231123
CHECK_SETENV2("SUDO_COMMAND", ctx->user.cmnd, true, true);
11241124
}
11251125

1126-
/* Add the SUDO_{USER,UID,GID,HOME} environment variables. */
1126+
/* Add the SUDO_{USER,UID,GID,HOME,TTY,TTY} environment variables. */
11271127
CHECK_SETENV2("SUDO_USER", ctx->user.name, true, true);
11281128
(void)snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) ctx->user.uid);
11291129
CHECK_SETENV2("SUDO_UID", idbuf, true, true);
11301130
(void)snprintf(idbuf, sizeof(idbuf), "%u", (unsigned int) ctx->user.gid);
11311131
CHECK_SETENV2("SUDO_GID", idbuf, true, true);
11321132
CHECK_SETENV2("SUDO_HOME", ctx->user.pw->pw_dir, true, true);
1133+
if (ctx->user.ttypath != NULL)
1134+
CHECK_SETENV2("SUDO_TTY", ctx->user.ttypath, true, true);
11331135

11341136
debug_return_bool(true);
11351137

0 commit comments

Comments
 (0)