Login or Boot up Command

I am using SuSe Linux 9.0.

For whatever reason I have to set my default gateway by executing, as root, the command

ip route add default via dev wlan0

Question is, where is the best place to put this command so that it gets executed on boot, or, perhaps better, on logon for any user.  Remember, especially on logon for any user, the command must be executed as root.

If more than the command itself has to be added, please include the extra stuff (like switching to root and back).
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

robertjbarkerAuthor Commented:
And if anybody knows how to execute the command on wireless lan (PCMCIA) card insert, that would be even better.
add these to your /etc/inittab.

startup scripts live in /etc/rc.d/init.d, and are activiated by the presence or absence of a symbolic link to that location in /etc/rc.d/rc3.d

man inittab.

when user login then u can put this command to shell profile file.
like if user has cshell put in .cshrc
ksh then in .profile
sh:     .login
bash:     .bshrc

u have to enable sudo program so that user can run command as a root.
sudo ip route add default via dev wlan0

echo 'default wlan0' >>/etc/sysconfig/network/routes

There should be a way to configure it in yast/yast2 too ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

robertjbarkerAuthor Commented:
What seems to have worked is putting the single line

       ip route add default via dev wlan0

in a script in directory "/etc/sysconfig/network/if-up.d".  I chose "wlan0-up" as the name of the script, but I am pretty sure it does not matter what the name of the script is.  I got this idea from reading the file "/etc/sysconfig/network/config" which says that a script in that directory will be executed when an interface comes up.  I suppose that means it will be executed when eth0 comes up also - pretty sure that will not make a difference though.

I wish I knew how to get DHCP working completely with wlan0.  eth0 works fine with the same DHCP server, my wireless router (which includes a few wired ports).  And wlan0 can get an IP, but nothing else.

So, I fill in the domain name servers in the YAST2 network card configuration dialog.  And those take.
I also tried filling in the default gateway and/or a specific default route in the same place.  The route file, /etc/sysconfig/network/routes, was filled in pretty much along the lines of what ahoffmann suggests (I tried filling that file in directly also as suggested) but, very strangely, they did not take.

By "not take" I mean they did not work and the routes did not show up when I executed "route -nN".

I did not go very far with shivsa's suggestion as I wasn't exactly sure where in the sequence to put it.  Also, sudo requires the root password, which I don't like to type into a script.  And when attempting to execute the command in a console, it said it could not find the "ip" command (I was not logged on as route).

Anyway, things work.  But Linux...I hate going to DHCP for a bit, and YAST for a little bit more, and mucking about in this and that rinky dink config file for the last little bit.  What is this? Windows?
> .. very strangely, they did not take
it will be used when initialising the corresponding runlevel, or at boot

Using /etc/sysconfig/network/if-up.d/wlan0-up or /etc/sysconfig/network/routes is rather a matter of your taste, unfortunatelly SuSE has it's own taste too, so take care ..
robertjbarkerAuthor Commented:

This is a PCMCIA card.  I wonder when it becomes active.  Even when I have the card inserted on boot up I see that, when the network interfaces become active, the wlan0 interface is tagged as "hotplug".  And seems to be skipped until later.  Maybe the ".../routes" are ignored because at the time they would be looked at during boot, there is no interface active.

But, they are ignored.  I have tried rebooting after setting the route file, and no dice.

"so take care..." - I would be interested in the details of the caution.
> .. hotplug ..
sorry, I'm not (yet) used to this mechanism at SuSE ..
In this case the wlan0-up script might be the better, and more system-conform, solution
I have SuSE 9.0 installed and have the same problem you have.
I Made a little bash script to set the default gateway:
#! /bin/bash

echo "* Setting up Wireless lan.."
echo "* Releasing current IP"
/bin/ifdown-dhcp wlan0 >/dev/null
echo "* Deleting current route"
ip route flush table main
echo "* Requesting IP for wlan0"
/bin/ifup-dhcp wlan0
echo "* Setting route"
GATE=`/bin/ifstatus-dhcp wlan0|grep GATEWAY|cut -c13-30`
echo "* Setting default gateway to $GATE"
ip route add default via "$GATE" dev wlan0

I made some LN's from /sbin to /bin in order to make the script work also als non-root and asdjusted the /etc/sudoers file:
YourUserName  ALL=NOPASSWD:/etc/init.d/wlanup
and execute script 'sudo yourscriptname'

Please note im beginner to linux and you probably know a more fancy script :)

I also experienced problems with resolving, i managed to solve this by using dhcpcd als dns client (change this in /etc/sysconfig/network/dhcp and set BIN to 'dhcpd')

Please drop a note if this helps you ;)
robertjbarkerAuthor Commented:
I'm putting bets on me being greener in Linux than you, and undoubtedly your stuff will help me understanding a number of things.  Like how dhcp is invoked and how sudo works.  Good things to know.  Thanks.

Although, I'm not so sure about sudo.  And it looks like you set it up so no password is required?  I would think that might be a security risk.

Like I say, for me, putting the line "ip route add default via dev wlan0" in a script in  "/etc/sysconfig/network/if-up.d" seems to complete my routing when I plug my card in with no need of "sudo".
I made this script because its for my laptop and the default gateway is different each time (multiple wireless networks).
Your right about the security thing, but its only possible to execute that certain script with root rights and as long as the scripts had the right chmod i guess its o.k.

Your solution far easyer, im gonne look if its posible to combine our solutions.
Greets ! :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Networking

From novice to tech pro — start learning today.