Home / Articles / BSD / FreeBSD / Changing Internet configuration from command line in FreeBSD

Changing Internet configuration from command line in FreeBSD

Changing Internet configuration from command line in FreeBSD
 
Changing Internet configuration from command line in FreeBSD
  • Currently 0 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Rating: 0/5 (0 votes cast)

Thank you for rating!

You have already rated this page, you can only rate it once!

Your rating has been changed, thanks for rating!

Log in or create a user account to rate this page.


Imagine a situation when Internet configuration must be changed on the fly which means without rebooting OS or editing configuration files.

The following commands can do this:

ifconfig em0 inet 192.168.1.4 netmask 255.255.255.0
route delete default
route add default 192.168.1.1
echo "nameserver 8.8.8.8" > /etc/resolv.conf

Where:

  • em0 - Ethernet interface with Internet access;
  • 192.168.1.4 - IP address;
  • 192.168.1.1 - default gateway;
  • 8.8.8.8 - DNS server.

But, what if you need to switch between several Internet configurations? For example: you have installed FreeBSD on your laptop and you need to have access to Internet when you bring your laptop to work. It would be difficult to change configuration manually every time you are at work. For this purpose the following script is the best solution.

Create a file switch-internet-configuration with the content:

#!/bin/sh

case $1 in
  "work" )
    echo "Start to switch to '$1' internet configuration"
    ifconfig em0 inet 192.168.1.4 netmask 255.255.255.0
    route delete default
    route add default 192.168.1.1
    echo "nameserver 192.168.1.1" > /etc/resolv.conf
    echo "Switching to '$1' internet configuration is finished"
  ;;
  "home" )
    echo "Start to switch to '$1' internet configuration"
    ifconfig em0 inet 192.168.0.4 netmask 255.255.255.0
    route delete default
    route add default 192.168.0.200
    echo "nameserver 192.168.0.200" > /etc/resolv.conf
    echo "Switching to '$1' internet configuration is finished"
  ;;
  * )
    echo "The internet configuration does not exist"
  ;;
esac

To switch Internet configuration to "home", run this command:

./switch-internet-configuration home

To switch Internet configuration to "work", run this command:

./switch-internet-configuration work

Before you use this script do not forget to change: Ethernet interface, IP address, default gateway and DNS server.


 

Read also

Install MySQL server on FreeBSD

Install MySQL server on FreeBSD

Install Apache in FreeBSD

Install Apache in FreeBSD

Log in or create a user account to post a comment.

easyos.net
117217982212577402640

Quick navigation

General navigation