1

I am making a script that will update my system (and do some other things, but that doesn't matter). I want the script to run sudo apt-get update and sudo apt-get dist-upgrade --yes automatically. I use --yes so I won't have to choose yes when it runs; it upgrades all packages automatically without me having to do anything. That said, I want to put my password in the script somehow so I won't have to type in my password when I run it either, probably with an argument or something. You know, something I can put at the end like sudo apt-get update --passwd MYPASSWORDHERE. I have tried that and it doesn't work, but that's an example of what I want. What argument (--passwd or --password or something) do I put at the end to do this?

John Scott
  • 1,472

3 Answers3

5

If your script runs as "yourUser", you could create a simple file:

  sudo visudo -f /etc/sudoers.d/myOverrides 

with this directive:

  yourUser ALL = NOPASSWD:/usr/bin/apt-get

You can find an useful explanation here.

Lety
  • 6,089
  • 2
  • 32
  • 38
2

Sudo doesn't behave that way, for security reasons. You can echo the password to sudo using the -s option, but I don't suggest it. Even if you protect your script from other users, they can still see your parameters using e.g. ps -ef.

I think your problem is better solved by installing the unattended-upgrades package.

Sophit
  • 306
  • 1
  • 8
-1

I wanted to update/upgrade my Ubuntu system on startup without any input from myself. As I am the sole user of my desktop, I don't have the security concerns system administrators do. I used

echo "password" | sudo -S apt-get update && sudo apt-get upgrade -y && sudo apt autoremove -y
Starship
  • 151