- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- bash script to setup a route
Operating System - Linux
1820069
Members
2459
Online
109608
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-10-2011 01:22 AM
тАО05-10-2011 01:22 AM
hi
I need a bash script running with root rights to setup a route, for example:
0.0.0.0 0.0.0.0 192.168.0.1
It should run on linux as a startup application.
THX
I need a bash script running with root rights to setup a route, for example:
0.0.0.0 0.0.0.0 192.168.0.1
It should run on linux as a startup application.
THX
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-10-2011 02:21 AM
тАО05-10-2011 02:21 AM
Solution
Most Linux distributions include configuration files you can use to specify custom routes. You probably don't need a script. (If such a configuration file is available, other sysadmins will expect to find custom routes there, not in arbitrary custom scripts. Being purposefully obscure only imperils your hard-earned vacation time.)
For example, on RedHat Enterprise Linux, if you need custom routes for e.g. network interface eth0, you can create a file named /etc/sysconfig/network-scripts/route-eth0 and write your route definition to it.
Please see:
http://www.cyberciti.biz/tips/configuring-static-routes-in-debian-or-red-hat-linux-systems.html
Furthermore, your example route has network 0/netmask 0, which means it's a default gateway route. All Linux distributions have a way to specify a default gateway without any need of custom scripting.
Even if you for some reason need a script, the script would just be an one-line "route" or "ip" command:
route add default gw 192.168.0.1
or
ip route add default via 192.168.0.1
The standard place for adding custom one-line commands to system startup would be the "rc.local" file, usually located in either /etc/rc.local or /etc/init.d/rc.local. It is automatically run as root at system startup.
MK
For example, on RedHat Enterprise Linux, if you need custom routes for e.g. network interface eth0, you can create a file named /etc/sysconfig/network-scripts/route-eth0 and write your route definition to it.
Please see:
http://www.cyberciti.biz/tips/configuring-static-routes-in-debian-or-red-hat-linux-systems.html
Furthermore, your example route has network 0/netmask 0, which means it's a default gateway route. All Linux distributions have a way to specify a default gateway without any need of custom scripting.
Even if you for some reason need a script, the script would just be an one-line "route" or "ip" command:
route add default gw 192.168.0.1
or
ip route add default via 192.168.0.1
The standard place for adding custom one-line commands to system startup would be the "rc.local" file, usually located in either /etc/rc.local or /etc/init.d/rc.local. It is automatically run as root at system startup.
MK
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-10-2011 08:43 AM
тАО05-10-2011 08:43 AM
Re: bash script to setup a route
Pls let explain our problem.
In the branch office we have 2 breakouts: 1 standard over MPLS and other one, just temporaily for some tests over DSL.
The default (standard) gateway for our branch clients is MPLS router and the standard internet breakout is normally over the same MPLS router.
We have ordered now DSL, just for some tests.
BTW It's quite difficult to change routings at MPLS or DSL router by the providers.
We try to change routings on the linux clients.
I've add these lines in: /etc/sudoers:
sta ALL=(root)NOPASSWD:/home/user/route.sh
sta ALL=(root)NOPASSWD:/sbin/route
and I've created this script, not tested yet:
---------------------------------------------------------
#!/bin/bash
# del existing default gw (MPLS router)
sudo route del default
# set DSL router as default gw
sudo route add default gw 192.168.0.1
# set route for the internal MPLS network
sudo route add 10.10.0.0 netmask 255.255.0.0 gw 10.10.50.1 dev eth0
------------------------------------------------------------------------------------------
Perhaps I should create another script to change everything back.
Both scripts should be run by a normal non-root user.
In the branch office we have 2 breakouts: 1 standard over MPLS and other one, just temporaily for some tests over DSL.
The default (standard) gateway for our branch clients is MPLS router and the standard internet breakout is normally over the same MPLS router.
We have ordered now DSL, just for some tests.
BTW It's quite difficult to change routings at MPLS or DSL router by the providers.
We try to change routings on the linux clients.
I've add these lines in: /etc/sudoers:
sta ALL=(root)NOPASSWD:/home/user/route.sh
sta ALL=(root)NOPASSWD:/sbin/route
and I've created this script, not tested yet:
---------------------------------------------------------
#!/bin/bash
# del existing default gw (MPLS router)
sudo route del default
# set DSL router as default gw
sudo route add default gw 192.168.0.1
# set route for the internal MPLS network
sudo route add 10.10.0.0 netmask 255.255.0.0 gw 10.10.50.1 dev eth0
------------------------------------------------------------------------------------------
Perhaps I should create another script to change everything back.
Both scripts should be run by a normal non-root user.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP