Tải bản đầy đủ - 0 (trang)
Hack 43. Make Laptop Settings Roam with Your Network

Hack 43. Make Laptop Settings Roam with Your Network

Tải bản đầy đủ - 0trang

walkyouthroughsomecommonconfigurationschemes.



Installlaptop-net

Thefirststepistoinstalllaptop-net.Usingyourpreferred

packagemanagerinstallthelaptop-netandlaptop-net-doc

packages.Onceeverythingisinstalled,youwillwantto

reconfigurelaptop-net:

$sudodpkg-reconfigurelaptop-net





Now,bydefault,laptop-netcandoanumberofnicethingssuch

asdetectcableinsertionandautomaticallybringupandtake

downinterfaces.We'venoticed,however,thattheseautomatic

functionscansometimesinterferewiththewayUbuntuwants

toconfigurethenetworkespeciallyifyouareusingapackage

likeNetworkManagersoyouneedtodisablesomeofthis

functionalityinlaptop-net.

Asthelaptop-netconfigurationprogramruns,besuretotellit

thatyouaregoingtouseDHCPforyournetworkandthatyour

EthernetadapterdoesnotsupportMII,evenifitdoes.

Sincelaptop-netwon'tautomaticallyengagewhenevery

interfaceisbroughtup,youmustaddaspecialscriptto

/etc/dhcp3/dhclient-exit-hooks.d.Scriptsinthisdirectoryare

executedwhenchangesoccurindhclient(specifically,whenthe

/etc/dhcp3/dhclient-scriptrunsandexecutestheexit_with_hooks

function).Namethefollowingfile/etc/dhcp3/dhclient-exithooks.d/laptop-net:

if[$reason='BOUND'];then

/usr/share/laptop-net/profile-change$interface



fi

if[$reason='RELEASE'];then

/usr/share/laptop-net/profile-change$interfacedown

fi



Thisscriptwilltelllaptop-nettochangetheprofilebasedonthe

currentinterfacewhenanewinterfacegetsalease,andwhen

theDHCPleaseisreleased,itwilltakethatinterfacedown.

Wenoticedabuginourversionofthepackagewherethe

/var/run/laptop-netdirectorywasnotcreated,soprofiles

weren'tbeingchanged.Tofixthis,weaddedanextralineto

/etc/init.d/laptop-net.Locatethefollowingsectioninthefile:

case"${1}"in

("start")

[$#-eq1]||usage

clean_state

start_ifd

;;



andchangeitto:

case"${1}"in

("start")

[$#-eq1]||usage

mkdir-p/var/run/laptop-net

clean_state

start_ifd

;;



Nowrestartlaptop-net:

$sudo/etc/init.d/laptop-netrestart





Configurelaptop-netProfiles

laptop-netdoesmostofitsconfigurationworkthroughdifferent

profilesthatyouneedtoconfigure.Theseprofilesresidewithin

the/etc/laptop-net/profilesdirectoryandcontainthe

configurationinformationforspecificmodesandnetworksthat

youmightwanttoconnectto.The/etc/laptop-net/profiles

directorywillendupcontainingonedirectoryforeachnew

profileyouwanttoconfigure.Alaptop-netprofileitselfcan

containanumberofdifferentconfigurationfilesandscripts

(check/usr/share/laptop-net-doc/formoredetails),buthereis

alistofthemainfiles:



patterns

ThisfilecontainsalistofIPorstatepatternstomatch.If

laptop-netmatchesapatterninthisfile,itwillthenselect

thisparticularprofile.



files.d/

Thisdirectorycontainsasubsetofyourrootfilesystem.

Whenthisprofileisselected,laptop-netwillcopyanyfilesit

findsheretothecorrespondingsystemdirectory,

overwritingexistingfileswiththesamename.Forinstance,



ifyouaddafileinfiles.d/etc/fstab,whenthisprofileis

selected,thatfilewillbecopiedoverthetopofthesystem

/etc/fstab.



after-select

Thisisanoptionalshellscriptthatlaptop-netwillexecute

afteraprofilehasbeenselected.Ifthereareanyfancy

tweaksorservicerestartsthatyouwanttomake,youcan

puttheminthisfile.



before-deselect

Thisscriptisalsooptionalandisliketheafter-selectscript,

onlyitisexecutedbeforeaparticularprofileisdeselected.

Youmightusethisscripttoundoparticularchangesyou

havemadetoasystembeforethenetworkinterfaceis

takendown.

Nomatterwhatnetworksyouwanttoconfigureforyourself,

thereareacoupleofdefaultprofilesyoushouldsetupfirst:

oneforwhenthenetworkisofflineandoneforwhenyouareon

someunknownnetwork.



Configuretheofflineprofile

Toconfiguretheofflineprofile,createanewdirectorycalled

offlinein/etc/laptop-net/profilesandchangetothatdirectory:

greenfly@ubuntu:~$sudomkdir/etc/laptop-net/profiles/offline

greenfly@ubuntu:~$cd/etc/laptop-net/profiles/offline

greenfly@ubuntu:/etc/laptop-net/profiles/offline$



Nowcreateafileinthisdirectorycalledpatternsandhaveit

containthefollowinglines:

down

unknown



Next,createafiles.ddirectoryhere.Wheneveryouchange

systemfileswithotherprofiles,it'sagoodideatostorethe

originaldefaultversionofthefileintheofflineprofile;thatway,

everythingwillgobacktonormalwhenthenetworkisoffline,

andyouwon'triskoverwritingafileforgood:



greenfly@ubuntu:/etc/laptop-net/profiles/offline$sudomkdirfi





Thenextstepistocreateafilecalledafter-selectandmakeit

executable:

/etc/laptop-net/profiles/offline$sudotouchafter-select

/etc/laptop-net/profiles/offline$sudochmoda+xafter-select





Thisfilewillrunanyprogramsorrestartanyscriptsthatyou

tellitto,butfornowyoucanhaveitdosomethingbasictogive

youdiagnosticinformationwhenitisselected.Addthefollowing

linestothefile:

#!/bin/sh



echo"offline">/tmp/laptop-net-state



Configurethedefaultnetworkprofile

Thenextstepistocreatea"catch-all"defaultnetworkprofile.

Thisprofilewillexecuteanynetwork-basedoperationsyouwant

toperformwheneveryouhaveanetworkconnectionbutdon't

quiteknowwhichnetworkyouareconnectedto.First,create

theprofiledirectoryandnameitzzz-default.Thereasonyou

startwiththezzzisthatlaptop-netgoesthroughtheprofiles

sequentially.SincethisprofilewillmatchallIPaddresses,you

wanttogiveyourotherprofilesachancetobeselectedfirst:



greenfly@ubuntu:/etc/laptop-net/profiles/$sudomkdirzzz-defau

greenfly@ubuntu:/etc/laptop-net/profiles/$cdzzz-default

greenfly@ubuntu:/etc/laptop-net/profiles/zzz-default$



Nowcreatethesamefiles.ddirectoryandpatternsfile.Ifthere

areanyconfigurationsettingsyouwanttoalwayshave

wheneveryouareconnectedtoastrangenetwork,youcan

copythemtothefiles.ddirectory:



greenfly@ubuntu:/etc/laptop-net/profiles/zzz-default$sudomkdi

greenfly@ubuntu:/etc/laptop-net/profiles/zzz-default$sudotouc





Placethefollowinglineinthepatternsfile:

*.*.*.*



ThislinewillmatchanypossibleIPaddress.

Thefinalstepistocreatetheafter-selectscriptthatisrun

whenthisprofileisselectedandtoputthefollowinglinesinit

fordiagnostics:

#!/bin/sh

echo"zzz-default">/tmp/laptop-net-state



Don'tforgettorun:

$sudochmoda+xafter-select





toensurethescriptisexecutable.



Onethingweliketodowhenourzzz-defaultprofilesareselectedisto

throwupaveryrestrictivefirewall.Sincewe'reconnectedtoa

presumablyunknownnetwork,thisensuresthatalltheshieldsareup.

Youcouldalsoperformanumberofothersecurity-tighteningmeasures

inthisscript.



Testyourconfiguration

Withthisnewconfigurationinplace,restartlaptop-net:



$sudo/etc/init.d/laptop-netrestart





NowclickSystem Administration Networkinganddeactivate

yourEthernetconnection(orwirelessifthatiswhatyouare

using).Nowcheck/tmp/laptop-net-state:

$cat/tmp/laptop-net-state

offline



Nowactivateyournetworkconnectionandcheckagainonceit

comesup:

$cat/tmp/laptop-net-state

zzz-default



Youcanalsoverifywhatlaptop-netisdoingbyexaminingthe

syslogfile.laptop-netwilllogtherewhenitchangesnetwork

settings,soyoucangrepthefileforlaptop-net,whichwillshow

youonlyitsloginformation:



$greplaptop-net/var/log/syslog

...

Mar416:12:56ubuntulaptop-net:Selectingnetworkprofile"z

Mar415:19:42ubuntulaptop-net:Deselectingnetworkprofile

Mar415:19:42ubuntulaptop-net:Selectingnetworkprofile"o

Mar415:24:06ubuntulaptop-net:Deselectingnetworkprofile

Mar415:24:07ubuntulaptop-net:Selectingnetworkprofile"z



CreatePersonallaptop-netProfiles

Onceyouhaveconfirmedlaptop-netisfunctioning,it'stimeto

createspecificprofilesfornetworksyoucommonlyconnectto.

Let'sassumeyouhavetwonetworks,oneforhomeandonefor

work.Tomakethingssimple,youcanjustcopyoverthezzzdefaultprofileanduseitasabasetemplatefornew

configurations:



greenfly@ubuntu:~$cd/etc/laptop-net/profiles/

greenfly@ubuntu:/etc/laptop-net/profiles/$sudocp-azzz-defau

greenfly@ubuntu:/etc/laptop-net/profiles/$sudocp-azzz-defau





Configurethepatternsfile

Thefirstthingyouwillneedtochangeisthepatternsfilefor

eachofthesenewprofiles.Let'sassumeyourhomenetwork

assignsyouanIPaddresssomewhereinthe192.168.0.1

network,nomatterwhetheritisonyourEthernetorwireless

card,andyourworknetworkalwaysassignsyou10.1.1.50on

yourEthernetcardand10.1.1.100onyourwirelesscard.You

wouldtheneditthepatternsfileinyourhomeprofileandmake

itcontainonlythefollowingline:

192.168.0.*



ThispatternwillmatchanyIPfrom192.168.0.1to

192.168.0.255.Next,youeditthepatternsfileinyourwork

profileandmakeitcontaintwolines:



10.1.1.50

10.1.1.100



Whenyoucan,trytobeasexplicitaspossiblewhendefining

patterns.That192.168.0.*patternwillmatchagreatnumberof

homenetworks,soyoumightendupsettingyourhomeprofile

whenyouconnectatafriend'shouse.Ideally,youwilltrytoset

upyournetworkssothatyougetassignedoneortwostaticIP

addresses.



Configuretheafter-selectscript

Oncethepatternfileissetup,edittheafter-selectscriptin

boththehomeandworkprofiles.Changethediagnosticlinein

thatscriptfrom:

echo"zzz-default">/tmp/laptop-net-state



to:

echo"home">/tmp/laptop-net-state



and:

echo"work">/tmp/laptop-net-state



inyourhomeandworkscripts,respectively.Nowaddany

particularprogramsyouwanttorunorotherthingsyouwould



liketodointheseafter-selectfiles.

Forinstance,ifyouwantyourdefaultprintertobesettoyour

homeprinterwhenyouareathomeandyourworkprinter

whenyouareatwork,configureyourprintersettingstheway

youwantthemforhome,andthencopythe

/etc/cups/printers.conffileto/etc/laptopnet/profiles/home/files.d/etc/cups/printers.conf(youwillneed

tocreatetheetcandetc/cupsdirectorieswithinthefiles.d

directory).Next,configuretheprintersettingstosuityourwork

environmentandcopythe/etc/cups/printers.confto

/etc/laptop-net/profiles/work/files.d/etc/cups/printers.conf.

Finally,intheafter-selectscriptsforbothhomeandwork

profiles,addtheline:

/etc/init.d/cupsysrestart



torefreshthesettingswhenthatprofileisselected.

Repeatthisprocessforanyothersettingsyouwanttochange.

Copytherelevantsettingstothecorrespondingdirectoryunder

files.d,andifachangerequirestheservicetoberestarted,add

thecorrespondingcommandtotheendofyourafter-select

script.

Formoreinformationaboutconfigurationoptionsinlaptop-net,

checkoutthedocumentationthatwasinstalledunder

/usr/share/doc/laptop-net-doc/.







Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 43. Make Laptop Settings Roam with Your Network

Tải bản đầy đủ ngay(0 tr)

×