Hướng dẫn cài bionick v3.0 wordpress

Introduction

You can configure a network interface from the command line. You can configure your network client hosts with the command line by using commands to change your current settings or by editing a number of system files. This has several advantages over network managers in the GUI.

This guide had been developed for Ubuntu and Debian, other distributions can use a similar setup, but be aware that file locations may differ.

Requirements

An editor. This guide will use vi, but you can replace it with any other editor of your liking, gedit, nano, gvim, pico, emacs..

Have a working internet connection during the installation. We need to install packages and those are fetched from the online repositories.

Backup any files we touch. Just easier to recover when you have a backup.

Read this guide in full before actually configuring your network.

Basics

Setting/changing the hostname

The hostname command allows you to directly query, or set, the hostname from the command line.

You can see your current hostname by running hostname. To set the hostname directly you can run hostname newname as root. If you do this however, make sure you change the /etc/hosts file first. This is needed because otherwise you will need to boot into single user mode and change your hostname in /etc/hosts. This is because sudo requires DNS lookups in certain cases and it cannot resolve your newly set hostname.. To circumvent this you need to add the new name to the hosts file prior to setting the hostname and remove it once that action has succeeded.

sudo vi /etc/hosts

127.0.1.1 ubuntu newname

Set the hostname to newname.

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

Now you can remove the old hostname

sudo vi /etc/hosts

127.0.1.1 newname

When your system boots it will automatically read the hostname from the file /etc/hostname. You can add hostname and IP addresses to the file /etc/hosts for static lookups.

Finding your network interface

When setting up your network you will need to know the network interface cards on your computer. The interface name of cards for different vendors may be different, which is why this step is needed.

ls /sys/class/net

or

ip addr

This will list the interface names for all NICs on your computer. It will probably include eth0 (hardwired NIC), lo (loopback interface for the localhost), and something for your wireless card (like wifi0, or wlan0).

Disable network managers and/or wicd

Configuring your network via the CLI will likely interfere with Network Manager or wicd. To counter this, disable the startup script of either application or completely remove the packages. Be aware that the rest of this guide will require packages which need to be installed from the online repositories.

Network manager

Stop the current process

sudo /etc/init.d/NetworkManager stop

Start: sudo /etc/init.d/NetworkManager start

The inofficial way:

sudo chmod -x /etc/init.d/NetworkManager

Reverse: sudo chmod +x /etc/init.d/NetworkManager

The official way:

sudo update-rc.d -f NetworkManager remove

Reverse: sudo update-rc.d -f NetworkManager defaults 50

Or remove the network manager package.

sudo aptitude purge network-manager

Reverse: sudo aptitude install network-manager

wicd

Stop the current process

sudo /etc/init.d/wicd stop

Start: sudo /etc/init.d/wicd start

The inofficial way:

sudo chmod -x /etc/init.d/wicd

Reverse: sudo chmod +x /etc/init.d/wicd

The official way:

sudo update-rc.d -f wicd remove

Reverse: sudo update-rc.d -f wicd defaults 20

Or remove the wicd package.

sudo aptitude purge network-manager

Reverse: sudo aptitude install network-manager

Note: At some point Ubuntu started using upstart instead of the /sbin/init/ daemon. Thus if the first command above does not stop Network Manager, try this.

sudo stop network-manager

Configuring an interface

Word of wisdom

We will use eth0 in this example, your interface can be named differently, see Finding your network interface.

If you have disabled the either wicd or the network manager you probably don’t have a network connection anymore. Connect via a regular UTP cable to your router, and assuming you have DHCP enabled do the following:

sudo ip link set dev eth0 down sudo dhclient eth0

This will bring your eth0 up by using DHCP. Your network is now configured (for the time being).

If you don’t have DHCP enabled configure your network by issueing the commands below, the gateway address is the IP address of your router. And your IP should be in the same range as the router is.

sudo ip addr add 192.168.1.14/24 dev eth0 sudo ip link set dev eth0 up sudo ip route add default via 192.168.1.1

These commands configure your interface but these changes will not survive a reboot, since the information is not stored anyhwere. This is where the interfaces file comes in handy. To configure a interface permanently you’ll need to edit the interfaces file, /etc/network/interfaces.

127.0.1.1 ubuntu newname

0

127.0.1.1 ubuntu newname

1

For these settings to take effect you need to restart your networking services.

127.0.1.1 ubuntu newname

2

Setting up a second IP address or Virtual IP address

If you need to set up a second ip address you need to edit the /etc/network/interfaces.

127.0.1.1 ubuntu newname

0

127.0.1.1 ubuntu newname

4

For these new settings to take effect you need to restart networking services using the following command

127.0.1.1 ubuntu newname

2

Howto set MTU for a connection

You can set the MTU for an interface, you could do this by using the mtu keyword in the interface file or by using the ip link command.

127.0.1.1 ubuntu newname

6

The above example sets the MTU for device eth0 to 1492, the usual MTU for a PPPoE ISP connection. This however is only needed if connections seem to hang otherwise (with the default of 1500).

This tip was found on //glasnost.beeznest.org/articles/290.

Setting up DNS

To cause your machine to consult with a particular server for name lookups you simply add their addresses to /etc/resolv.conf.

For example a machine which should perform lookups from the DNS server at IP address 192.168.1.1 would have a resolv.conf file looking like this

127.0.1.1 ubuntu newname

7

enter the following details

127.0.1.1 ubuntu newname

8

Wireless

We will not use un-encrypted wireless connections, but will make use of WEP/WPA/WPA2. Deviations from security/encryption method is an exercise left for the user. We also assume the network SSID is being broadcast.

Enabling wireless networking on your PC is pretty straight forward. If your network card is supported out of the box, then there are very little issues. If your card is not supported out of the box, you may need to use ndiswrapper, which will require the Windows drivers fo your card.

ndiswrapper

* Install a Windows driver with ndiswrapper for Dapper or ndiswrapper for 8.04 and up

127.0.1.1 ubuntu newname

9 More information about ndiswrapper

//help.ubuntu.com/community/WifiDocs/Driver/Ndiswrapper

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

0

Configure your wireless interface

In the examples I will use wlan0 as your wireless card, this could be different on your machine! I will do the examples with a dynamic IP address (DHCP), the same principles applies to staticly configured IP’s.

Open the interfaces file

127.0.1.1 ubuntu newname

0

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

2

The pre-up command is run before the interface goes up. The post-down command is run after the interface goes down. There are also post-up and pre-down commands. E.g. You could use post-up to mount disks after an interface goes up. In this case we start wpa_supplicant before we enable the interface and we stop wpa_supplicant after we bring the interface down.

WPA supplicant

Before you start

Before proceeding any further, it might be worthwhile to check whether your Wi-Fi Card is supported. Please see the wpa_supplicant website for more details. This will save you lots of time and frustration.

Install wpa_supplicant

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

3

Creating the password

You probably have given your wireless network a name (the ESSID or SSID), in this example we will use ubuntuwifi for our wireless network, our password will be OpenSesame. The SSID and password needs to be configured on your wireless router, how to do that is up to the reader. If you have problems coming up with a password, please have a look here.

If you are using WEP encryption (which is not advised) you may skip this step.

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

4

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

5

Adding your network

Open the /etc/wpa_supplicant file and define your network.

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

6

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

7

Please beware, if you use WPA, remove the WEP section and vice versa.

The priority in this file determines to which network you will connect if more then 2 configured networks are available. This will always be the network with the highest priority.

WPA start script

Create the WPA startup script /etc/init.d/wpa.sh

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

8

sudo hostname newname

or

echo newname | sudo tee /etc/hostname

9

Make the start script executable.

sudo vi /etc/hosts

0

You can now start WPA supplicant as a service or in debugging mode:

sudo vi /etc/hosts

1

Make sure the script gets run on boot and stopped at shutdown

sudo vi /etc/hosts

2

With wpa_cli you can make changes to your wpa_supplicant config file and reload these changes:

sudo vi /etc/hosts

3

Enter help to see what you can do with wpa_cli. Some of the options are: reconfigure, disconnect, reconnect, reassociate. These options speak for themself.

For more information about wpa_supplicant, please have a look at their respective manpages:

sudo vi /etc/hosts

4

Now that you have setup everything, you can try to connect to your network by restarting networking

127.0.1.1 ubuntu newname

2

You could also reboot your machine if you prefer that.

Configure laptops for multiple locations

When you have a laptop you don’t want to configure it to only be able to have a internet connection at only one location. You probably go to work and use a wired connection, you goto to coffeeshop and use the wifi hotspot overthere and at home you have another network configuration. That is exactly what we are going to configure now.

Guessnet, ifplugd and wpa_supplicant

Install guessnet and ifplugd

sudo vi /etc/hosts

6

Configure ifplugd. Define which interfaces need to be hotplug and adjust the arguments. You should remove the -q.

sudo vi /etc/hosts

7

sudo vi /etc/hosts

8

Some cards do not really play well with the defaults of Ubuntu, adjust where needed.

More information for ifplugd

See ifplugd for more information. And their respective man pages

sudo vi /etc/hosts

9

You will need the wpa.sh script mentioned earlier in this guide and you will need to make sure it will be started and stopped:

127.0.1.1 newname

0

Map your physical interfaces to logical interfaces

First, we are going to make a mapping for eth0 and wlan0 and said which mapped interface could be used for each physical interface:

127.0.1.1 newname

1

Normally ifupdown configures the interfaces, but now we’ve told it to use guessnet to determine the network being used.

Define tests to select networks

As you will see with the officelan, homelan and aruba examples below we will execute tests to see wheter a peer is present based on the MAC address. The simplest solution for this is to check whether your gateway is present. You can find out the MAC address of your gateway by running the ip neigh command. You need to be connected with your network for this.

127.0.1.1 newname

2

This my default gateway, has IP address 192.168.1.254 and 00:24:17:47:xx:xx as a MAC address. This gives us the needed information to define the test.

127.0.1.1 newname

3

You don’t need to specify the source IP address, but in certain cases this is required. I always add it so I don’t have to worry about it in case I do need it.

So let’s define our fixed networks using the peer tests:

127.0.1.1 newname

4

For wireless networks you can also use the peer test, but there is a simpler way to see if you can connect to a wireless network. Test to see if the ESSID is present:

127.0.1.1 newname

5 Spaces in the ESSID In older versions of guessnet you didn’t need to use quotes around the ESSID, but later versions do require it.

127.0.1.1 newname

6

To see what kind of other test you can run to determine which network you are in, please have a look at the manpage of guessnet.

127.0.1.1 newname

7

Use of metrics

Some of you might have noticed a metric statement for some of the interfaces. I do this because when you are connected via both wireless and wired networks, some things may go “bad” with the routing table. When you define a metric for these two interfaces you will tell the routing table which interface has preference over the other. The lowest metric has priority over the higher metric.

Or as I explained in a ubuntuforum post: The problem is that without the metric you will have 2 interfaces used for the same destinations. It will use either interface, which causes problems with the routing table.

By setting a metric you can avoid this, since the higher metric is more “expensive” to use. So the OS will use the interfaces with the lowest metric if it needs to route traffic. In case the lower metric interface is shutdown it will use the higher metric interface since it is the only interface which can be used to route traffic towards that particular network/destination.

See also //en.wikipedia.org/wiki/Metrics_%28networking%29

The problem could also be solved by adding static routes, but that is used mostly to make sure specific networks/hosts are routed via a different interface then the default. You then force only those hosts to use link B even though normally the OS would use link A.

Result

When we put all the configuration segments together you will get a interfaces file which looks like this

127.0.1.1 newname

8

You can now test if it all works like intended.

127.0.1.1 newname

9

If you enter ip addr you will only see the lo interface being active.

ls /sys/class/net

or

ip addr

0

And start all the required daemons:

ls /sys/class/net

or

ip addr

1

And now you will see your interfaces configured properly

ls /sys/class/net

or

ip addr

2

Done.

Bonus script

This script will be a stop/start script for your complete network, which can be used when you want to enable networking after you have suspended your laptop or for other reasons

ls /sys/class/net

or

ip addr

3

See also

Ubuntu resources

  • //ubuntuforums.org/showthread.php?t=124153 – This guide on the Ubuntu forums.
  • //ubuntuforums.org/showthread.php?t=1055393 – Setting hostname gone wrong.

Chủ đề