Linux网络基本配置
一.Linux網絡配置文件
1.? /etc/sysconfig/network-scripts/ifcfg-eth0 文件
? 在Red Hat系統中,系統網絡設備的配置文件保存在/etc/sysconfig/network-scripts目錄下,其中文件ifcfg-eth0包含第一塊網卡的配置信息,文件ifcfg-eth1包含第二塊網卡的配置信息,文件ifcfg-lo包含回路IP地址信息。
[root@redhat2 ~]# ls /etc/sysconfig/network-scripts
ifcfg-eth0?? ifdown-post??? ifup-eth??? ifup-routes
ifcfg-lo???? ifdown-ppp???? ifup-ippp?? ifup-sit
ifdown?????? ifdown-routes? ifup-ipv6?? ifup-tunnel
ifdown-bnep? ifdown-sit???? ifup-isdn?? ifup-wireless
ifdown-eth?? ifdown-tunnel? ifup-plip?? init.ipv6-global
ifdown-ippp? ifup?????????? ifup-plusb? net.hotplug
ifdown-ipv6? ifup-aliases?? ifup-post?? network-functions
ifdown-isdn? ifup-bnep????? ifup-ppp??? network-functions-ipv6
[root@redhat2 ~]#
下面是/etc/sysconfig/network-scripts/ifcfg-eth0文件內容的示例。
[root@redhat2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"?????????????????????????? //表示物理設備的名字,如eth0,eth1
BOOTPROTO=static??????????????? //表示為網卡配置靜態還是動態IP地址
IPADDR=192.168.12.11???????? //表示賦給該網卡的IP地址
NETMASK=255.255.255.0??? //表示子網掩網
ONBOOT=yes????????????????????????? //表示啟動系統時是否激活該卡,yes激活,no不激活
TYPE="Ethernet"
[root@redhat2 ~]#
可以為BOOTPROTO設置4種選項。
● none:表示無需啟動協議。
● bootp:表示使用bootp協議。
● dhcp: 表示使用dhcp協議動態獲取IP地址。
● static:表示手工設置靜態IP地址。
文件/etc/resolv.conf是由域名解析器(resolver,一個根據主機名解析IP地址的庫)使用的配置文件,下面是/etc/resolv.conf文件內容的示例。
????? search openarch.com
???? nameserver 202.96.209.5
???? nameserver 202.96.209.6
該文件中包含的內容描述如下。
Search:表示DNS搜索路徑,即解析不完整名稱時默認的附件域名后綴,這樣可以在解析名稱時用簡短的主機名而不是完全合格域名(FQDN)。
Nameserver:表示解析域名時使用該地址指定的主機為域名服務器,其中域名服務器是按照文件中出現的順序來查詢的。
文件/etc/host.conf指定如何解析主機名,redhat系統通過解析器庫來獲得主機名對應的IP地址,下面是/etc/host.conf文件內容的示例。
Order bind,hosts
Multi on
Nospoof on
???? 該文件中包含的內容描述如下:
?order bind,hosts:指定主機名查詢順序,這里規定先使用DNS來解析域名,然后再查詢/etc/hosts文件。
Multi on
Nospoof on
該文件中包含的內容描述如下。
Order bind,hosts:指定主機名查詢順序,這里規定先使用DNS來解析域名,然后再查詢/etc/hosts文件。
Multi on:表示在/etc/hosts文件中指定的主機是否可以有多個地址,擁有多個IP地址的主機一般稱為多穴主機。
Nospoof on:表示不允許對該服務器進行IP地址欺騙。IP欺騙是一種攻擊系統安全的手段,通過把IP地址偽裝成別的計算機,來取得其他計算機的信任。
4./etc/sysconfig/network文件
??? 文件/etc/sysconfig/network用來指定服務器上的網絡配置信息,下面是/etc/sysconfig/network文件內容的示例。
[root@redhat2 ~]# cat /etc/sysconfig/network
NETWORKING=yes? //是否配置網絡
HOSTNAME=redhat2? //表示計算機的主機名
5./etc/hosts文件
?? 當計算機啟動時,在可以查詢DNS以前,計算機需要查詢一些主機名到IP地址的匹配。這些匹配信息存放在/etc/hosts文件中。在沒有域名服務器的情況下,系統上的所有網絡程序都通過查詢該文件來解析對應于某個主機名的IP地址。
[root@redhat2 ~]# cat /etc/hosts
192.168.12.11?? redhat2
::1???????? localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@redhat2 ~]#
? 最左邊一列是計算機IP地址,中間一列是主機名,最后面的列都是該主機的別名。一旦配置完計算機的網絡配置文件,應該重新啟動網絡以使修改生效。
6./etc/services文件。
? 文件/etc/services定義了Linux系統中所有服務的名稱、協議類型、服務的端口等信息。/etc/services文件是一個服務名和服務端口對應的數據庫文件,下面是/etc/services文件內容的示例。
二.Linux網絡命令
? 在Linux系統中提供了大量的網絡命令用于網絡配置、網絡測試以及網絡診斷,如ifconfig,ping,netstat,traceroute,arp以及tcpdump等。
1.traceroute
? 使用tracerout命令可以顯示數據包到目標主機之間的路徑。
Traceroute? [-dFlnrvx] [-f<存活數值>] [-g<網關>]…[-I <網絡界面>] [-m<存活數值>] [-p<通信端口>] [-s<來源地址>][-t<服務類型>][-w<超時秒數>][主機名稱或IP地址][數據包大小]。
2.ifconfig
? 使用ifconfig命令可以顯示或設置計算機網卡的IP地址。
?命令語法:
[root@redhat2 ~]# ifconfig --help
Usage:
? ifconfig [-a] [-v] [-s] <interface> [[<AF>] <address>]
? [add <address>[/<prefixlen>]]
? [del <address>[/<prefixlen>]]
? [[-]broadcast [<address>]]? [[-]pointopoint [<address>]]
? [netmask <address>]? [dstaddr <address>]? [tunnel <address>]
? [outfill <NN>] [keepalive <NN>]
? [hw <HW> <address>]? [metric <NN>]? [mtu <NN>]
? [[-]trailers]? [[-]arp]? [[-]allmulti]
? [multicast]? [[-]promisc]
? [mem_start <NN>]? [io_addr <NN>]? [irq <NN>]? [media <type>]
? [txqueuelen <NN>]
? [[-]dynamic]
? [up|down] …
例1:配置網卡eth3的IP地址,同時激活該設備。
? [root@redhat2 ~]# ifconfig eth3 192.168.12.121 netmask 255.255.255.0 up
例2:配置網卡eth3別名設備eth3:1的IP的地址。
? [root@redhat2 ~]# ifconfig eth3:1 192.168.12.0
例3:激活網卡eth3:1設備。
?[root@redhat2 ~]# ifconfig eth3:1 up
例4:查看所有的網卡網絡接口配置。
[root@redhat2 ~]# ifconfig
eth2????? Link encap:Ethernet? HWaddr 00:0C:29:53:BB:56?
????????? inet addr:192.168.12.11? Bcast:192.168.12.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:fe53:bb56/64 Scope:Link
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
????????? RX packets:873 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:915 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:1000
????????? RX bytes:91453 (89.3 KiB)? TX bytes:414896 (405.1 KiB)
eth3????? Link encap:Ethernet? HWaddr 00:0C:29:53:BB:60?
????????? inet addr:192.168.12.121? Bcast:192.168.12.255? Mask:255.255.255.0
????????? inet6 addr: fe80::20c:29ff:fe53:bb60/64 Scope:Link
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
????????? RX packets:124 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:134 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:1000
????????? RX bytes:12196 (11.9 KiB)? TX bytes:17381 (16.9 KiB)
eth3:1??? Link encap:Ethernet? HWaddr 00:0C:29:53:BB:60?
????????? inet addr:192.168.12.0? Bcast:192.168.12.255? Mask:255.255.255.0
????????? UP BROADCAST RUNNING MULTICAST? MTU:1500? Metric:1
lo??????? Link encap:Local Loopback?
????????? inet addr:127.0.0.1? Mask:255.0.0.0
????????? inet6 addr: ::1/128 Scope:Host
????????? UP LOOPBACK RUNNING? MTU:16436? Metric:1
????????? RX packets:914 errors:0 dropped:0 overruns:0 frame:0
????????? TX packets:914 errors:0 dropped:0 overruns:0 carrier:0
????????? collisions:0 txqueuelen:0
????????? RX bytes:45860 (44.7 KiB)? TX bytes:45860 (44.7 KiB)
[root@redhat2 ~]#
例5:禁用網卡eth3:1設備。
[root@redhat2 ~]# ifconfig eth3:1 down
3.ping
使用ping命令可用于測試與目標計算機之間的連通性。
[root@redhat2 ~]# ping --help
ping: invalid option -- '-'
Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
??????????? [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
??????????? [-M mtu discovery hint] [-S sndbuf]
??????????? [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
[root@redhat2 ~]#
例1:測試與 192.168.12.121計算機的連通性,要求返回4個ICMP數據包。
[root@redhat2 ~]# ping -c 4 192.168.12.121
PING 192.168.12.121 (192.168.12.121) 56(84) bytes of data.
64 bytes from 192.168.12.121: icmp_seq=1 ttl=64 time=0.158 ms
64 bytes from 192.168.12.121: icmp_seq=2 ttl=64 time=0.069 ms
64 bytes from 192.168.12.121: icmp_seq=3 ttl=64 time=0.067 ms
64 bytes from 192.168.12.121: icmp_seq=4 ttl=64 time=0.037 ms
--- 192.168.12.121 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.037/0.082/0.158/0.046 ms
[root@redhat2 ~]#
4.netstat
? 使用netstat命令可用于顯示網絡狀態的信息。
[root@redhat2 ~]# netstat --help
usage: netstat [-veenNcCF] [<Af>] -r???????? netstat {-V|--version|-h|--help}
?????? netstat [-vnNcaeol] [<Socket> ...]
?????? netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]
??????? -r, --route??????????????? display routing table
??????? -I, --interfaces=<Iface>?? display interface table for <Iface>
??????? -i, --interfaces?????????? display interface table
??????? -g, --groups?????????????? display multicast group memberships
??????? -s, --statistics?????????? display networking statistics (like SNMP)
??????? -M, --masquerade?????????? display masqueraded connections
??????? -v, --verbose????????????? be verbose
??????? -n, --numeric????????????? don't resolve names
??????? --numeric-hosts??????????? don't resolve host names
??????? --numeric-ports??????????? don't resolve port names
??????? --numeric-users??????????? don't resolve user names
??????? -N, --symbolic???????????? resolve hardware names
??????? -e, --extend?????????????? display other/more information
??????? -p, --programs???????????? display PID/Program name for sockets
??????? -c, --continuous?????????? continuous listing
??????? -l, --listening??????????? display listening server sockets
??????? -a, --all, --listening???? display all sockets (default: connected)
??????? -o, --timers?????????????? display timers
??????? -F, --fib????????????????? display Forwarding Information Base (default)
??????? -C, --cache??????????????? display routing cache instead of FIB
??????? -T, --notrim?????????????? stop trimming long addresses
??????? -Z, --context????????????? display SELinux security context for sockets
? <Iface>: Name of interface to monitor/list.
例1:顯示網絡接口狀態信息。
[root@redhat2 ~]# netstat -i
Kernel Interface table
Iface?????? MTU Met??? RX-OK RX-ERR RX-DRP RX-OVR??? TX-OK TX-ERR TX-DRP TX-OVR Flg
eth2?????? 1500?? 0????? 883????? 0????? 0????? 0????? 916????? 0????? 0????? 0 BMRU
eth3?????? 1500?? 0????? 366????? 0????? 0????? 0????? 309????? 0????? 0????? 0 BMRU
lo??????? 16436?? 0???? 1034????? 0????? 0????? 0???? 1034????? 0????? 0????? 0 LRU
[root@redhat2 ~]#
例2:顯示內核路由表信息。
[root@redhat2 ~]# netstat -r
Kernel IP routing table
Destination???? Gateway???????? Genmask???????? Flags?? MSS Window? irtt Iface
192.168.12.0??? *?????????????? 255.255.255.0?? U???????? 0 0????????? 0 eth3
192.168.12.0??? *?????????????? 255.255.255.0?? U???????? 0 0????????? 0 eth2
[root@redhat2 ~]#
三.chkconfig和service命令
??? 在Linux系統上可以使用chkconfig和service命令控制服務的啟動、停止和重新啟動,兩者之間的區別在于使用service命令控制服務可以馬上生效,而使用chkconfig命令控制服務需要等計算機重新啟動后才會生效。
1.chkconfig命令
? 使用chkconfig命令主要用來設置下次重啟計算機以后啟動、停止服務,使用chkconfig命令不會立即啟動或停止一項服務。
[root@redhat2 ~]# chkconfig --help
chkconfig version 1.3.49.3 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
usage:?? chkconfig [--list] [--type <type>] [name]
???????? chkconfig --add <name>
???????? chkconfig --del <name>
???????? chkconfig --override <name>
???????? chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
[root@redhat2 ~]#
例1:查看各種不同的運行等級中各項服務的狀況。
[root@redhat2 ~]# chkconfig --help
chkconfig version 1.3.49.3 - Copyright (C) 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
usage:?? chkconfig [--list] [--type <type>] [name]
???????? chkconfig --add <name>
???????? chkconfig --del <name>
???????? chkconfig --override <name>
???????? chkconfig [--level <levels>] [--type <type>] <name> <on|off|reset|resetpriorities>
[root@redhat2 ~]# chkconfig --list
NetworkManager? 0:off?? 1:off?? 2:on??? 3:on??? 4:on??? 5:on??? 6:off
abrt-ccpp?????? 0:off?? 1:off?? 2:off?? 3:on??? 4:off?? 5:on??? 6:off
abrt-oops?????? 0:off?? 1:off?? 2:off?? 3:on??? 4:off?? 5:on??? 6:off
abrtd?????????? 0:off?? 1:off?? 2:off?? 3:on??? 4:off?? 5:on??? 6:off
acpid?????????? 0:off?? 1:off?? 2:on??? 3:on??? 4:on??? 5:on??? 6:off
atd???????????? 0:off?? 1:off?? 2:off?? 3:on??? 4:on??? 5:on??? 6:off
auditd????????? 0:off?? 1:off?? 2:on??? 3:on??? 4:on??? 5:on??? 6:off
avahi-daemon??? 0:off?? 1:off?? 2:off?? 3:on??? 4:on??? 5:on??? 6:off
bluetooth?????? 0:off?? 1:off?? 2:off?? 3:on??? 4:on??? 5:off?? 6:off
cpuspeed??????? 0:off?? 1:on??? 2:on??? 3:on??? 4:on??? 5:on??? 6:off
crond?????????? 0:off?? 1:off?? 2:on??? 3:on??? 4:on??? 5:on??? 6:off
例2:列出named服務在各個運行級別上的運行狀態。
[root@redhat2 ~]# chkconfig --list bluetooth
bluetooth?????? 0:off?? 1:off?? 2:off?? 3:on??? 4:on??? 5:off?? 6:off
[root@redhat2 ~]#
?例3:在運行級別5,6上啟動bluetooth服務。??
[root@redhat2 ~]# chkconfig --list bluetooth
bluetooth?????? 0:off?? 1:off?? 2:off?? 3:on??? 4:on??? 5:off?? 6:off
[root@redhat2 ~]# chkconfig --level 56 bluetooth? on
[root@redhat2 ~]# chkconfig --list bluetooth?
bluetooth?????? 0:off?? 1:off?? 2:off?? 3:on??? 4:on??? 5:on??? 6:on
[root@redhat2 ~]#
2.service命令
? 使用service命令可以啟動或停止守護進程,service命令執行后會立即生效。
[root@redhat2 ~]# service --help
Usage: service < option > | --status-all | [ service_name [ command | --full-restart ] ]
[root@redhat2 ~]#
? 例1:啟動 bluetooth服務。
[root@redhat2 ~]# service? bluetooth start
Enabling Bluetooth devices:
[root@redhat2 ~]#
例2:查看 bluetooth服務運行狀態。
[root@redhat2 ~]# service? bluetooth status
Usage: /etc/init.d/bluetooth {start|stop}
例3:重啟bluetooth服務。?
[root@redhat2 ~]# service bluetooth restart
Usage: /etc/init.d/bluetooth {start|stop}
[root@redhat2 ~]#
例4:停止bluetooth服務。??
[root@redhat2 ~]# service bluetooth stop
Stopping Bluetooth services:
[root@redhat2 ~]#
?
轉載于:https://www.cnblogs.com/numberone/p/4264816.html
總結
以上是生活随笔為你收集整理的Linux网络基本配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 控制器的显示注入
- 下一篇: android 重装sdk或者系统的时模