个人工具

“UbuntuHelp:NetworkConfigurationCommandLine”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/NetworkConfigurationCommandLine}}
 
{{From|https://help.ubuntu.com/community/NetworkConfigurationCommandLine}}
 
{{Languages|UbuntuHelp:NetworkConfigurationCommandLine}}
 
{{Languages|UbuntuHelp:NetworkConfigurationCommandLine}}
=== Howto Configure the Network device ===
+
=== Configure Network Interface Using Command-Line ===
The <code><nowiki>ifconfig</nowiki></code> tool can set the IP-Address of your network device.
+
You can configure a network interface from the command line. You 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.
* <code><nowiki>sudo ifconfig eth0 192.168.0.xxx</nowiki></code> ... or whatever you need as IP-Address.
+
=== Find Network Interface Card ===
The <code><nowiki>iwconfig</nowiki></code> tool is for configuring WLAN devices.
+
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.
Note: These commands will configure your device for the current session, but changes will be lost when you reboot.
+
<pre><nowiki>
=== Howto Permanently Configure the Network device ===
+
ls /sys/class/net
To permanently configure your network devices you need to edit the /etc/network/interfaces file.
+
</nowiki></pre>
(Type 'man 5 interfaces' to see the manual for the available commands).
+
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).
 +
=== Configuring Static IP Address For Your Network Card ===
 +
Configure a  Static IP address by editing /etc/network/interfaces. Replace eth0 with your network interface card (see Find Network Interface Card).
 +
<pre><nowiki>
 +
sudo nano /etc/network/interfaces
 +
</nowiki></pre>
 +
<pre><nowiki>
 +
# The primary network interface
 +
auto eth0
 +
iface eth0 inet static
 +
address 192.168.2.33
 +
gateway 192.168.2.1
 +
netmask 255.255.255.0
 +
network 192.168.2.0
 +
broadcast 192.168.2.255
 +
</nowiki></pre>
 +
For these settings to take effect you need to restart your networking services.
 +
<pre><nowiki>
 +
sudo /etc/init.d/networking restart
 +
</nowiki></pre>
 +
=== Setting up 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.
 +
<pre><nowiki>
 +
sudo nano /etc/network/interfaces
 +
</nowiki></pre>
 +
<pre><nowiki>
 +
auto eth0:1
 +
iface eth0:1 inet static
 +
address 192.168.2.33
 +
netmask 255.255.255.0
 +
network x.x.x.x
 +
broadcast x.x.x.x
 +
gateway x.x.x.x
 +
</nowiki></pre>
 +
You need to enter all the details like address,netmask,network,broadcast and gateways values.
 +
For these new settings to take effect you need to restart networking services using the following command
 +
<pre><nowiki>
 +
sudo /etc/init.d/networking restart
 +
</nowiki></pre>
 +
=== Setting Your Hostname ===
 +
The hostname command allows you to directly query, or set, the hostname from the command line.
 +
You can see your current hostname with
 +
<pre><nowiki>
 +
sudo /bin/hostname
 +
</nowiki></pre>
 +
To set the hostname directly you can become root and run
 +
<pre><nowiki>
 +
sudo /bin/hostname newname
 +
</nowiki></pre>
 +
When your system boots it will automatically read the hostname from the file /etc/hostname
 +
=== Setting up DNS ===
 +
You can add hostname and IP addresses to the file /etc/hosts for static lookups.
 +
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.3.2 would have a resolv.conf file looking like this
 +
<pre><nowiki>
 +
sudo nano /etc/resolv.conf
 +
</nowiki></pre>
 +
enter the following details
 +
search myaddress.com
 +
nameserver 192.168.3.2
 +
=== Configuring DHCP Address for Your Network Card ===
 +
The DHCP address can be configured by editing the following file /etc/network/interfaces. Replace eth0 with your interface card (see Find Network Interface Card).
 +
<pre><nowiki>
 +
sudo nano /etc/network/interfaces
 +
</nowiki></pre>
 +
<pre><nowiki>
 +
# The primary network interface – use DHCP to find our address
 +
auto eth0
 +
iface eth0 inet dhcp
 +
</nowiki></pre>
 
=== Howto Set MTU for a DHCP Connection ===
 
=== Howto Set MTU for a DHCP Connection ===
 
Although this is not documented in the manual for interfaces, MTU for a DHCP connected device can be set in the /etc/network/interfaces file. To do so you need to append the 'pre-up' command to the 'iface' section of the relevent interface.
 
Although this is not documented in the manual for interfaces, MTU for a DHCP connected device can be set in the /etc/network/interfaces file. To do so you need to append the 'pre-up' command to the 'iface' section of the relevent interface.
eg:
+
<pre><nowiki>
iface eth0 inet dhcp
+
      iface eth0 inet dhcp
pre-up /sbin/ifconfig $IFACE mtu 1492
+
          pre-up /sbin/ip link set $IFACE mtu 1492
The above example sets the MTU for device eth0 to 1492, the usual MTU for a PPPoE ISP connection.
+
</nowiki></pre>
For a wireless device, you must replace 'ifconfig' with 'iwconfig' in the 'pre-up' command.
+
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 [http://glasnost.beeznest.org/articles/290].
 
This tip was found on [http://glasnost.beeznest.org/articles/290].
 
----
 
----
[[category:CategoryCleanup]] [[category:CategoryNetworking]] [[category:CategoryWireless]]
+
[[category:CategoryNetworking]] [[category:CategoryWireless]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2009年11月17日 (二) 20:12的最新版本

Configure Network Interface Using Command-Line

You can configure a network interface from the command line. You 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.

Find Network Interface Card

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

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).

Configuring Static IP Address For Your Network Card

Configure a Static IP address by editing /etc/network/interfaces. Replace eth0 with your network interface card (see Find Network Interface Card).

sudo nano /etc/network/interfaces
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.2.33
gateway 192.168.2.1
netmask 255.255.255.0
network 192.168.2.0
broadcast 192.168.2.255

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

sudo /etc/init.d/networking restart

Setting up 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.

sudo nano /etc/network/interfaces
auto eth0:1
iface eth0:1 inet static
address 192.168.2.33
netmask 255.255.255.0
network x.x.x.x
broadcast x.x.x.x
gateway x.x.x.x

You need to enter all the details like address,netmask,network,broadcast and gateways values. For these new settings to take effect you need to restart networking services using the following command

sudo /etc/init.d/networking restart

Setting Your Hostname

The hostname command allows you to directly query, or set, the hostname from the command line. You can see your current hostname with

sudo /bin/hostname

To set the hostname directly you can become root and run

sudo /bin/hostname newname

When your system boots it will automatically read the hostname from the file /etc/hostname

Setting up DNS

You can add hostname and IP addresses to the file /etc/hosts for static lookups. 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.3.2 would have a resolv.conf file looking like this

sudo nano /etc/resolv.conf

enter the following details search myaddress.com nameserver 192.168.3.2

Configuring DHCP Address for Your Network Card

The DHCP address can be configured by editing the following file /etc/network/interfaces. Replace eth0 with your interface card (see Find Network Interface Card).

sudo nano /etc/network/interfaces
# The primary network interface – use DHCP to find our address
auto eth0
iface eth0 inet dhcp

Howto Set MTU for a DHCP Connection

Although this is not documented in the manual for interfaces, MTU for a DHCP connected device can be set in the /etc/network/interfaces file. To do so you need to append the 'pre-up' command to the 'iface' section of the relevent interface.

       iface eth0 inet dhcp
          pre-up /sbin/ip link set $IFACE mtu 1492

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 [1].