Linux nội địa hóa

Khi đặt địa chỉ IP tĩnh cho card mạng private bạn chỉ cần đặt địa chỉ IP và subnet mask của mạng đó, đối với mạng public bạn sẽ phải đặt địa chỉ gateway cho card mạng đó.

1. Cấu hình địa chỉ ip tĩnh cho Ubuntu server 16.04

File cấu hình IP cho các card mạng của Ubuntu Server nằm tại /etc/network/interfaces
Nội dung ban đầu sẽ như sau:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
auto eth1
iface eth1 inet dhcp

Để kiểm tra địa chỉ IP được cấp cho Cloud Server có 2 cách:

  • Kiểm tra trên dashboard
  • Kiểm tra trên server: ip a với server linux và ifconfig với server window

Ví dụ ta muốn cấu hình địa chỉ IP tĩnh cho cả 2 card mạng, ta sửa file cấu hình như sau:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8

Giải thích file cấu hình:

Phần này là cấu hình cho card loopback. Mặc định với tất cả các server:

#The loopback network interface
auto lo
iface lo inet loopback

Phần cấu hình IP tĩnh cho interface eth0:

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

  • Dòng 1: auto eth0 sẽ cấu hình cho interface eth0 khởi động cùng hệ thống
  • Dòng 2: iface eth0 inet static tạo 1 đoạn cấu hình tĩnh với tên là eth0 trên card Ethernet của chúng ta. Các dòng tiếp theo là phần cấu hình IP cho interface:
    address: địa chỉ chúng ta muốn đặt cho server
    netmask: subnet mask của dải mạng chúng ta sử dụng

Phần cấu hình IP tĩnh cho interface eth1:

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8

Các khai báo giống eth0, ngoài ra:

  • gateway: default gateway cho server
  • dns-nameserver: DNS server sẽ dùng cho server để phân giải internet. Ở đây chọn DNS server của google.

Giải thích tương tự như eth0

Lưu ý: Các địa chỉ IP ở trên là địa chỉ IP Cloud Server ví dụ, Cloud Server của bạn sẽ có địa chỉ IP khác

Sau khi sửa file cài đặt ta lưu lại thiết lập và sử dụng câu lệnh sau để khởi động lại card mạng ta vừa thiết lập:

sudo ifdown  && sudo ifup 

Với ví dụ trên ta sẽ chạy lệnh

sudo ifdown eth1 && sudo ifup eth1
sudo ifdown eth0 && sudo ifup eth0

hoặc

sudo ifdown -a && sudo ifup -a

Và kiểm tra lại bằng lệnh ip a

2. Cấu hình địa chỉ ip tĩnh cho Ubuntu server 18.04

File cấu hình IP cho các card mạng của Ubuntu Server nằm tại /etc/netplan/50-cloud-init.yaml
Nội dung ban đầu sẽ như sau:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
 network:
     ethernets:
        eth0:
           dhcp4: true
        eth1:
           dhcp4: false

Để kiểm tra địa chỉ IP được cấp cho Cloud Server có 2 cách:

  • Kiểm tra trên dashboard
  • Kiểm tra trên server: ip a với server linux và ifconfig với server window

Ví dụ ta muốn cấu hình địa chỉ IP tĩnh cho cả 2 card mạng, ta sửa file cấu hình như sau:

# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        eth0:
            dhcp4: false
            addresses:
              - 10.6.169.252/24
            gateway4: 10.6.169.3
            nameservers:
                addresses: [8.8.8.8, 1.1.1.1]
        eth1:
            dhcp4: false
            addresses:
              - 103.56.156.215/24
            gateway4: 103.56.156.1
            nameservers:
                addresses: [8.8.8.8, 1.1.1.1]

Giải thích file cấu hình:
Phần cấu hình IP tĩnh cho interface eth0:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
0

  • Dòng 1: Cấu hình trên interface eth0
  • Dòng 2: Vì cấu hình ip tĩnh nên set dhcp4 là false
  • Dòng 3,4: Addresses: nhập địa chỉ ip và subnetmark
  • Dòng 5: Nhập địa chỉ gateway của mạng
  • Dòng 6,7: Cấu hình DNS dùng để phân giải internet
    Phần cấu hình IP tĩnh cho interface eth1:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
1

Giải thích tương tự như eth0

Lưu ý: Các địa chỉ IP ở trên là địa chỉ IP Cloud Server ví dụ, Cloud Server của bạn sẽ có địa chỉ IP khác

Sau khi sửa file cài đặt ta lưu lại thiết lập và sử dụng câu lệnh sau để khởi động lại card mạng ta vừa thiết lập:\

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
2

Với ví dụ trên ta sẽ chạy lệnh\

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
3

3. Cấu hình IP tĩnh cho CentOS 6 và 7

Đối với các hệ điều hành CentOS thì các file cấu hình các card mạng được tách ra mỗi một card mạng một file. Nằm tại foler /etc/sysconfig/network-scripts/

Tên của file cấu hình sẽ có cú pháp ifcfg- với interface là tên của card mạng, Ví dụ file cấu hình cho card eth0 là ifcfg-eth0

Ví dụ ta có file cấu hình cho card eth0 ban đầu như sau

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
4

Để đặt địa chỉ IP 10.20.0.13 subnetmask 255.255.255.0 cho card mạng eth0 ta sửa file ifcfg-eth0 như sau

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
5

Sau đó ta lưu lại và khởi động lại card mạng eth0

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
6

hoặc khởi động lại network service

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 10.5.55.136
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 103.56.158.174
netmask 255.255.255.0
gateway 103.56.158.1
dns-nameservers 8.8.8.8
7

Lưu ý: Đối với CentOS 7 ta sử dụng lệnh systemctl restart network.service

Nếu ta cấu hình trên card mạng ra internet (eth1) ta cần phải đặt gateway cho card mạng bằng cách thêm dòng sau vào trong file cấu hình