Ubuntu – Add and remove update channels in an easy terminal way

aptcommand linedevelopmentscripts

I know there is a GUI in Ubuntu Software & Updates to enable the update channels

  • updates
  • proposed
  • backports
  • security

as shown in this screenshot:

enter image description here

I am looking for an easy way to do this from within a terminal using commands such as

sudo apt-add-update enable updates
sudo apt-add-update enable proposed
sudo apt-add-update enable backports
sudo apt-add-update enable security

sudo apt-add-update disable updates
sudo apt-add-update disable proposed
sudo apt-add-update disable backports
sudo apt-add-update disable security

and an additional thing

sudo apt-add-update enable default

sudo apt-add-update disable default

Some examples for a better understanding

  1. An empty sources.list

    • cat /etc/apt/sources.list

      <empty>
      
    • sudo apt-add-update enable security

      <empty>
      
  2. One enabled repository (main)

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main
      
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main
      deb http://archive.ubuntu.com/ubuntu wily-security main
      
  3. Two or more enabled repositories in one or two lines

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main universe
      

      or

      deb http://archive.ubuntu.com/ubuntu wily main
      deb http://archive.ubuntu.com/ubuntu wily universe
      
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu wily-security main universe
      

      or

      deb http://archive.ubuntu.com/ubuntu wily main
      deb http://archive.ubuntu.com/ubuntu wily-security main
      deb http://archive.ubuntu.com/ubuntu wily universe
      deb http://archive.ubuntu.com/ubuntu wily-security universe
      
  4. With deb-src entries

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb-src http://archive.ubuntu.com/ubuntu wily main universe
      
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb-src http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu wily-security main universe
      deb-src http://archive.ubuntu.com/ubuntu wily-security main universe
      
  5. With inactive deb-src entries

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main universe
      # deb-src http://archive.ubuntu.com/ubuntu wily main universe
      
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main universe
      # deb-src http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu wily-security main universe
      
  6. The default thing

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily-security universe
      
    • sudo apt-add-update enable default

      deb http://archive.ubuntu.com/ubuntu wily universe
      deb http://archive.ubuntu.com/ubuntu wily-security universe
      
  7. Only one entry and the disable action

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily-security universe
      
    • sudo apt-add-update disable security

      <empty>
      
  8. Different or the same servers for different or the samerepositories, respect each server

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily universe
      deb http://us.archive.ubuntu.com/ubuntu wily main
      
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily universe
      deb http://us.archive.ubuntu.com/ubuntu wily main
      deb http://archive.ubuntu.com/ubuntu wily-security universe
      deb http://us.archive.ubuntu.com/ubuntu wily-security main
      
  9. Different Ubuntu releases for different repositories, respect each release

    • cat /etc/apt/sources.list

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu trusty main
      
    • sudo apt-add-update enable security

      deb http://archive.ubuntu.com/ubuntu wily main universe
      deb http://archive.ubuntu.com/ubuntu trusty main
      deb http://archive.ubuntu.com/ubuntu wily-security main universe
      deb http://archive.ubuntu.com/ubuntu trusty-security main
      
  10. PPAs or other package sources (not-Canonical) in the sources.list?

    Ignore!

  11. Don't change the protocols, e.g. https, http, tor, …

Best Answer

This does exactly what has been asked;

I'll update this from time to time if necessary;

The bleeding edge version can be found at this GitHub repository;

To install from the GitHub repository:

  • Install git: sudo apt-get install git
  • Clone the repository: git clone https://github.com/kos0/addRemoveDistribution.git

Synopsis:

enableDisableDistribution.pl <enable|disable> <default|security|updates|proposed|backports>

s1

s2

s3

#!/usr/bin/perl

sub printUsage {
    print("Usage: enableDisableDistribution.pl \e[4maction\e[0m \e[4mdistribution\e[0m\n\n");
    print("\e[4maction\e[0m must be \e[1menable\e[0m or \e[1mdisable\e[0m\n");
    print("\e[4mdistribution\e[0m must be \e[1mdefault\e[0m, \e[1msecurity\e[0m, \e[1mupdates\e[0m, \e[1mproposed\e[0m or \e[1mbackports\e[0m");
    exit(0);
}

sub parse {
    open(my $in, "/etc/apt/sources.list") || die("Couldn't open '/etc/apt/sources.list': $!");
    while(<$in>) {
        my $matchDistribution;
        chomp;
        if(/^deb(-src)? +(.*?).ubuntu.com\/ubuntu\/? +(.*?) +(.*?) *(#.*)?$/) {
            my $debSrc = $1 eq "-src";
            my $URI = $2;
            my @split = split("-", $3);
            my @components = sort(split(" ", $4));
            if(($distribution eq "default" && defined($split[1])) || ($distribution ne "default" && $split[1] ne $distribution)) {
                push(@add, "$debSrc,$URI,$split[0],@components");
            }
            else {
                $matchDistribution = 1;
            }
        }
        (! $matchDistribution && push(@notMatchDistribution, $_)) || push(@matchDistribution, $_);
    }
    close($in);
}

sub update {
    while(1) {
        print("Would you like to update the cache? Y-y/N-n: \n");
        my $update = <STDIN>;
        if($update =~ /^y$/i) {
            my $ret = system("apt-get update");
            exit($ret);
        }
        elsif($update =~ /^n$/i) {
            exit(0);
        }
        else {
            print("Please enter Y-y or N-n.\n");
        }
    }
}

sub rewrite {
    if($action eq "enable") {
        if(@matchDistribution == 0) {
            open(my $out, ">", "/etc/apt/sources.list") || die("Couldn't open '/etc/apt/sources.list': $!");
            foreach(@notMatchDistribution) {
                print $out ($_ . "\n");
            }
            foreach(@add) {
                my @x = split(",");
                my @y = split(" ", $x[3]);
                my $line = sprintf("deb%s $x[1].ubuntu.com/ubuntu $x[2]%s @y", $x[0] && sprintf("-src"), $distribution ne "default" && sprintf("-$distribution"));
                if(! grep(/^$line$/, @added)) {
                    print $out ($line . " #Added by enableDisableDistribution\n");
                    push(@added, $line);
                }
            }
            close($out);
            printf("Added %s %s.\n", scalar(@added), @added == 1 ? sprintf("entry") : sprintf("entries"));
            update;
        }
        else {
            print("$distribution is enabled already. Aborting.\n");
            exit(1);
        }
    }
    else {
        if(@matchDistribution > 0) {
            open(my $out, ">", "/etc/apt/sources.list") || die("Couldn't open '/etc/apt/sources.list': $!");
            foreach my $line (@notMatchDistribution) {
                print $out ($line . "\n");
            }
            close($out);
            printf("Removed %s %s.\n", scalar(@matchDistribution), @matchDistribution == 1 ? sprintf("entry") : sprintf("entries"));
            update;
        }
        else {
            print("$distribution is disabled already. Aborting.\n");
            exit(1);
        }
    }
}

if($> != 0) {
    print("You must be root to run enableDisableDistribution.\n");
    exit(1);
}
if(@ARGV == 2 && $ARGV[0] =~ /^(enable|disable)$/ && $ARGV[1] =~ /^(default|security|updates|proposed|backports)$/) {
    $action = $ARGV[0];
    $distribution = $ARGV[1];
}
else {
    printUsage;
}

parse;
rewrite;

exit(0);