0

TL;DR

Can you lock the copy-paste input into terminal while sudo is prompting for a password?

More info

Sometimes you want to copy and paste a long series of commands into the terminal and have them run sequentially. If sudo is part of any of the commands it prevents said command to run.

An example of this would be installing Spotify using the terminal commands provided on their website:

# 1. Add the Spotify repository signing key to be able to verify downloaded packages
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys BBEBDCB318AD50EC6865090613B00F1FD2C19886

# 2. Add the Spotify repository
echo deb http://repository.spotify.com stable non-free | sudo tee /etc/apt/sources.list.d/spotify.list

# 3. Update list of available packages
sudo apt-get update

# 4. Install Spotify
sudo apt-get install spotify-client

Copy-pasting this into the terminal yields the following result:

user:~$ # 1. Add the Spotify repository signing key to be able to verify downloaded packages
user:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys BBEBDCB318AD50EC6865090613B00F1FD2C19886
[sudo] password for user: 
Sorry, try again.
[sudo] password for user: 

What happens is that the terminal interprets the line "# 2. Add the Spotify repository" as the provided password (or possibly even "" as a password as there's a line break between the sudo command and that line), locks for a few seconds while the copy-paste is still being input to the terminal, and then unlocks again for a second atempt at the password.

This is bad if you have a lot of commands you want to run in series, since then some of the non-sudo commands will be run while the sudo commands are denied.

A quick workaround to fix this is to manually write a nonsense command first, like $ sudo whatever , then enter the password, then paste the command series, like so:

user:~$ sudo whatever
[sudo] password for user: 
sudo: whatever: command not found
user:~$ # 1. Add the Spotify repository signing key to be able to verify downloaded packages
user:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys BBEBDCB318AD50EC6865090613B00F1FD2C19886
Executing: /tmp/tmp.JQ3ntmdbnL/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80 
--recv-keys
BBEBDCB318AD50EC6865090613B00F1FD2C19886
...
...
...

This works since the terminal instance will not prompt for a password again.

Is there a smarter way to do this? Can you i.e. lock the copy-paste input until a valid password has been provided?

  • 1
    If you just want to get sudo permissions for subsequent commands, you can use sudo -v instead of sudo whatever. It caches sudo credentials without running a command. – wjandrea Jul 25 '17 at 18:03
2

Yes, there are easy ways to do this.

E.g. if you want to copy-paste this into terminal:

sudo echo 1
echo 2
echo 3

Normally this happens:

$ sudo echo 1
[sudo] password for wja: 
Sorry, try again.
[sudo] password for wja: 
1

Instead, you can use braces:

$ {
> sudo echo 1
> echo 2
> echo 3
> }
[sudo] password for wja: 
1
2
3

Or you can use bash with a herestring:

$ bash << EOF
> sudo echo 1
> echo 2
> echo 3
> EOF
[sudo] password for wja: 
1
2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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