On Wed, 14 Nov 2007, in the Usenet newsgroup comp.os.linux.networking, in
article <XhL_i.23202$(E-Mail Removed)>, Tomás Ó hÉilidheÿÿ wrote:
>(While I'm an experienced PC user, I've only started using Linux in the
>last two weeks (Ubuntu to be exact), so forgive me if I'm making some
>school-boy errors here)
Don't worry about it - that's how you learn
>I have a laptop that I connect to many different networks. I've been
>trying to write simple scripts so that I can effortlessly set my
>network settings as I move from network to network.
There are many ways to do this. We usually don't use DHCP (one
possibility - it depends on access to a DHCP server on all nets that
are set up properly for "that" network), but our systems are usually
moved when powered off. Thus, we have a boot variable that selects
which of the network configuration files to use. A bit complex.
Another poster has suggested the package called "ifscheme".
>The scripts don't work however, and I don't know why. Maybe one of
>you can tell me what I'm doing wrong. First of all, I created a file
>called "VPN at work"
Suggestion: While spaces in a file name are legal (the only illegal
characters are the directory separator and newline), having such
spaces makes you jump through extra hoops - quoting the filename
EVERY time it's used. Using an underscore (_) as a word separator
might make life easier.
>and put the following in it:
>
>ifconfig eth0 down
>cp "resolv_VPN at work" /etc/resolv.conf
>ifconfig eth0 arp -promisc netmask 255.255.255.0 10.0.1.5 up
The 'arp' and '-promisc' should not be needed.
>In the same folder, I have a file called "resolv_VPN at work" which
>contains the following:
>
>nameserver 10.0.0.1
>nameserver 10.0.0.2
OK - I can see one problem.
>When I run the script, it changes the network settings perfectly, but
>it doesn't change the DNS servers for me. While it _does_ copy over the
>resolv.conf,
and that's all it takes - but...
>it doesn't apply the changes. How can I apply the changes? Or if there's
>anything else I'm doing wrong then please let me know.
Let's have a look at the output of '/sbin/route -n' which is going to
show the kernel routing table. I suspect what you will see is
[example ~]$ /sbin/route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 1 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
[example ~]$
and what this says is "there is a route to 10.0.1.0 - 10.0.1.255, and
they are directly connected to the eth0 interface". It also says "there
is a route to 127.0.0.0 - 127.255.255.255, and they are directly
connected to the lo interface". So a question - how to you get to those
DNS servers... they're on a different network, and "you can't get there
from here".
How to fix? You need to add a line to your script to add a route using
some gateway that will forward your packets to the network where the
name servers are located. The syntax depends on the layout of the
network. If FOR EXAMPLE there is a router on your network with the
address 10.0.1.254 and it knows how to forward packets to that other
network which is using a 255.255.255.0 mask, the syntax would be
/sbin/route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.0.1.254 eth0
and that would add a line to the routing table above that looks like
10.0.0.0 10.0.1.254 255.255.255.0 UG 0 0 1 eth0
Another possibility is that the router can forward packets to the
every other network in the 10.0.0.0 - 10.255.255.255 range. The syntax
would be the same EXCEPT that the netmask changes on this line to
/sbin/route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.0.1.254 eth0
The way routing works is that the most definitive route is used. Thus
a packet to 10.0.1.53 would be sent locally even though this new route
covers all addresses beginning with 10.x.x.x.
A third possibility is that the router can forward packets to the
entire world. Only in this case should you use the "default" syntax
where the word 'default' means "if nothing else works, use this". The
command would be
/sbin/route add default gw 10.0.1.254 eth0
and the resulting routing table would look like
[example ~]$ /sbin/route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.1.0 0.0.0.0 255.255.255.0 U 0 0 1 eth0
127.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 lo
0.0.0.0 10.0.1.254 0.0.0.0 UG 0 0 0 eth0
[example ~]$
>I come from Microsoft Windows so I'm not very knowledgeable on how the
>Linux file system works with permissions and so forth. I followed a
>tutorial online that told me I should write the script and then do the
>following:
>
>chmod +x scriptname
>
>As far as I know, this marks the file as "executable".
Yes - the permissions on the file would then look like
-rwxr-xr-x 1 root root 121 Nov 15 12:30 scriptname
>If anyone could point me to a not-dumbed-down tutorial on how the Linux
>file system works, I'd be appreciative.
Hmmm.... some documents at the Linux Documentation Project
http://tldp.org/guides.html would be
* Introduction to Linux - A Hands on Guide
* The Linux System Administrators' Guide
* The Linux Users' Guide
and a HOWTO that should be on your system (try /usr/share/HOWTO/)
71626 Apr 4 2004 Unix-and-Internet-Fundamentals-HOWTO
which is also at
http://ibiblio.org/pub/linux/docs/HOWTO/ if you can't
find it on the system. There are around 450 HOWTOs and mini-howtos, as
well as 35 LDP guides.
>It'd be handy if Linux had a command for setting DNS servers:
>
>dns-set clear
>dns-set 10.0.0.1
>dns-set 10.0.0.2
Actually, your existing script is changing the /etc/resolv.conf file
and that's all that is needed to set/clear the name servers. I suspect
your problem is that you can't reach the name servers - routing issue.
>Maybe something like that would be very easy to code as a script? I used
>to write batch files in MS-DOS, and I'd have written them using "%1" to
>get the command line arguments; is there something like that in Linux?
31540 Jul 27 2000 Bash-Prog-Intro-HOWTO
* Bash Guide for Beginners
* Advanced Bash-Scripting Guide
and the man page for the shell ("man bash"). In the shell script, the
command line variables are "$0" (the command name), "$1" (the first
variable), "$2" (the second, and so on) and "$*" (the entire command
with all options/variables).
Slow down please - this stuff _can_ be horrible complicated if you are
jumping in with your eyes closed. The water is deep here. ;-)
Old guy