Thứ Hai, 3 tháng 2, 2014

// // Leave a Comment

Cấu hình mạng căn bản

I. Hostname (tên máy):
- Xem hostname hệ thống:
[root@cent5 ~]# hostname
cent5.thehai.com
hoặc:
[root@cent5 ~]# uname -n
cent5.thehai.com
- Thay đổi hostname:
[root@cent5 ~]# vi /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
192.168.1.7     testserver

- Nếu muốn hostname thay đổi kể cả khi ta khởi động lại hệ thống, thì ta chỉnh sửa file:
[root@cent5 ~]# vi /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=cent5.thehai.com

(khởi động lại máy sau khi đổi HOSTNAME)

II. Cấu hình IP của card mạng:
Xem thông tin tất cả các card mạng
[root@cent5 ~]# ifconfig -a hoặc ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3691 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3296 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:341204 (333.2 KiB)  TX bytes:402449 (393.0 KiB)
          Interrupt:67 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:2319 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2319 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3669160 (3.4 MiB)  TX bytes:3669160 (3.4 MiB)
 Nếu muốn xem từng card mạng thì
 [root@cent5 ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3726 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3339 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:344185 (336.1 KiB)  TX bytes:407567 (398.0 KiB)
          Interrupt:67 Base address:0x2000

- Thay đổi địa chỉ IP:
[root@cent5 ~]# ifconfig eth0 192.168.36.231 netmask 255.255.255.0 up

Tuy nhiên IP này chỉ được gán tạm thời, và không được lưu lại khi khởi động lại máy hoặc khởi động lại service network

Vì thế, để lưu lại IP ta chỉnh sửa file ifcfg-eth0
[root@cent5 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HWADDR=00:0c:29:d8:12:fe
TYPE=Ethernet

Hoặc:

DEVICE=eth0
HWADDR=00:0C:29:14:11:7A
TYPE=Ethernet
IPADDR=192.168.1.99
NETMASK=255.255.255.0
UUID=a64c8673-c53a-4f6f-b46d-1f4a77419c20
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
GATEWAY=192.168.1.1

Hoặc - Dcom

DEVICE=eth1
HWADDR=00:0C:29:03:B2:53
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
PEERDNS=yes
PEERROUTES=yes
Hoặc (nc ngoài - chuẩn)
Card WAN:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=aa.aa.aa.aa        # e.g. 123.45.67.89
NETMASK=bb.bb.bb.bb       # e.g. 255.255.255.0
GATEWAY=ee.ee.ee.ee       # e.g. 123.45.67.1
HWADDR=00:30:4f:3b:af:45  # MAC address (optional entry)
Debian - Ubuntu:
auto eth0
iface eth0 inet static
address aa.aa.aa.aa
netmask bb.bb.bb.bb
gateway ee.ee.ee.ee
Card LAN:
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
IPADDR=cc.cc.cc.cc       # e.g. 192.168.0.1
NETMASK=dd.dd.dd.dd      # e.g. 255.255.255.0
HWADDR=00:50:8d:d1:24:db # MAC address of LAN NIC (optional entry)
Debian - Ubuntu:
auto eth1
iface eth1 inet static
address cc.cc.cc.cc
netmask dd.dd.dd.dd
- Khởi động / stop network:
[root@cent5 ~]# /etc/init.d/network start|stop|restart|reload|status
[root@cent5 ~]# service network start|stop|restart|reload|status
- Tạo card mạng ảo:
+ Tạo nhanh tạm thời và kiểm tra lại:
[root@cent5 ~]# ifconfig eth0:0 192.168.100.20 netmask 255.255.255.0 up
[root@cent5 ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:344 errors:0 dropped:0 overruns:0 frame:0
          TX packets:350 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:44584 (43.5 KiB)  TX bytes:35875 (35.0 KiB)
          Interrupt:67 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.100.20  Bcast:192.168.100.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:67 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1817 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1817 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2972052 (2.8 MiB)  TX bytes:2972052 (2.8 MiB)

Nhưng sau khi khởi động lại netword thì card eth0:0 sẽ bị mất. Để lưu lại, ta sao chép file ifcfg-eth0 thành 1 file khác với tên mới ifcfg-eth0:0, sau đó sửa lại IP
[root@cent5 network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
Mở file vừa tạo và chỉnh sửa nội dung:
[root@cent5 network-scripts]# vi ifcfg-eth0:0

Nội dung (sửa IP và device thôi cũng đc):
DEVICE=eth0:0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.100.30
NETMASK=255.255.255.0
HWADDR=00:0c:29:d8:12:fe
TYPE=Ethernet

 Hoac

DEVICE=eth0:0
HWADDR=00:0C:29:14:11:7A
TYPE=Ethernet
IPADDR=192.168.1.110
NETMASK=255.255.255.0
UUID=a64c8673-c53a-4f6f-b46d-1f4a77419c20
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static

Khởi động lại network và kiểm tra lại thử:
[root@cent5 network-scripts]# service network restart
Shutting down interface eth0:                              [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:
Determining IP information for eth0... done.
                                                           [  OK  ]
[root@cent5 network-scripts]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.1.7  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:754 errors:0 dropped:0 overruns:0 frame:0
          TX packets:676 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:81836 (79.9 KiB)  TX bytes:78463 (76.6 KiB)
          Interrupt:67 Base address:0x2000

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:D8:12:FE
          inet addr:192.168.100.30  Bcast:192.168.100.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:67 Base address:0x2000

III. Lệnh route:
- Xem routing table (bảng định tuyến):
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

- Set Default gateway cho từng ethernet device:
[root@cent5 /]# route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.100.1

Kiểm tra lại:
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   192.168.100.1   255.255.255.0   UG    0      0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
- Thêm Default gateway:
[root@cent5 /]# route add default gw 192.168.100.1

Kiểm tra lại:
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   192.168.100.1   255.255.255.0   UG    0      0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.100.1   0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

- Xóa 1 Default route:
[root@cent5 /]# route del default gw 192.168.100.1
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   192.168.100.1   255.255.255.0   UG    0      0        0 eth0
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

- Xóa 1 đường đi trong bảng định tuyến:
[root@cent5 /]# route del -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.100.1 eth0
[root@cent5 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

IV. Lệnh netstat:
- Xem routing table:
[root@cent5 /]# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.100.0   0.0.0.0         255.255.255.0   U         0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0

- Kiểm tra các port đang mở:
[root@cent5 /]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State   
tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN  
tcp        0      0 0.0.0.0:905                 0.0.0.0:*                   LISTEN  
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN  

V. Các lệnh khác:
- Lệnh tracert: Kiểm tra kết nối đến 1 remote host:
[root@cent5 ~]# tracert www.google.com
traceroute to www.google.com (74.125.203.103), 30 hops max, 40 byte packets
 1  mygateway1.ar7 (192.168.1.1)  1.313 ms  1.936 ms  1.750 ms
 2  localhost (123.26.48.1)  14.510 ms  15.097 ms  17.336 ms
 3  vdc.vn (123.29.3.33)  17.896 ms  26.981 ms  27.135 ms
 4  * * *
 5  * * *
 6  * * *
 7   (74.125.51.214)  105.699 ms  76.613 ms  78.817 ms
 8   (209.85.241.54)  81.346 ms  (72.14.221.6)  75.772 ms

- Lệnh tcpdump: Capture các packets
[root@cent5 ~]# tcpdump -n icmp -i eth0

VI. Tập tin /etc/resolv.conf:
Khai báo cấu hình DNS Client
nameserver 192.168.1.1
seach thehai.com
nameserver 8.8.8.8
nameserver 8.8.4.4
 search : danh sách các domain cho việc lookup các host-name, mặc định khai báo local domain. (tối đa khai báo 6 domain và tổng số ký tự là 256 ký tự)
nameserver : chỉ định các DNS Servers

0 nhận xét:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.