วันพฤหัสบดีที่ 2 กันยายน พ.ศ. 2553

Linux, Network : เปลี่ยนชื่อ Network Interface

ในกรณีที่ใช้ Linux มาทำเป็น Firewall นั้น บางทีจะอยากจะเปลี่ยนชื่อของ Network Interface ให้ตรงกับ Zone ในใช้งาน เพื่อให้ง่ายในการจดจำ ในกรณีที่มีหลาย Interface เช่น จากเดิม eth0 ก็ไปเป็น wan เป็นต้น

ระบบที่ทดสอบ

OS : Ubuntu Linux 10.04.1 LTS

กำหนดให้
eth0 --> wan
eth1 --> lan
eth2 --> wireless
eth3 --> dmz

ระบบเดิม
root@server:~# ifconfig -a
eth0 Link encap:Ethernet HWaddr 00:14:22:15:eb:ed
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Base address:0xccc0 Memory:df8e0000-df900000

eth1 Link encap:Ethernet HWaddr 00:14:22:15:eb:ee
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Base address:0xbcc0 Memory:df6e0000-df700000

eth2 Link encap:Ethernet HWaddr 00:01:02:58:ed:ca
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:37 Base address:0xc00

eth3 Link encap:Ethernet HWaddr 00:04:76:21:ac:96
inet addr:172.17.12.55 Bcast:172.17.12.255 Mask:255.255.255.0
inet6 addr: fe80::204:76ff:fe21:ac96/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:359 errors:0 dropped:0 overruns:1 frame:0
TX packets:173 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:33463 (32.6 KB) TX bytes:22815 (22.2 KB)
Interrupt:39 Base address:0xec00

ขั้นตอนการเปลี่ยน
ให้แก้ไขที่ไฟล์ /etc/udev/rules.d/70-persistent-net.rules โดยก่อนการแก้ไข ควร backup ไฟล์นี้ไว้ก่อน

root@server:/etc/udev/rules.d# cp 70-persistent-net.rules 70-persistent-net.rules.backup

จากนั้นมาดูเนื้อหาในไฟล์นี้ก่อนว่ามีอะไรน่าสนใจบ้าง

root@server:/etc/udev/rules.d# cat 70-persistent-net.rules

# This file was automatically generated by the /lib/udev/write_net_rules
# program run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single line.

# PCI device 0x8086:0x1076 (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:14:22:15:eb:ed", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

# PCI device 0x8086:0x1076 (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:14:22:15:eb:ee", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"

# PCI device 0x10b7:0x9200 (3c59x)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:01:02:58:ed:ca", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"

# PCI device 0x10b7:0x9200 (3c59x)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:04:76:21:ac:96", ATTR{type}=="1", KERNEL=="eth*", NAME="eth3"


จาก output จะเห็นว่า จะมีการ map หมายเลขของ MAC address เช่น ATTR{address}=="00:04:76:21:ac:96" ---> NAME="eth3"
ซึ่งเราจะมาทำการแก้ตรงนี้แหละ

root@server:/etc/udev/rules.d# vi 70-persistent-net.rules

# This file was automatically generated by the /lib/udev/write_net_rules
# program run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single line.

# PCI device 0x8086:0x1076 (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:14:22:15:eb:ed", ATTR{type}=="1", KERNEL=="eth*", NAME="wan" <--- แก้เป็น wan จากเดิม eth0

# PCI device 0x8086:0x1076 (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:14:22:15:eb:ee", ATTR{type}=="1", KERNEL=="eth*", NAME="lan" <--- แก้เป็น lan จากเดิม eth1

# PCI device 0x10b7:0x9200 (3c59x)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:01:02:58:ed:ca", ATTR{type}=="1", KERNEL=="eth*", NAME="wireless" <--- แก้เป็น wireless จากเดิม eth2

# PCI device 0x10b7:0x9200 (3c59x)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:04:76:21:ac:96", ATTR{type}=="1", KERNEL=="eth*", NAME="dmz" <--- แก้เป็น dmz จากเดิม eth3

จากนั้นให้ทำการแก้ไข network configuration เพื่อให้รู้จักชื่อ Interface ใหม่

root@server# vi /etc/network/interfaces

auto lo
iface lo inet loopback

# The primary network interface
auto dmz <--- แก้เป็น dmz จากเดิม eth3
iface dmz inet static <--- แก้เป็น dmz จากเดิม eth3
address 172.17.12.55
netmask 255.255.255.0
gateway 172.17.12.1


เมื่อตรวจสอบเรียบร้อยแล้ว ก็ reboot เครื่อง
และเมื่อเครื่อง boot เสร็จแล้ว ก็ทำการตรวจสอบผลงานซะหน่อย

root@server:/# ifconfig -a
dmz Link encap:Ethernet HWaddr 00:04:76:21:ac:96
inet addr:172.17.12.55 Bcast:172.17.12.255 Mask:255.255.255.0
inet6 addr: fe80::204:76ff:fe21:ac96/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:3135 errors:0 dropped:0 overruns:1 frame:0
TX packets:1038 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:317387 (309.9 KB) TX bytes:171021 (167.0 KB)
Interrupt:39 Base address:0xcc00

lan Link encap:Ethernet HWaddr 00:14:22:15:eb:ee
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Base address:0xbcc0 Memory:df6e0000-df700000

wan Link encap:Ethernet HWaddr 00:14:22:15:eb:ed
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Base address:0xccc0 Memory:df8e0000-df900000

wireless Link encap:Ethernet HWaddr 00:01:02:58:ed:ca
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:37 Base address:0xec00

root@server:/# ethtool dmz

Settings for dmz:
Supported ports: [ TP MII ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: MII
PHYAD: 24
Transceiver: internal
Auto-negotiation: on
Current message level: 0x00000001 (1)
Link detected: yes

ทดสอบด้วย iptables

root@server:/# iptables -A INPUT -i dmz -j ACCEPT
root@server:/# iptables -A INPUT -i lan -j ACCEPT
root@server:/#
root@server:/# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 796 packets, 64226 bytes)
pkts bytes target prot opt in out source destination
57 4308 ACCEPT all -- dmz * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lan * 0.0.0.0/0 0.0.0.0/0


แต่เมื่อทดสอบด้วย
mii-tool กลับไม่รู้จักซะงั้น (ไม่เป็นไร พอรับได้)
root@server:/# mii-tool
no MII interfaces found


หมายเหตุ การเปลี่ยนชื่อ interface นั้นอาจทำให้ Application บางตัวที่สนใจแต่ชื่อ eth มีปัญหาได้ ดังนั้นควรตรวจสอบให้ดีก่อนใช้งานในระบบจริง (แต่เท่าที่ผมทดสอบในระบบของผม ไม่พบปัญหา)

ไม่มีความคิดเห็น:

แสดงความคิดเห็น