Controlling repos with sed

As a RHEL & Fedora user of Atomic Host, I find myself using package layering on a regular basis. I typically only want a limited selection of repos enabled to keep the metadata transfers to a minimum. This is really simple with the default Fedora repos, but can be trickier with RHEL depending on the subscription that’s attached to the system. Subscription-manager can quickly disable everything and then enable the desired repos, but it’s a slower command to execute as it connects to the CDN. Anyway, I found this sed snippet handy for making this easy and quicker:

Disable everything:

sed -i 's/enabled\ =\ 1/enabled\ =\ 0/g' /etc/yum.repos.d/redhat.repo

Enable the rhel-7-server-rpms repo:

sed -i '/\[rhel-7-server-rpms\]/,/^ *\[/ s/enabled\ =\ 0/enabled\ =\ 1/' /etc/yum.repos.d/redhat.repo

Enable the rhel-7-server-extras-rpms repo:

sed -i '/\[rhel-7-server-extras-rpms\]/,/^ *\[/ s/enabled\ =\ 0/enabled\ =\ 1/' /etc/yum.repos.d/redhat.repo


Fire it all together!

sed -e 's/enabled\ =\ 1/enabled\ =\ 0/g' -e '/\[rhel-7-server-rpms\]/,/^ *\[/ s/enabled\ =\ 0/enabled\ =\ 1/' -e '/\[rhel-7-server-extras-rpms\]/,/^ *\[/ s/enabled\ =\ 0/enabled\ =\ 1/' -i /etc/yum.repos.d/redhat.repo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.