做爲一個linux的瘋狂熱愛者和工做者,咱們首先須要掌握的就是在linux系統下怎麼配置IP、路由和主機名基本的linux網絡配置,若是這都不會的話,那真實沒什麼顏面。固然若是要咱們接入互聯網基本的IP地址配置仍是不夠的,訪問網絡的話咱們須要配置咱們的域名解析服務器DNS,(不然咱們只能訪問ip地址,不能訪問帶域名的網址)下面做爲菜鳥的我來和你們介紹一下,主要是從實戰的角度講解怎麼配置一個能夠和外界聯通的網絡,本節暫不講域名配置。
linux
衆所周知,網絡地址有兩種類型:shell
暫時性網絡地址:利用ifconfig、ip等命令配置的網絡信息,會當即生效,但重啓網絡服務或系統會失效服務器
永久性的網絡地址:經過修改系統內的網絡配置文件(/etc/sysconfig/network-scripts/ifcfg-*)進行的修改,不會當即生效,須要重啓網絡服務或者系統會生效,而且會永久性的生效。 網絡
1.給eth0配置一個網絡地址
dom
ifconfig eth0 172.18.186.74 netmask 255.255.255.0
來ping 其餘ip地址,看看網絡的連通狀況測試
能夠看得能夠直接ping同同一網段內的ip主機。可是ping不通再也不同一個網絡段的ip主機。若是想ping通外網,就要設置一個默認網卡(其實就是默認路由,該臺電腦和外界通訊必需要經由該默認路由出去)spa
2 給系統添加一個默認網關(默認路由)rest
route add default gw 172.16.186.254
以下圖,能夠看獲得能夠ping同外界網絡了。code
能夠看獲得最後一條就是默認路由,當找不到目的路由,最後都會走默認路由。server
可是重啓以後就會無效了。
能夠看獲得eth0網卡的配置文件如上:
DEVICE="eth0" 設備名
NM_CONTROLLED="yes" 設備是否被NetworkManager管理
ONBOOT="yes" 開機或重啓網絡是否啓動
HWADDR="00:0C:29:59:E2:D3" 硬件地址(MAC地址)
TYPE=Ethernet 類型
BOOTPROTO=none 啓動協議{none|static|dhcp}
static(靜態IP)
none(不指定,設置固定ip的狀況,這個也行,可是若是要設定多網口綁定bond的時候,必須設成none)
dhcp(動態得到IP相關信息)
IPADDR=192.168.0.1 IP地址
NETMASK=255.255.255.0 子網掩碼
GATEWAY=192.168.0.254 默認網關
寫好上面的配置文件後,執行service network restart(或者/etc/init.d/network restart)或重啓系統就能夠永久生效。
注意:若是沒有重啓網絡或重啓系統,配置文件裏面的ip地址不會起效
# ifconfig eth0 up # 開啓eth0網卡 # ifconfig eth0 down # 關閉eth0網卡 # ifconfig eth0 -arp # 關閉eth0網卡arp協議 # ifconfig eth0 promisc # 開啓eth0網卡的混合模式 # ifconfig eth0 mtu 1400 # 設置eth0網卡的最大傳輸單元爲1400 # ifconfig eth0 192.168.0.2/24 # 設置eth0網卡IP地址 # ifconfig eth0 192.168.0.2 netmask 255.255.255.0 # 功能同上 注:單網卡多ip ifconfig eth0:1 192.168.0.3 netmask 255.255.255.0 # 功能同上
ip link show # 顯示網絡接口信息 # ip link set eth0 up # 開啓網卡 # ip link set eth0 down # 關閉網卡 # ip link set eth0 promisc on # 開啓網卡的混合模式 # ip link set eth0 promisc offi # 關閉網卡的混個模式 # ip link set eth0 txqueuelen 1200 # 設置網卡隊列長度 # ip link set eth0 mtu 1400 # 設置網卡最大傳輸單元 # ip addr show # 顯示網卡IP信息 # ip addr add 192.168.0.1/24 dev eth0 # 設置eth0網卡IP地址192.168.0.1 # ip addr del 192.168.0.1/24 dev eth0 # 刪除eth0網卡IP地址 注:單網卡多ip # 功能同上 ip addr add 192.168.0.1/24 dev eth0 label eth0:1
route add [-net|-host] target [netmask] gw route del [-net|-host] target [netmask] gw # route add -net 192.168.3.0/24 gw 192.168.0.254 # 設置到192.168.3.0網段的網關爲192.168.0.254 # route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.0.254 # 功能同上 # route add -host 192.168.4.4 gw 192.168.0.254 # 設置到192.168.4.4主機的網關爲192.168.0.254 # # route del -net 192.168.3.0/24 # 刪除192.168.3.0網段的網關信息 # route del -host 192.168.4.4 # 刪除192.168.4.4主機的網關信息 # route add default gw 192.168.0.254 # 設置默認網關爲192.168.0.254 # route del default gw 192.168.0.254 # 刪除默認網關爲192.168.0.254
ip route list # 查看路由信息 # ip route add 192.168.4.0/24 via 192.168.0.254 dev eth0 # 設置192.168.4.0網段的網關爲192.168.0.254,數據走eth0接口 # ip route add default via 192.168.0.254 dev eth0 # 設置默認網關爲192.168.0.254 # ip route del 192.168.4.0/24 # 刪除192.168.4.0網段的網關 # ip route del default # 刪除默認路由
DNS配置文件位置:/etc/resolv.conf
DNS配置格式: nameserver DNS_IP1 nameserver DNS_IP2 指定本地解析: /etc/hosts下添加 目標主機IP 主機名 fg:172.16.36.1 www.chris.com DNS解析過程-->/etc/hosts-->DNS 服務器
文件位置: /etc/sysconfig/network, 典型的配置以下
NETWORKING=yes HOSTNAME=localhost.localdomain GATEWAY=172.16.186.254
參數簡要解釋:
NETWORK 設置網絡是否有效,yes有效,no無效
NETWORKING_IPV6 設置ipv6網絡是否有效,yes有效,no無效
HOSTNAME 設置服務器的主機名,最好和/etc/hosts裏設置同樣,不然在使用一些程序的時候會有問題。
GATEWAY 指定默認網關IP
/etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
可見,默認的狀況是本機ip和本機一些主機名的對應關係,第一行是ipv4信息,第二行是ipv6信息,若是用不上ipv6本機解析,通常把該行註釋掉。
第一行表示 localhost localhost.localdomain localhost4 localhost4.localdomain4
都會被解析成127.0.0.1,咱們能夠用ping試試。
測試以下:
[root@localhost ~]# ping -c 3 localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.026 ms
--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.026/0.031/0.038/0.005 ms
[root@localhost ~]# ping -c 3 localhost.localdomain
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.022 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.064 ms
--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.022/0.041/0.064/0.017 ms
[root@localhost ~]# ping -c 3 localhost4
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.027 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.021 ms
--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.021/0.023/0.027/0.006 ms
[root@localhost ~]# ping -c 3 localhost4.localdomain4
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.028 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.029 ms
--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.023/0.026/0.029/0.006 ms
至此,總結完畢!