Right after my login session, I would like Ubuntu to execute sudo apt-get update & apt-get dist-upgrade , systematically / automatically. The terminal must be opened so that I can see the output of each of these executions. In particular, I want to be asked to type "y/n" due to the command apt-get dist-upgrade. I don't want to have to type my password : thus, Ubuntu must use it as input of the sudo execution instead of me.

The modifications involved must be independant of any upgrade and migration of Ubuntu.


Which bash file should I edit and in which way ?

  • "In particular, I want to be asked to type "y/n" due to the command apt-get dist-upgrade" there is a command for that called "yes" in case you want it to automate the y. yes | apt dist-upgradde . Besides that: I consider this a bad way of doing this. – Rinzwind Aug 18 '18 at 17:48
  • DId you ever get this working? I have tested it on my machine... it's a nice little feature and actually gives more info and control than the auto-check process that would run anyway. – Joshua Besneatte Aug 19 '18 at 18:51
  • 1
    also, I am surprised at the down votes here... how is this any different (other than being way cooler) than having automatic updates enabled???? – Joshua Besneatte Aug 19 '18 at 18:55

Sudo without password

You can tell sudo to let you execute commands without a password.

per the most upvoted answer here

Open terminal window and type:

sudo visudo

In the bottom of the file, type the follow:


You can also make it only allow apt to work without a password for just one user, and just for apt by adding this instead of the above:

username ALL = NOPASSWD: /usr/bin/apt

replacing "username" with your username. You may have to reboot for these changes to take effect.

I would highly recommend using the single user/app solution as this will be much more secure than allowing complete use off sudo without a password for all sudoers... This could be a SERIOUS security issue, esspecially if someone gained physical access to your computer. In general, if this computer contains sensitive data, you will want to forgo any kind of passwordless sudoing.

Run command on startup

  • GUi: In activities window search for "Startup" and run "Startup Applications" click the add button and create with the following command


  • Command Line: add following command to the end of ~/.profile

The following command will open a terminal and run update:

gnome-terminal -- bash -c "sudo apt update && sudo apt dist-upgrade; read line"

Name and comment (gui) as you wish and save it. Now when you log in, a terminal will open and run your upgrade commands. You will not need to enter a password, but you will have to confirm the upgrade.

The window will then stay open until you press enter.

Now that you have this, you might want to go to Software & Updates and disable automatic checking for updates since this new script does it for you.

enter image description here

  • 1
    added per request... please feel free to improve it as you see fit! – Joshua Besneatte Aug 24 '18 at 4:40

This is easy enough, although I like to know what updates are applied so I prefer having the final say manually.

Anyway, the essential bit you need to know is how to run a sudo command from a script. That can be done using:

echo password | sudo -S

or with the longer explicit but equivalent version

echo password | sudo -u root --stdin

where password is your password, of course and the argument to -u is the user which is root in this case.

Then, note that your .profile bash script is run on logins. So in short, append the following commands to your .profile:

echo password | sudo -S apt-get update
echo password | sudo -S apt-get -y upgrade

where the -y flag to apt-get means answer yes to all questions. Your .profile should have been created when your account was made, but in case it's been deleted, you can copy the default from /etc/skel/.profile.

Finally, for security, you will probably want to chmod 600 .profile to protect your password.

  • 1
    Instead of putting a cleartext password into a shell script I'd recommend to add a file to /etc/sudoers.d which allows the user to issue sudo apt-get update without a password. – PerlDuck Aug 18 '18 at 15:19
  • 1
    On your question above: if you login to your machine interactively (e.g. using ssh), the .profile will be sourced and you will see the output in your terminal. However, GDM will also source .profile at startup, so you will not see the results (not sure if that output will appear in the system journal, you would need to check). If you want to see output in some convenient location, you could redirect stdout and stderr into a file in the usual way. – Martin W Aug 18 '18 at 15:50
  • 2
    Do not put dist-upgrade into a script. That is asking for trouble. – Martin W Aug 18 '18 at 15:51
  • 1
    Remember to disable or uninstall unattended-upgrade to prevent lock conflicts and confusion. – user535733 Aug 18 '18 at 15:55
  • 1
    @PerlDuck: here's another solution: use a systemd oneshot user script. It would start on login and exit after logout. – Martin W Aug 18 '18 at 19:00

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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