ubuntu server的网络配置

来源:互联网 发布:la域名注册 编辑:程序博客网 时间:2024/05/01 09:08

首先,查看本服务器的都有哪些本地网卡接口

1
ifconfig -a | grep eth

另一个可以查看网卡接口信息的程序是lshw

1
lshw -class network

以太网接口的逻辑名字(默认是eth0 , eth1 , eth2 ……):
该名称保存在:

/etc/udev/rules.d/70-persistent-net.rules

文件中,可以修改对应MAC的网卡的名称。

以太网接口配置:

ethtool 是一个可以显示和修改以太网卡配置信息的程序,比如:
auto-negotiation (自适应) ,port speed(端口速率), duplex mode(双工模式)和 Wake-on-LAN(从网络唤醒).
默认是没有安装的,可以通过以下命令安装:

1
sudo apt-get install ethtool

查看eth0的信息:

1
sudo ethtool eth0

与ifconfig 修改网卡配置信息一样,ethtool所做的修改也只是临时的,服务器重启后将会丢失。如果你想保留的设置,只需添加一个pre-up 语句(包含所需的ethtool命令)到网卡配置文件/etc/network/interfaces :
下面是一个将eth0网卡永久配置为1000Mb/s全双工模式的例子:

1
2
3
auto eth0
iface eth0 inet static
pre-up /usr/sbin/ethtool -s eth0 speed 1000 duplex full

IP地址的设置
可以使用ip, ifconfig 和 route 来设置,不过和ethtool一样,它们设置的信息在服务器重启后也将会丢失。
如:

1
sudo ifconfig eth0 10.0.0.100 netmask 255.255.255.0

查看:

1
ifconfig eth0

添加网关:

1
sudo route add default gw 10.0.0.1 eth0

查看默认路由配置:

1
route -n

如果你想为临时网络配置信息加DNS配置:
可以修改/etc/resolv.conf这个文件:

1
2
nameserver 8.8.8.8
nameserver 8.8.4.4

如果你想清空一个网卡接口的所有IP配置,你可以使用ip命令另上flush 参数:

1
ip addr flush eth0

注意:
用ip命令不会清空/etc/resolv.conf文件的内容. 你必需手动修改该文件。


动态IP地址分配(DHCP Client)

修改/etc/network/interfaces文件如下:

1
2
auto eth0
iface eth0 inet dhcp

通过以上配置网卡后,你可以手动运行ifup命令来启用该网卡,该命令会通过dhclient来初始化DHCP进程。

1
sudo ifup eth0

要手动禁用某网卡,可以使用 ifdown 命令,它将启动DHCP释放进程然后关闭网卡接口。

1
sudo ifdown eth0

静态IP地址分配
同样是修改/etc/network/interfaces文件内容:

1
2
3
4
5
auto eth0
iface eth0 inet static
address 10.0.0.100
netmask 255.255.255.0
gateway 10.0.0.1

同样可以通过 ifup 和 ifdown命令来启用和禁用网卡。

本地回路接口
一般/etc/network/interfaces 文件中都有如下信息:

1
2
auto lo
iface lo inet loopback


域名解析

修改/etc/resolv.conf 文件:

1
2
3
search example.com
nameserver 8.8.8.8
nameserver 8.8.4.4

静态主机名
修改/etc/hosts文件内容:
下面是一个例子:
从左到右分别是IP 简洁的主机名 别名 相应的 FQDN ( Fully Qualified Domain Names )

1
2
3
4
5
6
127.0.0.1   localhost
127.0.1.1   ubuntu-server
10.0.0.11   server1 vpn server1.example.com
10.0.0.12   server2 mail server2.example.com
10.0.0.13   server3 www server3.example.com
10.0.0.14   server4 file server4.example.com

NSS (Name Service Switch )配置:
该配置保存在/etc/nsswitch.conf文件中。

1
hosts:          files mdns4_minimal [NOTFOUND=return] dns mdns4

* files 表示解析位于 /etc/hosts文件中的静态主机名。
* mdns4_minimal 试图用组播DNS ( Multicast DNS)来解析名字。
* [NOTFOUND=return] means that any response of notfound by the preceeding mdns4_minimal process should be treated as authoritative and that the system should not try to continue hunting for an answer.
* dns 代表传统的单播DNS (unicast DNS)查询 .
* mdns4 代表一个多播DNS查询

网桥
桥接多个接口是一个更先进的配置,但在多种情况下是非常有用的。一种情况是,建立一个具有多个网络接口的网桥,然后使用一个防火墙来过滤两个网段之间的流量。另一种情况是的一个只有一个网卡的系统上使用网桥,让虚拟机直接访问外部网络。下面的例子涵盖了后一种情况。
要配置网桥,要先安装bridge-utils 包:

1
sudo apt-get install bridge-utils

然后通过 编辑/etc/network/interfaces来配置网桥:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
auto lo
iface lo inet loopback

auto br0
iface br0 inet static
        address 192.168.0.10
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1
        bridge_ports eth0
        bridge_fd 9
        bridge_hello 2
        bridge_maxage 12
        bridge_stp off

然后重启网络来使网桥生效:

1
sudo /etc/init.d/networking restart
0 0