hailai 发表于 2018-5-7 12:17:39

Ubuntu 网络管理命令

  网络配置文件:/etc/network/interfaces
  每次修改完配置文件后,需要重启networking 服务。
  $ sudo /etc/init.d/networking restart
  1. 配置DHCP
  配置文件:/etc/network/interfaces
  $ sudo vi /etc/network/interfaces
# The primary network interface - use DHCP to find our address  auto eth0
  iface eth0 inet dhcp
  2. 指定IP地址
  $ sudo vi /etc/network/interfaces
# The primary network interface  auto eth0
  iface eth0 inet static
  address 192.168.3.90
  gateway 192.168.3.1
  netmask 255.255.255.0
  network 192.168.3.0
  broadcast 192.168.3.255
  3. 设置第二IP地址或虚拟IP地址
  $ sudo vi /etc/network/interfaces
auto eth0:1  iface eth0:1 inet static
  address 192.168.1.60
  netmask 255.255.255.0
  network x.x.x.x
  broadcast x.x.x.x
  gateway x.x.x.x
  4.设置静态路由(原文没有此段,引用整理自 http://gfrog.net/2008/01/config-file-in-debian-interfaces-1/)
auto eth0  iface eth0 inet static
  address 192.168.1.42
  network 192.168.1.0
  netmask 255.255.255.128
  broadcast 192.168.1.0
  up route add -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2
  up route add default gw 192.168.1.200
  down route del default gw 192.168.1.200
  down route del -net 192.168.1.128 netmask 255.255.255.128 gw 192.168.1.2
  两行up route表示接口启用的时候,添加一条静态路由和一个缺省路由;
  两行down route表示接口禁用的时候,删掉这两条路由配置。
  5. 设置主机名
  1)
  查看主机名
  $ sudo /bin/hostname
  设置主机名
  $ sudo /bin/hostname newname
  注销后再登录,新主机名将生效。但是当系统重启时,该设置将失效,系统将从 /etc/hostname 文件重新读取主机名。
  2)
  显示主机名:
  $ sudo sysctl kernel.hostname
  设置主机名:
  $ sudo sysctl kernel.hostname=server2
  注销后,新主机名生效。但是当系统重启时,该设置将失效,系统将从 /etc/hostname 文件重新读取主机名。
  3)
  如果要固定主机名,需要编辑 /etc/hostname 文件。
  $ sudo vi /etc/hostname
  4)
  如果要使新的主机名立即生效(译者注:原文此段有误,以下是在ubuntu server 11.10上经过验证的指令格式和结果),执行:
  $ sudo /etc/init.d/hostname start
password for localadmin:  Rather than invoking init scripts through /etc/init.d, use the service(8)
  utility, e.g. service hostname start
  Since the script you are attempting to invoke has been converted to an
  Upstart job, you may also use the start(8) utility, e.g. start hostname
  hostname stop/waiting
  6. 设置 DNS
  这方面ubuntu与其他linux发行版没有区别。
  可以编辑 /etc/hosts 文件指定固定的名称解析。
  可以编辑 /etc/resolv.conf 指定DNS服务器地址,如下:
  nameserver 192.168.3.2
  7. 网络调试工具
  这方面Ubuntu与其他linux发行版没有区别,比如:
  ping
  traceroute
  ifconfig
  route -n
  netstat -nr
  netstat -a
  netstat -l
  原文:http://www.debianadmin.com/ubuntu-networking-for-basic-and-advanced-users.html
  

  2012-02-14 补充:

  把一个安装好的ubuntu server 虚拟机复制到另一台电脑,启动后网络接口 eth0 总是失败。
  看看:
$ sudo ifquery --list  lo
  eth0
  试试:
$ sudo ifup eth0  Can not find device “eth0"
  Failed to bring up eth0.
  查查:
$ cat /etc/network/interfaces  # The primary network interface
  auto eth0
  iface eth0 inet dhcp
  改改:
# The primary network interface  auto eth0
  iface eth0 inet static
  address ...
  netmask ...
  gateway ...
  network ...
  broadcast ...
  再试试:
$ sudo ifup eth0  Can not find device “eth0"
  Failed to bring up eth0.
  换个地方看看:
$ sudo vim /etc/udev/rules.d/70-persistent-net.rules  发现有两行,不同之处:
  第一行
  ATTR{address}=="08:00:27:B3:FA:43"
  NAME="eth0"
  第二行
  ATTR{address}=="08:00:27:06:AC:04"
  NAME="eth1"
  经查,第二行的MAC地址跟虚拟机设置符合,注释掉第一行,将第二行的eth1改为 eth0.
  再来:
$ sudo /etc/init.d/networking restart  OK了!
页: [1]
查看完整版本: Ubuntu 网络管理命令