Dynamic-IP-Hacks Mini-HOWTO moderated by Michael Driscoll Version 0.4 Created 4/22. Last updated 6/1. 0.0 Table of Contents 1.0 Legalities, Definitions, &c. 1.1 Where to get the latest version of this HOWTO 1.2 Feedback 1.3 Why this HOWTO? 1.4 What is dynamic IP addressing and why do I have to put up with it? 1.5 Can you help me set up {pppd,telnetd,sendmail,&c.}? 1.6 Hey, what about SLIP? 1.7 Copyright 1.8 A Warning 2.0 Hack #1: Keeping your system up and accessible 24/7 2.1 What you'll need 2.2 Setting up crontab 2.2.1 Explanation of the crontab entry 2.3 ip-up and ip-down 2.3.1 Setting up your ip-up and ip-down 2.4 How to find out your new IP address 2.4.1 Telnetting to a POP3 server 3.0 Hack #2: Dynamically changing /etc/hosts 3.1 What you'll need 3.2 More fun with ip-up and ip-down! 4.0 We need more hacks! 5.0 CREDITS 1.0 Legalities, Definitions, &c. Section 1.2: Where to get the latest version of this HOWTO The latest version of the Dynamic-IP-Hacks HOWTO can be found at sunsite.unc.edu under the directory /pub/Linux/docs/howto/mini. Section 1.3: Feedback Any feedback concerning this HOWTO should be addressed to Michael Driscoll . Section 1.1: Why this HOWTO? The problem is, Linux just isn't as happy as it could be with dynamic IP addressing. It's the Unix equivalent of waking every morning to find that your postal address has changed and that all of your stationary is out of date. Unfortunately, ISP's are moving more and more towards this kind of addressing these days, and anyone who can't shell out more bucks for a stable IP address is just kinda stuck with this. The purpose of this HOWTO, therefore, is to make your Linux box happier and more comfortable with dynamic IP addressing, which in turn should make you a bit happier and more comfortable. Section 1.2: What is dynamic IP addressing and why do I have to put up with it? First of all, I suppose I should explain just what an IP address is :) An IP address is a set of four numbers, each from 0 to 255, separated by a dot. An example would be 10.127.96.1. Each computer on the Internet has a unique IP address. The human-readable addresses that you probably use, like sunsite.unc.edu and bak2.lightspeed.net, are just semi-arbitrary names that are translated into their IP addresses by a DNS server whenever you try to access them. Dynamic IP addressing is something used by an ISP to cut down on the number of IP addresses that they need to "own". The way it works is, when you dial up your ISP, they simply give you the next IP number in their queue. This way, they don't need an IP address for every single customer they have, they just need one for every customer that might be online at any given time. Usually this is only about a tenth of their customer base at any given time. Why do they do this? Well, there just aren't enough IP addresses out there to give to every single customer of every single ISP on the planet, especially now that every computer user and his dog is getting connected. Besides, it doesn't affect Windows users at all, so why should they care? :) Therefore, it seems that unless you want to shell out some more money for a stable IP you should just try to make do with what you have. Section 1.3: Can you help me set up {pppd,telnetd,sendmail,&c.}? No. Figure it out yourself. Builds character :) Actually, it would be pretty pointless for me to reinvent the wheel by helping you set these up, as many documents already exist to help you out with these. Start by trying these: man pppd man chat man in.telnetd man inetd PPP-HOWTO NET-2-HOWTO NAG (Network Administrator's Guide by Olaf Kirch, available at fine LDP sites everywhere!) Section 1.4: Hey, what about SLIP? To tell you the truth, I didn't write about it here because I don't know it. Luckily, this shouldn't be too much of a problem as PPP seems to be the emerging standard, especially for dynamic IP addressing. Sorry if you're that 2% out there that has dynamic IP addressing with SLIP, but maybe you can glean a bit out of this HOWTO and make your own setup (if you do then be sure to see Section 4 to get your hack included in this HOWTO!) Section 1.5: Copyright Unless otherwise stated, Linux HOWTO documents are copyrighted by their respective authors. Linux HOWTO documents may be reproduced and distributed in whole or in part, in any medium physical or electronic, as long as this copyright notice is retained on all copies. Commercial redistribution is allowed and encouraged; however, the author would like to be notified of any such distributions. All translations, derivative works, or aggregate works incorporating any Linux HOWTO documents must be covered under this copyright notice. That is, you may not produce a derivative work from a HOWTO and impose additional restrictions on its distribution. Exceptions to these rules may be granted under certain conditions; please contact the Linux HOWTO coordinator at the address given below. In short, we wish to promote dissemination of this information through as many channels as possible. However, we do wish to retain copyright on the HOWTO documents, and would like to be notified of any plans to redistribute the HOWTOs. If you have questions, please contact Greg Hankins, the Linux HOWTO coordinator, at gregh@sunsite.unc.edu via email. Section 1.6: A Warning This should go without saying, but I should say it anyways to cover things. I'm not sure all of this is the canonical way to do things, and if something I've done is just too weird let me know how to fix it. There might be security risks in all of this, but I don't know of them yet. This works on my machine, and I don't think it'll break yours too badly :) Either way, you should take care when doing this stuff and make sure you understand at least a bit about what this stuff is doing so you can fix it if it goes haywire. Happy now? :) ***************************** *The good stuff starts here!* ***************************** 2.0 Hack #1: Keeping your system up and accessible 24/7 This hack involves a problem I have with my ISP. For reasons unknown to me (or apparently even to them) they like to hang up on me every five minutes to five hours. This was a problem for me because I wanted to be able to show off by telnetting to my Linux box from the computer in math class :) While it was easy enough to use a cron job to check the network status every few minutes and start pppd if it wasn't up, since my ISP uses dynamic addressing I couldn't find my address after this happened (except by luck). This hack keeps your PPP session up and puts your new IP number in an accessible place. Section 2.1: What you'll need Not too much. A working PPP setup. A working sendmail (actually it just has to be working to the point where it can send a message to your ISP's mail server). A working telnetd/ftpd/inetd/whatever setup so you can actually get into your computer from somewhere else. A working cron. Section 2.2: Setting up crontab cron is a daemon that starts programs at specified times. You can look at your crontab by running "crontab -e". We'll be using crontab to set up your constant PPP connection. Run crontab -e as root and add this line: #*/2 * * * * ping -c1 your_nameserver 2>&1 | grep "0 packets" >/dev/null && { /usr/sbin/ppp-off >/dev/null ; kill -9 `/sbin/pidof pppd 2>/dev/null` ; sleep 2 ; /usr/sbin/pppd } What this line does is ping your ISP's name server every two minutes, and restarts your connection if it doesn't get a responce. Hint1: This is all one line, don't break it into three! Hint2: Those ">/dev/null"s and "2>&1"s prevent crond from mailing error and status messages to root. You can take them all out for debugging purposes if you're having trouble. Hint3: Check those paths! Hint5: I use your ISP's DNS server because I figure that if that's down, your net connection is screwed anyways :) Hint6: Is this all mud to you? No prob, just check out "man crontab" and the next subsection to see why I do what I do. Hint7: Be sure to use your DNS server's numeric IP address, otherwise ping returns a different message. Now whenever you want your connection to stay up, you can just run crontab -e and take out that "#"...and when you want your connection to stay down, run crontab -e and put the "#" back in, then kill your connection with ppp-off or whatever it is that you use. I know there are other ways then this to keep a connection up (specifically I know that there is a script for pppd, and also a daemon called pppupd that will do it) but I have not tried them. If you use them and think them worthwhile, then drop me a note and I'll try them out. Section 2.2.1: Explanation of the crontab entry Let's look at that line in its component pieces: Part 1: ping -c1 your_nameserver 2>&1 | grep "0 packets" > /dev/null This bit pings the address of your ISP's name server once. If the connection is down, then ping returns a status message that includes "0 packets received", which is what we're grepping for. Part 2: { /usr/sbin/ppp-off >/dev/null ; kill -9 `/sbin/pidof pppd 2> /dev/null` ; sleep 2 ; /usr/sbin/pppd } What this part does is kill the ppp-connection (ppp-off), kill the pppd itself in case that didn't happen before for some reason (kill -9 `/sbin/ pidof pppd 2> /dev/null`), wait for two seconds to make sure everything is dead, then start pppd again. For ppp-off and pppd substitute whatever you use to stop and start your ppp connection. Section 2.3: ip-up and ip-down /etc/ppp/ip-up and /etc/ppp/ip-down are rather useful scripts for us. They are executed when pppd goes up/down and therefore are good for events that need to occur at this time. Mine look like this: ip-up: >#!/bin/sh >echo -n ^G > /dev/console >echo $4 > /etc/add >mail -s "New IP address" fenris@lightspeed.net < /etc/add >chmod 644 /etc/add >echo "127.0.0.1 localhost" > /etc/hosts >echo $4 ulfheim.lightspeed.net ulfheim >> /etc/hosts ip-down: >#!/bin/sh >echo -n ^G > /dev/console >rm /etc/add >echo 127.0.0.1 ulfheim.lightspeed.net localhost ulfheim > /etc/hosts Don't worry if you don't get all of this, we'll be explaining most of it right now. Section 2.3.1: Setting up your ip-up and ip-down The second part of this hack involves making your new IP address available to you from practically any machine on the net. To achieve this we do the following: Create a file as root called /etc/ppp/ip-up. Put this in it: #!/bin/sh # $4 is our ip address in these two scripts (convenient eh?) # /etc/add will hold our address echo $4 > /etc/add # mails our address to our ISP's mail server, with the subject "new ip # address" mail -s "New IP address" yourname@your_internet_address < /etc/add Then create /etc/ppp/ip-down and put this in it: #!/bin/sh rm /etc/add Then run "chmod 700 /etc/ppp/ip-up /etc/ppp/ip-down" to make them executable. Now edit your rc startup scripts and put in a "rm /etc/add" anywhere you feel is appropriate (just in case your power goes down while your connection is up, not really necessary, but I like to be somewhat complete about these things :) I put it in /etc/rc.d/rc.S, which should be okay. Voila! Now every time pppd is started your address will be mailed to your ISP's mail server. We retreive it in the next section. Section 2.4: How to find out your new IP address Well, this calls for some resourcefulness on your part. I really can't walk you through it, as it depends on which machine you're trying to get it from. In Windows, you can try to set up an email program to do retrieve mail from your ISP's mail server, and if you're in Unix see if there's a program called "popclient" or somesuch. If all else fails, you can always use telnet (see the next section). Section 2.4.1: Telnetting to a POP3 server This is how I retrieve my IP address, as I am usually showing off when I do all of this anyways, and it really confuses all of the GUI users I show it to :) Hopefully your ISP uses a POP3 server for mail (most likely), otherwise you'll just have to figure this out yourself by looking up the RFC for the protocol you need and figure out how to do it by telnet. Anyways, first you want to telnet to port 110 of your ISP's mail server. In Unix you do this with "telnet your_isp's_mail_server 110", on a VAX you might need to do "telnet your_isp's_mail_server/port=110", and in a Web browser you should use "telnet://your_isp's_mail_server:110". Hopefully you can figure it out. Once you are connected, you should see something like this: >Connected to new-ls.lightspeed.net. >Escape character is '^]'. >+OK QUALCOMM Pop server derived from UCB (version 2.1.4-R3) at >new-ls.lightspeed.net starting. type "user your_username" to login. >+OK Password required for fenris. now type "pass your_password" >+OK fenris has 2 message(s) (3030 octets). type "list" to look at a list of your messages. >+OK 2 messages (3030 octets) >1 2400 >2 630 >. See that message with size 630? That's my IP address! How do I know? Because it's always that size :) Now type "retr message_number" to retrieve the message you want. >+OK 630 octets >Received: from ulfheim.lightspeed.net (root@bak2-pp-ls.lightspeed.net >[204.216.66.74]) by new-ls.lightspeed.net (8.6.12/8.6.12) with ESMTP id >TAA12048 for ; Mon, 22 Apr 1996 19:15:37 -0700 >Received: (from root@localhost) by ulfheim.lightspeed.net (8.7/8.6.9) >TAA00594 for fenris@lightspeed.net; Mon, 22 Apr 1996 19:15:29 >Date: Mon, 22 Apr 1996 19:15:29 -0700 >From: Deus In Machina >Message-Id: <199604230215.TAA00594@ulfheim.lightspeed.net> >To: fenris@lightspeed.net >Subject: New IP address > >204.216.66.74 > >. And there it is! Use "dele message_number" to get rid of it or just "quit" to leave it there and quit. Now just telnet to your machine at that address and have fun! If any of this is just not working, then check out RFC 1225, which describes the POP3 protocol in full detail. 3.0 Hack #2: Dynamically changing /etc/hosts As I said before, Linux isn't completely happy with dynamic IP addressing. For example, sometimes talkd won't work with kludgy values in /etc/hosts. However, the following hack makes up for a lot of that by changing /etc/hosts according to the IP address we receive when we call up our ISP. For me this fixed problems with "hostname" and "talk". Section 3.1: What you'll need. Nothing but a working PPP setup, really. Section 3.2: More fun with ip-up and ip-down This hack is really very easy, and only involves three files, ip-up, ip-down, and an rc file (I put it in rc.S). First, make a backup of /etc/hosts, just in case this screws up anything. Add the following lines to /etc/ppp/ip-up: # echo 127.0.0.1 localhost > /etc/hosts echo $4 yourmachine.yourdomain yourmachine >> /etc/hosts # EXAMPLE: My machine is named "ulfheim" and its domain is "lightspeed.net". The lines in my ip-up say: # echo 127.0.0.1 localhost > /etc/hosts echo $4 ulfheim.lightspeed.net ulfheim >> /etc/hosts # Then add the following lines to /etc/ppp/ip-down: # echo 127.0.0.1 yourmachine.yourdomain localhost yourmachine > /etc/hosts # EXAMPLE: My machine is named "ulfheim" and its domain is "lightspeed.net". The lines in my ip-down say: # echo 127.0.0.1 ulfheim.lightspeed.net localhost ulfheim > /etc/hosts # Now put the following in an rc script (I chose rc.S, it seems ok there). # echo 127.0.0.1 yourmachine.yourdomain localhost yourmachine > /etc/hosts # EXAMPLE: My machine is named "ulfheim" and its domain is "lightspeed.net". The lines at the end of my /etc/rc.d/rc.S say: # echo 127.0.0.1 ulfheim.lightspeed.net localhost ulfheim > /etc/hosts # This last one is in case your power goes out or something while your connection was still up. 4.0 We need more hacks! If you have a Dynamic IP hack that you would like to contribute to this HOWTO, then let me know. Be warned that if you do so that it will have to be covered under the copyright notice in Section 1.5, for reasons of CD publishing and whatnot. If you {liked this HOWTO, didn't like this HOWTO, thought this HOWTO was too confusing, found something in this HOWTO that doesn't work and/or is just plain wrong, want to send me email, don't want to send me email, found a security problem in this howto, etc} then please mail me at . I mean it, I really would appreciate *any* feedback on this HOWTO, even if you just mailed me to say that you read it! Really! Just e-mail me on anything! I *really do* love getting e-mail! Flames will *not* go to /dev/null. I think if you feel that strongly about it, I should listen. If you write up a chapter for this HOWTO not only will you be helping the Linux community, but you will receive full credit for your hack, your name will go into the credits at the end of this HOWTO, you will be acknowledged as a contributor in the beginning of this HOWTO, your idea will be archived with almost every CD Linux distribution in the world, you'll be the envy of all of your peers, etc. 5.0 CREDITS "Michael Driscoll" is the HOWTO moderator and the author of hacks one and two. "Christian G. Warden" helped debug the crontab line used in hack one, now the entry will work in (hopefully) all cases, including those when the connection goes down but pppd does not. YOUR NAME IN LIGHTS HERE! Just send me a hack to put in this mini-HOWTO!