Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to do the following but it's not quite right.

cat ~/Desktop/blah.png | ssh -t -t trusktr@ "cat | sudo -i 'cat | /path/to/blah.png'"

I have my public ssh key in the trusktr@ user's .ssh folder, so no initial password is needed. When the sudo -i command finally starts, it prompts me for the root password, but immediately fails as if I had pressed enter but I didn't even touch the keyboard. I have a feeling maybe the cat command is being catted into the password prompt. The output looks like this:

trusktr@LENOVO-PC ~
$ cat ~/Desktop/win-8.1-missing-lockscreen-setting.png | ssh -t -t trusktr@ "cat | sudo -i 'cat > /srv/http/default/htdocs/~/img/win-8.1-missing-lockscreen-setting.png'"
tcgetattr: Not a character device
[sudo] password for root:
You type like i drive.
[sudo] password for root:
Your mind just hasn't been the same since the electro-shock, has it?
[sudo] password for root:
Maybe if you used more than just two fingers...
sudo: 3 incorrect password attempts
Connection to closed.

I don't have direct ssh access through the root user, only through a normal user with access to sudo using the root password.

How might I cat the image through ssh, through the non-root user, into the root location all in a single command?

share|improve this question
up vote 0 down vote accepted

It does not work because you are cating blah.png into sudo, which is not a valid password.

This should combine the needed commands onto one line:

scp ~/Desktop/blah.png trusktr@ && ssh -t -t trusktr@ "sudo cp ~/blah.png /path/to/blah.png && rm ~/blah.png"
share|improve this answer
Does it "press enter" when a new line character is catted? How might I achieve what I'm trying to do in a single command? – trusktr Jan 28 '14 at 4:28
@trusktr, yes, a newline is the same thing as enter, and you don't do it in a single command. – psusi Jan 28 '14 at 21:02
Thanks for the input. I'm not entirely convinced there isn't a way to do it though. – trusktr Jan 29 '14 at 5:16
@trusktr, you can't do it with a single command, but you can combine the commands into a single pipeline using the && operator. – psusi Jan 29 '14 at 14:09
Oh, yeah, a one liner is what I meant. Could you give more details on how I'd change my command? – trusktr Jan 30 '14 at 3:58

Option #1: scp the file to your account, then ssh in and put the file in place from the terminal.

Option #2: if your sudo caches auth, login and sudo to root before attempting the complex copy. (the second sudo shouldn't need a password)

(also, sudo uses your password, not root's)

share|improve this answer
Thanks. Yeah, I ended up doing the first option. My setup only caches during the session, so logging out destroys it. sudo can be configured to use the root password instead of the user's password by putting Defaults rootpw in /etc/sudoers. – trusktr Jan 25 '14 at 10:41
That defeats the purpose of sudo... let people without the rootpw run commands as root. – Ricky Beam Jan 25 '14 at 21:31
Depends on the use case. I'm the only user, so it's more secure this way because an adversary now has to guess two passwords instead of just one. – trusktr Jan 26 '14 at 8:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.