Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Updated version of switchmods
# This is a script to automate the switch from r8169 to r8168.
# Jameson Williams
# Taras Taday
# Apr 15, 2012: Updated code to work with Ubuntu 12.04
# May 16, 2008: Corrected a typo on line 43 which prevented the r8169
# module from being renamed. Thanks to
# for spotting it.
# Jun 1, 2008: Added a bunch of checks to beef up the script a bit.
# Removed all reliance upon the internet, as it is more than
# likely the case that this script is being run without
# connectivity. Added better output in case of failures.
# Jun 2, 2008: Simplified many of the if statements to simpler checks.
# Added a few more, and created the die() function.
# Jun 4, 2008: Moved the stuff to removfe r8169 to the beginning. This
# makes the depmod command behave correctly, finally! Also fixed
# the insmod check so that it no longer incorrectly dies. (Good
# news!) Updated the die command to delete rogue $TMP folders in
# case the script dies before completion. Also added more output
# to help with debugging.
# Aug 13, 2008: Updated the code to work with version 8.008.00 of the
# Realtek driver. The new code no longer needs to be patched,
# which was sort of the *magic* of the previous versions of this
# script. Still a useful quick-fix script, though.
# Nov 7, 2008: Updated package for 8.009.00
# Aug 17, 2009: Updated package for 8.013.00, added a .ko, etc.
# Oct 18, 2009: Updated package for, including a patch for
# kernels >= 2.6.31.
# Oct 26, 2011: Changed so that it works for linux 3.0 (Ahtenus)
# setup a temporary directory, and save some environmental infos
RUNPATH=$(dirname $0)
TMP=$(mktemp -d)
MODULEPATH=/lib/modules/`uname -r`/kernel/drivers/net/ethernet/realtek
function die {
# $1 is an error message to print to stderr.
echo $1 1>&2;
if [[ -d $TMP ]]; then
rm -rf $TMP2
if [[ -f $MODULEPATH/r8169.ko.bak ]] && \
[[ ! -f $MODULEPATH/r8169.ko ]] && \
[[ $(id -u) -eq 0 ]]; then
cp $MODULEPATH/r8169.ko.bak $MODULEPATH/r8169.ko
exit 1
# check to make sure that this is running as root!
[[ $(id -u) -eq 0 ]] || die "You must run this script as root."
echo "Attempting to remove running r8168 and r8169 modules if loaded..."
[[ $(lsmod | grep -c r8169) -eq 0 ]] || rmmod r8169
[[ $(lsmod | grep -c r8168) -eq 0 ]] || rmmod r8168
echo "Attempting to move $MODULEPATH/r8169.ko to $MODULEPATH/r8169.ko.bak."
[[ ! -f $MODULEPATH/r8169.ko ]] || \
mv $MODULEPATH/r8169.ko $MODULEPATH/r8169.ko.bak
echo "Blacklisting r8169 in /etc/modprobe.d/blacklist..."
if [[ $(grep -c "blacklist r8169" /etc/modprobe.d/blacklist.conf) -lt 1 ]]; then
echo -e "\nblacklist r8169\n" >> /etc/modprobe.d/blacklist.conf
echo "Creating a tmp dir in which to build the module..."
cd $TMP
tar xjf "$DRVFILE"
cd ./${VERSION}/src
echo -n "Checking for gcc and linux-headers-$(uname -r)..."
[[ $(dpkg -l | grep -c linux-headers-$(uname -r )) -gt 0 ]] || \
die "You need to install package linux-headers-$(uname -r )."
[[ $(dpkg -l | grep -c gcc) -gt 0 ]] || \
die "You need to install the GNU C Compiler."
echo -e "\tOK"
echo -n "Attempting to build the module..."
cd ..
make clean
make modules
make install # this copies the module to the appropriate /lib/modules
# dir.
} &>/dev/null
[[ -f $MODULEPATH/r8168.ko ]] || die "Could not build module."
echo -e "\tOK"
rm -r $TMP
echo -n "Inserting the module into running kernel code..."
insmod $MODULEPATH/r8168.ko
[[ $? ]] || die "Failed to insert module."
echo -e "\tOK"
update-initramfs -u
echo -n "All done! You should now have a "
echo "working wired ethernet connection, persistant at boot."
exit 0

Hi gist,

I'm trying out your script here, I got into trouble with "You need to install package linux-headers-". I'm a newbie and such a lazy ass :) I'll try to google it though after this, but just wondering if that could be automated to into the script :D



just realized that was a stupid suggestion hahaha!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.