ifconfig命令

简介:

ifconfig命令

by Jian Lee

手动设置网卡示例

假设想把网卡设置为如下:

网卡:eth0
IP:192.168.2.110
掩码:255.255.255.0
网管:192.168.2.1

可以这样:

ifconfig eth0 192.168.2.110 netmask 255.255.255.0 up
route add default gw 192.168.2.1 dev eth0

修改MAC

有时候我们需要修改网卡的mac地址,比如我现在的网卡信息如下:

eth0      Link encap:Ethernet  HWaddr 00:15:22:99:36:c9
          inet addr:192.168.2.110  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::215:22ff:fe99:36c9/64 Scope:Link
...

上面显示的HWaddr就是mac地址:00:15:22:99:36:c9。现在我们想把 它修改成我们需要的:

ifconfig eth0 down  # 一定要先停止网卡
ifconfig eth0 hw ether 00:15:22:99:36:c9   # 修改mac地址

然后可以重启网络:

/etc/init.d/networking restart

MAC和网卡名绑定

如果我们有多个网卡,linux下可能叫eth0,eth1,eth2等等,有时候我 们自己都分不清到底哪个网卡对于哪个名字。这时候可以将mac可网卡 名绑定。

  • Redhat系列linux:

    使用sysinit作为scripts管理一些系统服务,可以在类似的 /etc/sysconfig/network-scripts/ifcft-ethX 中写上 HWADDR=00:01:02:8C:50:09 ,这样以后这个ethX网卡就是mac值为 00:01:02:8C:50:09 的网卡了。这里MAC换成自己机器网卡的 mac,ethX中的X代表0,1,2,等等数字。

  • 有nameif命令的linux:

    基本都有这个命令,可以man一下,这个可以设置mac和网卡名字绑定。

桥接网络

创建网络设备

创建一个指定用户有权限的设备 tap0

# tunctl -t tap0 -u 用户名
# chmod 0666 /dev/net/tun

网卡混杂模式

# ifconfig eth0 promisc // 设置eth0为混杂模式
# ifconfig tap0 promisc // 设置tap0为混杂模式

建立桥接接口

# brctl addbr br0
# brctl addif br0 eth0
# brctl addif br0 tap0

设置br0的IP

  • 静态IP
# ifconfig br0 IP地址 netmask 掩码
# route add default gw 默认网关 dev br0
  • DHCP
# dhclient br0

常见应用

1. 查看网络接口使用什么驱动

[root@jianlee ~]# ethtool -i eth0
driver: 3c59x
version:
firmware-version:
bus-info: 0000:01:02.0

2. 查看网络接口link

[root@jianlee ~]# mii-tool
eth0: negotiated 100baseTx-FD, link ok

3. 网卡名字eth0,eth1修改

modprobe 中修改模块别名

使用 udev 的情况

fedora10 就是使用 udev 设置网卡的名字,比如我的系统有两块网卡,e1000e已经 坏了,3c59x还是好的。每次都认3c59x为eth1,即使我在 /etc/sysconfig/network-scripts/ifcfg-eth0 中设置了mac都不行。后来发现在 /etc/udev/rules.d/70-persistent-net.rules 下有这些内容 :

# 3Com Corporation 3c905B 100BaseTX [Cyclone] (rule written by anaconda)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:01:02:8c:50:09", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
# Intel Corporation 82567LM-3 Gigabit Network Connection (rule written by anaconda)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="88:88:88:88:87:88", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

我把 eth0,和 eth1 名字交换。这样就可以了。

使用 ip link

ip link set eth0 name eth2
ip link set eth1 name eth0
ip link set eth2 name eth1

禁止某个 IP 访问

如果不需要用复杂的 iptables,可以简单地在 route 里加上一条:

route add -host IP-A gw 127.0.0.1

路由

参考: http://blog.ligj.eol.cn/628

route 命令

使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:

添加到主机的路由

# route add –host 192.168.168.110 dev eth0
# route add –host 192.168.168.119 gw 192.168.168.1

添加到网络的路由

# route add –net IP netmask MASK eth0
# route add –net IP netmask MASK gw IP
# route add –net IP/24 eth1

添加默认网关

# route add default gw IP

删除路由

# route del –host 192.168.168.110 dev eth0

设置永久路由

/etc/rc.local

route add -net 192.168.3.0/24 dev eth0
route add -net 192.168.2.0/24 gw 192.168.3.254

在/etc/sysconfig/network里添加到末尾

方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev

/etc/sysconfig/static-router

any net x.x.x.x/24 gw y.y.y.y




本文转自 tiger506 51CTO博客,原文链接:http://blog.51cto.com/tiger506/362260,如需转载请自行联系原作者

目录
相关文章
|
1月前
ifconfig 配置网络接口
ifconfig 配置网络接口。
15 1
|
5月前
|
Linux
Linux命令(7)之ifconfig
Linux命令(7)之ifconfig
45 3
|
7天前
|
网络协议 Linux
nmcli命令详解
【4月更文挑战第9天】`nmcli`是Red Hat 7及CentOS 7后的网络管理命令,用于配置网卡并持久化设置。它可以显示网络连接信息(如`connection show`、`dev status`),控制网卡状态(启用、停用、删除连接),以及修改配置(如IP地址、DNS)。其他功能包括检查NetworkManager状态、开关网络连接和查看系统网络状态。要了解全部详情和高级用法,建议查阅相关文档。
10 1
|
8天前
|
Unix Linux
`ifconfig`命令
`ifconfig`是UNIX和类UNIX系统中用于查看和配置网络接口的命令,提供IP地址、子网掩码等信息。常用参数包括显示所有接口(`-a`)、启用/禁用接口(`up/down`)、设置IP地址和子网掩码。配置在重启后失效,需修改配置文件永久保存。在某些新Linux版本中,可能已被`ip`命令替代。是一款便捷的网络管理工具,更多详情可参考相关手册或文档。
11 4
|
4月前
|
运维 Linux Perl
Linux命令(56)之ifconfig
Linux命令(56)之ifconfig
42 1
|
11月前
|
监控 Unix Linux
Linux网络常用命令(ifconfig/ethtool/nmon+n)
Linux网络常用命令(ifconfig/ethtool/nmon+n)
118 0
|
移动开发 网络协议 Linux
网络环境查看命令:ifconfig、ifup、ifdown、route、netstat、ss
一、ifconfig ifconfig命令被用于配置和显示Linux内核中网络接口的网络参数。用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。 1. 安装 若系统默认没有ifconfig命令,则使用下面命令进行安装。