Is it possible to get the output of only sudo command?
up vote
0
down vote
favorite
I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.
[sudo] password for user:
If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?
sudo io-redirection
add a comment |
up vote
0
down vote
favorite
I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.
[sudo] password for user:
If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?
sudo io-redirection
For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt fromsudo
– roaima
Dec 3 at 21:22
@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.
[sudo] password for user:
If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?
sudo io-redirection
I want to write the output of only sudo command to some file i.e. the line which I get on terminal after writing sudo i.e.
[sudo] password for user:
If I use sudo ls > file or sudo ls 2> file then I get the stdout and stderr respectively of the whole command. Also I can't use sudo > file or sudo 2> file, then I will get error that I should use some options or command after sudo. So, is it possible to achieve?
sudo io-redirection
sudo io-redirection
edited Dec 4 at 11:37
asked Dec 3 at 16:32
Debian_yadav
1,3603922
1,3603922
For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt fromsudo
– roaima
Dec 3 at 21:22
@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36
add a comment |
For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt fromsudo
– roaima
Dec 3 at 21:22
@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36
For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from
sudo– roaima
Dec 3 at 21:22
For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from
sudo– roaima
Dec 3 at 21:22
@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36
@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.
Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:
sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr
This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.
Note that there are likely subtle security concerns with the above command.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485723%2fis-it-possible-to-get-the-output-of-only-sudo-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.
Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:
sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr
This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.
Note that there are likely subtle security concerns with the above command.
add a comment |
up vote
3
down vote
accepted
With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.
Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:
sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr
This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.
Note that there are likely subtle security concerns with the above command.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.
Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:
sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr
This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.
Note that there are likely subtle security concerns with the above command.
With sudo -S, the sudo utility will write its prompt to standard error, and will read the password from standard input. You can capture the output from standard error with 2>filename.
Note that you still have to supply a command for sudo to execute and that the standard error of this command would also be redirected to the file, unless you do some file descriptor juggling:
sudo -C 4 -S sh -c 'ls non-existant 2>&3' 3>&2 2>sudo.stderr
This would redirect the output of sudo to sudo.stderr while still allowing the executed command to write to the original standard error stream. We do this by opening file descriptor 3 as a copy of standard error, and then explicitly writing to this file descriptor in our command. For this to work, we have to ask sudo to not close file descriptor 3 (which it would do by default). This may require that an administrator has enabled the closefrom_override option in the sudoers file.
Note that there are likely subtle security concerns with the above command.
edited Dec 3 at 17:13
answered Dec 3 at 17:01
Kusalananda
120k16225367
120k16225367
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485723%2fis-it-possible-to-get-the-output-of-only-sudo-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
For the avoidance of XY Problem solving, it seems prudent to ask why you'd want to capture this prompt from
sudo– roaima
Dec 3 at 21:22
@roaima, that was just for knowledge.
– Debian_yadav
Dec 4 at 11:36