-3

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.

Question

Which bash file should I edit and in which way ?

2 Answers2

2

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:

$USER ALL=(ALL) NOPASSWD: ALL

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

OR

  • 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

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.

Zanna
  • 72,312
Martin W
  • 2,675