您好,登錄后才能下訂單哦!
查看網絡設置
測試網絡連接
? 本文主要介紹Linux中一些基礎常用的Linux網絡設置命令與一些腳本文件配置步驟。
格式:ifconfig [網絡接口名] [ip地址](子網掩碼)
[root@lokott ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<link>
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<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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<UP,BROADCAST,MULTICAST> 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<UP,BROADCAST,RUNNING,MULTICAST> 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<link>
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
格式: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 ~]#需要重啟才可以設置成功,另一種方法是使用hostnamectl set-hostname 主機名^c
[root@lokott ~]#reboot
[root@local ~]# hostnamectl set-hostname lokott
[root@local ~]# su
[root@lokott ~]# 換為lokott了^c
-n:數字顯示(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
-n 序列號
-p 端口號
-a 所有服務端口
-r 顯示路由表信息
-l 顯示處于監聽狀態的網絡連接及端口信息
-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
[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 ~]#
[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
補充:在Windows系統的dos界面也一樣適用
1.臨時配置——調試網絡時使用
2.固定配置——更改配置文件,需要重啟network服務或重啟主機后才會生效,相當于永久配置。
[root@lokott ~]# ifconfig ens33:0 192.168.155.55 //虛接口配置
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<UP,BROADCAST,RUNNING,MULTICAST> 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 <設備名>
[root@lokott ~]# ifdown ens33
成功斷開設備 'ens33'。
[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:56:d3:4a txqueuelen 1000 (Ethernet)
[root@lokott ~]# ifup ens33
連接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@lokott ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> 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<link>
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
[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 //刪除默認網關
[root@lokott ~]# route add default gw 192.168.68.2 //添加默認網關
[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet" //類型為以太網
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp" //動態獲取
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" //網卡名字為ens33
UUID="5785d9d2-2603-4b14-bbfb-d95f6545e260"
DEVICE="ens33" //設置網絡接口名稱為ens33
ONBOOT="yes" //設置網絡接口在Linux系統啟動時激活
[root@lokott ~]#
當然我們可以設置靜態ip地址如下(務必重啟網絡服務):
[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<UP,BROADCAST,RUNNING,MULTICAST> 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<link>
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<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
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<UP,BROADCAST,MULTICAST> 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 ~]#
1)指定為本機提供DNS解析的服務器地址信息
[root@lokott ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.68.2
[root@lokott ~]#
2)本地主機映射文件
[root@lokott ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
? 一般可以將經常需要訪問的主機信息存放在這里,根據這里的映射表可以查找是否有與之對應的記錄,沒有再去DNS服務器查詢,從而提高上網速度。但如果錯誤添加就會出現異常。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。