LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 01-19-2006, 01:46 AM   #1
fdahl_009
Member
 
Registered: Oct 2005
Location: Norway
Posts: 148

Rep: Reputation: 16
bootscript?


Hi! Want to let the commands below to be run at boot. What do I do to get it work?

su
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Thanks for any help..
 
Old 01-19-2006, 02:21 AM   #2
kadhiravan.r
Member
 
Registered: Nov 2004
Location: india/tamil nadu/chennai
Distribution: Linux 8.0
Posts: 37

Rep: Reputation: 15
check this method

Put those lines in .bash_profile file of the users to whome u want it to run.


If u want it to run in every user put it in the /etc/rc/d/rc5.d/S99local file.
You can also put it in the

/etc/rc/d/rc3.d/S99local file. So that it will be excuted in runlevel 3 or 5 at every time you login the pc.




Ok..

check it ..



bye
 
Old 01-19-2006, 04:35 AM   #3
fdahl_009
Member
 
Registered: Oct 2005
Location: Norway
Posts: 148

Original Poster
Rep: Reputation: 16
ok, but any way to get this procedure automated?

tried sudo echo "1"> ......
but that gives me an error permission denied
because of this I have to su, and login as root to do this

is there a boot file/script that can do this right before it
enters the loginscreen, so that no user have to login to
get the command running / typing it? (it is on a server)

and possibly automated, so when I boot the server it is ready to go..
 
Old 01-19-2006, 05:31 AM   #4
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The idea of having either or both of these commands put into a login script is definitely wrong. The idea of putting these commands into a boot script is definitely correct.

Are you sure that the setting in /proc/sys/net/ipv4/ip_forward needs to be set every time the system starts? I believe that this setting will keep its value between reboots without any help. If it does need to be set every time that you start the system then you may find that your distribution has a system configuration file that will take care of this. For example the ip forwarding command that you want to execute is done in my SuSE 9.2 system in the file /etc/init.d/boot.ipconfig. Here is the pertinent code in that file.
Code:
   #
   # Enable IP forwarding ?
   #
   if test -e /proc/sys/net/ipv4/ip_forward -a -n "$IP_FORWARD" ; then
    case $IP_FORWARD in
      yes)
        echo -n "Enabling IP forwarding"
        echo "1" > /proc/sys/net/ipv4/ip_forward
      ;;
      *)
        echo -n "Disabling IP forwarding"
        echo "0" > /proc/sys/net/ipv4/ip_forward
      ;;
    esac
    rc_status -v -r
   fi
   #
In my SuSE 9.2 the IP_FORWARD variable is set in the /etc/sysconfig/sysctl file.

This could be done very differently in different distributions.


The same thing is true of the iptables. I know that my Debian Sarge installation did not automatically configure a firewall but my SuSE 9.2 installation did automatically set up a firewall and it automatically starts whenever the system starts. You should find out if the distribution that you are using for this already has an iptables setup routine and add your NAT configuration command to that. If you don't already have an iptables configuration in your system startup then you would want to have it start after your network is running. You could create a file called iptables-nat in your /etc/init.d directory containing your command. Then in each of the rc2.d, rc3.d, and rc5.d you create a link to that iptables-nat file with the correct name (S??iptables-nat). For example if you already have a link in your rc5.d directory named S11network then you would create a link in the same directory that points to your /etc/init.d/iptables-nat file. That link could be named S12iptables-nat.

Post any questions. Keep in mind that different distributions do some things differently but other things are pretty much the same in any Linux distribution. Startup files are in /etc/init.d. The startup rc?.d directories are either in /etc or in /etc/init.d. Some distributions use an rc.local while others don't. I'm just trying to say that you may have to do a little looking around your system startup to find the way that your system does the things that I mentioned.

Always make a copy of a system startup file before you edit it.

Last edited by stress_junkie; 01-19-2006 at 05:47 AM.
 
Old 01-19-2006, 06:35 AM   #5
fdahl_009
Member
 
Registered: Oct 2005
Location: Norway
Posts: 148

Original Poster
Rep: Reputation: 16
thanks for the help, will try to look around a bit and test some solutions.. may take some time, but thanks again for such a "deep" explanation!

"Are you sure that the setting in /proc/sys/net/ipv4/ip_forward needs to be set every time the system starts?" -When restaring the computer I have to echo 1 to the ip_forward and do iptables command..

Last edited by fdahl_009; 01-19-2006 at 06:37 AM.
 
Old 01-19-2006, 06:46 AM   #6
fdahl_009
Member
 
Registered: Oct 2005
Location: Norway
Posts: 148

Original Poster
Rep: Reputation: 16
ok, found this in /etc/init.d/networking; isn't this telling the computer to put 1 in ip_forward?

ip_forward () {
if [ -e /proc/sys/net/ipv4/ip_forward ]; then
if [ "$VERBOSE" != no ]; then
log_begin_msg "Enabling packet forwarding..."
echo 1 > /proc/sys/net/ipv4/ip_forward
log_end_msg $?
else
echo 1 > /proc/sys/net/ipv4/ip_forward
fi
fi
}
 
Old 01-19-2006, 06:58 AM   #7
fdahl_009
Member
 
Registered: Oct 2005
Location: Norway
Posts: 148

Original Poster
Rep: Reputation: 16
restarted server now, puts "0" in ip_forward, and I also need to do the iptables command!
 
Old 01-19-2006, 08:25 AM   #8
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by fdahl_009
ok, found this in /etc/init.d/networking; isn't this telling the computer to put 1 in ip_forward?
Yes it is but it is implemented as a function. Some other line will have to call this function or it will not execute.
Code:
ip_forward () {
if [ -e /proc/sys/net/ipv4/ip_forward ]; then
 if [ "$VERBOSE" != no ]; then
  log_begin_msg "Enabling packet forwarding..."
  echo 1 > /proc/sys/net/ipv4/ip_forward
  log_end_msg $?
 else
  echo 1 > /proc/sys/net/ipv4/ip_forward
 fi
fi
}
Code:
if [ -e /proc/sys/net/ipv4/ip_forward ]; then
Here the code tests the existence of the file /proc/sys/net/ipv4/ip_forward. If this file doesn't already exist then it will not turn ip forwarding on.

Code:
if [ "$VERBOSE" != no ]; then
At this point in the code ip forwarding will be turned on. This just tests to see if a log file entry should be made.

Code:
echo 1 > /proc/sys/net/ipv4/ip_forward
This turns ip forwarding on. Notice that the echo command does not have quotes around the 1. The code in my SuSE machine had quotes around the 1. I don't know if this is important.

So one or more of three things could be happening on your system. Either
1) the /proc/sys/net/ipv4/ip_forward file doesn't exist when the system starts or
2) this function isn't being called or
3) the lack of quotes around the 1 is incorrect.

You could rewrite the function as follows to see if it is being called and if it is trying to set a 1 in the ip_forward file. Make a backup copy of your original startup file before you make any edits. This change would only be temporary.

Code:
ip_forward () {
log_begin_msg "Entering ip_forward function"
log_end_msg
if [ -e /proc/sys/net/ipv4/ip_forward ]; then
 if [ "yes" != no ]; then
  log_begin_msg "Enabling packet forwarding..."
  echo 1 > /proc/sys/net/ipv4/ip_forward
  log_end_msg $?
 else
  echo 1 > /proc/sys/net/ipv4/ip_forward
 fi
fi
}
The changes simply attempt to make an entry to the boot log file when the function is called and to use the verbose code regardless of the value of VERBOSE, wherever that is set.

Note that I've never seen or used the log_begin_msg or log_end_msg feature so I could be introducing an error.

When the system starts you should see the message that we added at the beginning of the function when you look at dmesg or in the /var/log/boot.msg file. If you don't see the message that we added at the beginning of the function then it (probably) is not being called. (Or I messed up with the log_begin_msg).

If you do see that message then you should also see a message saying that ip_forwarding is being enabled. If not then the /proc/sys/net/ipv4/ip_forward file doesn't exist. The fix for that would be to remove the test for the existence of that file.

If you do see the message indicating that ip forwarding is being enabled then the echo command has a problem. In that case I would try putting quotes around the 1.

Or you could just change the code as follows:
Code:
ip_forward () {
log_begin_msg "Enabling packet forwarding..."
echo 1 > /proc/sys/net/ipv4/ip_forward
log_end_msg $?
}
In this form the only question is whether the function gets called. If it does get called then it will work. I didn't put quotes around the 1 because I didn't know if it would cause a problem in the middle of the log_begin_msg ... log_end_msg. You could even eliminate that question with this rewrite.
Code:
ip_forward () {
echo "1" > /proc/sys/net/ipv4/ip_forward
}

Last edited by stress_junkie; 01-19-2006 at 08:48 AM.
 
Old 01-19-2006, 12:01 PM   #9
fdahl_009
Member
 
Registered: Oct 2005
Location: Norway
Posts: 148

Original Poster
Rep: Reputation: 16
thanks, will try that. But I can't be the only one who needs this to work? Someone else must have configured ubuntu as an server and needed this? Someone must know ubuntu good enough that they can tell me exactly what to do!?
 
Old 01-19-2006, 01:11 PM   #10
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
If you want to know the real answer then RTFM.
 
Old 01-19-2006, 04:26 PM   #11
fdahl_009
Member
 
Registered: Oct 2005
Location: Norway
Posts: 148

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by stress_junkie
If you want to know the real answer then RTFM.
And that means?
 
Old 02-18-2006, 10:04 AM   #12
dmoore
LQ Newbie
 
Registered: Aug 2004
Location: Columbus, OH
Distribution: Ubuntu, FC3
Posts: 1

Rep: Reputation: 0
Use the options file

Another way to specify ip_forward at boot in Ubuntu 5.10 is to edit the file /etc/network/options
and change ip_forward=no to ip_forward=yes
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a bootscript? artofluke Fedora 4 01-13-2006 05:26 AM
Creating bootscript rael_kid Linux - Newbie 6 03-26-2005 01:47 PM
LVM Bootscript Problems kvanblijderveen Mandriva 0 07-07-2004 06:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration