系統(tǒng)運(yùn)維 Linux網(wǎng)絡(luò)設(shè)置
查看網(wǎng)絡(luò)設(shè)置
創(chuàng)新互聯(lián)長(zhǎng)期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為靈寶企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),靈寶網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。測(cè)試網(wǎng)絡(luò)連接
使用網(wǎng)絡(luò)配置命令 修改網(wǎng)絡(luò)配置文件? 本文主要介紹Linux中一些基礎(chǔ)常用的Linux網(wǎng)絡(luò)設(shè)置命令與一些腳本文件配置步驟。
一、查看及測(cè)試網(wǎng)絡(luò)配置 1.ifconfig——查看網(wǎng)絡(luò)接口地址格式:ifconfig [網(wǎng)絡(luò)接口名] [ip地址](子網(wǎng)掩碼)
[root@lokott ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 3556 bytes 232528 (227.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 104 bytes 16400 (16.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 56 bytes 6144 (6.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 56 bytes 6144 (6.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:71:37:09 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@lokott ~]# ifconfig ens33
ens33: flags=4163 mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 3560 bytes 232768 (227.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 104 bytes 16400 (16.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
2.hostname ——查看主機(jī)名格式:hostname
[root@lokott ~]# hostname
lokott
[root@lokott ~]# cat /etc/hostname
lokott
[root@lokott ~]# echo local > /etc/hostname
[root@lokott ~]# cat /etc/hostname
local
[root@lokott ~]#需要重啟才可以設(shè)置成功,另一種方法是使用hostnamectl set-hostname 主機(jī)名^c
[root@lokott ~]#reboot
[root@local ~]# hostnamectl set-hostname lokott
[root@local ~]# su
[root@lokott ~]# 換為lokott了^c
3.route——查看路由條目-n:數(shù)字顯示(centos6中,7中加不加幾乎一樣)
[root@lokott ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.68.2 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
4.netstat——查看網(wǎng)絡(luò)連接情況-n 序列號(hào)
-p 端口號(hào)
-a 所有服務(wù)端口
-r 顯示路由表信息
-l 顯示處于監(jiān)聽狀態(tài)的網(wǎng)絡(luò)連接及端口信息
-t tcp
-u udp
[root@lokott ~]# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1490/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 941/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 939/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1186/master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 941/sshd
tcp6 0 0 ::1:631 :::* LISTEN 939/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 1186/master
[root@lokott ~]# netstat -nuap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp 0 0 0.0.0.0:18066 0.0.0.0:* 3935/dhclient
udp 0 0 192.168.122.1:53 0.0.0.0:* 1490/dnsmasq
udp 0 0 0.0.0.0:67 0.0.0.0:* 1490/dnsmasq
udp 0 0 0.0.0.0:68 0.0.0.0:* 3935/dhclient
udp 0 0 0.0.0.0:5353 0.0.0.0:* 553/avahi-daemon: r
udp 0 0 0.0.0.0:50467 0.0.0.0:* 553/avahi-daemon: r
udp6 0 0 :::61206 :::* 3935/dhclient
[root@lokott ~]# netstat -r //與route一樣
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default gateway 0.0.0.0 UG 0 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
5.traceroute—— 測(cè)試從當(dāng)前主機(jī)到目標(biāo)主機(jī)之間經(jīng)過的網(wǎng)絡(luò)節(jié)點(diǎn)
[root@lokott ~]# traceroute 127.0.0.1
traceroute to 127.0.0.1 (127.0.0.1), 30 hops max, 60 byte packets
1 localhost (127.0.0.1) 0.030 ms 0.007 ms 0.035 ms
[root@lokott ~]# traceroute 192.168.10.11
traceroute to 192.168.10.11 (192.168.10.11), 30 hops max, 60 byte packets
1 gateway (192.168.68.2) 0.154 ms 0.085 ms 0.087 ms
2 * * *
3 * * *
4 * * *
5 * * *
6 * * *
7 *^C
[root@lokott ~]#
6.nslookup—— 測(cè)試DNS域名解析
[root@lokott ~]# nslookup www.taobao.com
Server: 192.168.68.2
Address: 192.168.68.2#53
Non-authoritative answer:
www.taobao.com canonical name = www.taobao.com.danuoyi.tbcache.com.
Name: www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.235
Name: www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.234
Name: www.taobao.com.danuoyi.tbcache.com
Address: 211.138.124.238
補(bǔ)充:在Windows系統(tǒng)的dos界面也一樣適用
二、設(shè)置絡(luò)地址參數(shù)1.臨時(shí)配置——調(diào)試網(wǎng)絡(luò)時(shí)使用
2.固定配置——更改配置文件,需要重啟network服務(wù)或重啟主機(jī)后才會(huì)生效,相當(dāng)于永久配置。
7.ifdown 和ifup 禁用、激活網(wǎng)絡(luò)接口[root@lokott ~]# ifconfig ens33:0 192.168.155.55 //虛接口配置
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163 mtu 1500
inet 192.168.155.55 netmask 255.255.255.0 broadcast 192.168.155.255
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
[root@lokott ~]# ifconfig ens33:0 down
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163 mtu 1500
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
[root@lokott ~]# ifconfig ens33:0 192.168.155.55
[root@lokott ~]# ifdown ens33:0
用法:ifdown <設(shè)備名>
[root@lokott ~]# ifdown ens33
成功斷開設(shè)備 \'ens33\'。
[root@lokott ~]# ifconfig ens33
ens33: flags=4163 mtu 1500
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 5048 bytes 329861 (322.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 490 bytes 46907 (45.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163 mtu 1500
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
[root@lokott ~]# ifup ens33
連接已成功激活(D-Bus 活動(dòng)路徑:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@lokott ~]# ifconfig ens33
ens33: flags=4163 mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 5082 bytes 332183 (324.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 514 bytes 50940 (49.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
8.route添加、刪除靜態(tài)路由記錄
[root@lokott ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route add -net 192.168.10.0/24 gw 192.168.122.1
[root@lokott ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.68.2 0.0.0.0 UG 100 0 0 ens33
192.168.10.0 192.168.122.1 255.255.255.0 UG 0 0 0 virbr0
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route del -net 192.168.10.0/24 //刪除路由記錄
[root@lokott ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.68.2 0.0.0.0 UG 100 0 0 ens33
192.168.68.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@lokott ~]# route del default gw 192.168.68.2 //刪除默認(rèn)網(wǎng)關(guān)
[root@lokott ~]# route add default gw 192.168.68.2 //添加默認(rèn)網(wǎng)關(guān)
9.網(wǎng)絡(luò)接口配置文件
[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet //類型為以太網(wǎng)
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp //動(dòng)態(tài)獲取
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33 //網(wǎng)卡名字為ens33
UUID=5785d9d2-2603-4b14-bbfb-d95f6545e260
DEVICE=ens33 //設(shè)置網(wǎng)絡(luò)接口名稱為ens33
ONBOOT=yes //設(shè)置網(wǎng)絡(luò)接口在Linux系統(tǒng)啟動(dòng)時(shí)激活
[root@lokott ~]#
當(dāng)然我們可以設(shè)置靜態(tài)ip地址如下(務(wù)必重啟網(wǎng)絡(luò)服務(wù)):
[root@lokott ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=5785d9d2-2603-4b14-bbfb-d95f6545e260
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.68.130
NETMASK=255.255.255.0
GATEWAY=192.168.68.2
[root@lokott ~]# systemctl restart network
[root@lokott ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.68.130 netmask 255.255.255.0 broadcast 192.168.68.255
inet6 fe80::7eb1:2dde:8a54:6927 prefixlen 64 scopeid 0x20
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
RX packets 5803 bytes 377581 (368.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 552 bytes 55914 (54.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 168 bytes 16496 (16.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 168 bytes 16496 (16.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:71:37:09 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@lokott ~]#
10.域名解析配置文件1)指定為本機(jī)提供DNS解析的服務(wù)器地址信息
[root@lokott ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.68.2
[root@lokott ~]#
2)本地主機(jī)映射文件
[root@lokott ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
? 一般可以將經(jīng)常需要訪問的主機(jī)信息存放在這里,根據(jù)這里的映射表可以查找是否有與之對(duì)應(yīng)的記錄,沒有再去DNS服務(wù)器查詢,從而提高上網(wǎng)速度。但如果錯(cuò)誤添加就會(huì)出現(xiàn)異常。