理論:linux網(wǎng)絡(luò)服務(wù)-linux網(wǎng)絡(luò)設(shè)置-創(chuàng)新互聯(lián)

配置路由,追蹤路由

創(chuàng)新互聯(lián)從2013年創(chuàng)立,先為象州等服務(wù)建站,象州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為象州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

前言

  • 查看網(wǎng)絡(luò)設(shè)置

  • 測試網(wǎng)絡(luò)連接

  • 使用網(wǎng)絡(luò)配置命令

  • 修改網(wǎng)絡(luò)配置文件

一 :查看網(wǎng)絡(luò)接口信息 ifconfig

1.1 查看所有活動網(wǎng)接口的信息

  • 執(zhí)行ifconfig 命令

1.2 查看指定網(wǎng)絡(luò)接口信息

[root@localhost ~]# ifconfig 網(wǎng)絡(luò)接口名
[root@localhost ~]# ifconfig ens33
'ens33第一塊以太網(wǎng)卡的名稱': flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        'inet IP地址 192.168.139.153'  'netmask 子網(wǎng)掩碼 255.255.255.0'  'broadcast 廣播地址 192.168.139.255'
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether '00:0c:29:d6:c0:8a MAC地址'  txqueuelen 1000  (Ethernet)
        RX packets 28803  bytes 2100162 (2.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 21495  bytes 3147424 (3.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

網(wǎng)卡名稱ens33可以修改,修改時機(jī)是在安裝系統(tǒng)時修改網(wǎng)卡名稱

二 : 查看主機(jī)名稱 hostname

2.1 hostname命令

  • 查看或設(shè)置當(dāng)前主機(jī)名

  • [root@localhost ~]# hostname

2.2 示例

[root@localhost ~]# hostname 
localhost主機(jī)名.'localdomain含義是域,后期會提到'
[root@localhost ~]# hostnamectl --help
hostnamectl [OPTIONS...] COMMAND ...

Query or change system hostname.

  -h --help              Show this help
     --version           Show package version
     --no-ask-password   Do not prompt for password
  -H --host=[USER@]HOST  Operate on remote host
  -M --machine=CONTAINER Operate on local container
     --transient         Only set transient hostname
     --static            Only set static hostname
     --pretty            Only set pretty hostname

Commands:
  status                 Show current hostname settings
  set-hostname NAME      Set system hostname
  set-icon-name NAME     Set icon name for host
  set-chassis NAME       Set chassis type for host
  set-deployment NAME    Set deployment environment for host
  set-location NAME      Set location for host
  [root@localhost ~]# hostnamectl set-hostname gsy
[root@localhost ~]# su
[root@gsy ~]# 

設(shè)置主機(jī)名的第二種方法,即修改配置文件

/etc/hostname 配置文件

[root@gsy ~]# hostname
gsy
[root@gsy ~]# vim /etc/sysconfig/network    
'這個配置文件的修改適用于centos 6,不適用于centos 7'
# Created by anaconda
HOST=mysql
~ 

三 :查看路由條目 route

3.1 route 命令

  • 查看或設(shè)置主機(jī)中的路由表信息
  • route [-n]
  • -n 以數(shù)字形式去顯示

3.2 示例

[root@965 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.139.2   0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@965 ~]# 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.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

Destination 網(wǎng)段 Gateway 網(wǎng)關(guān) Genmask 子網(wǎng)掩碼 metric 度量值

在centos 7中,route 與route -n 的區(qū)別在于默認(rèn)網(wǎng)絡(luò)改為數(shù)字0.0.0.0

在centos 6中,route 與route -n 具體區(qū)別如下顯示

[root@gsy ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.139.0   *               255.255.255.0   U     1      0        0 eth0
default         192.168.139.2   0.0.0.0         UG    0      0        0 eth0
[root@gsy ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.139.0   0.0.0.0         255.255.255.0   U     1      0        0 eth0
0.0.0.0         192.168.139.2   0.0.0.0         UG    0      0        0 eth0

四 : 查看網(wǎng)絡(luò)連接情況 netstat

4.1 netstat 命令

  • 查看系統(tǒng)的網(wǎng)絡(luò)連接狀態(tài)、路由表、接口統(tǒng)計(jì)等信息
  • netstat 【選項(xiàng)】

4.2 常用選項(xiàng)

選項(xiàng) 解釋
-a顯示所有
-n顯示序列號
-p顯示端口協(xié)議
-t顯示TCP協(xié)議
-u顯示UDP協(xié)議
-r顯示route
描述 DESCRIPTION
       Netstat 程序顯示Linux網(wǎng)絡(luò)子系統(tǒng)的信息。 輸出信息的類型是由第一個參數(shù)控制的,就像這樣:

   (none)
       無選項(xiàng)時,                               netstat                               顯示打開的套接字.
       如果不指定任何地址族,那么打印出所有已配置地址族的有效套接字。

   --route , -r
       顯示內(nèi)核路由表。

   --groups , -g
       顯示IPv4 和 IPv6的IGMP組播組成員關(guān)系信息。

   --interface=iface , -i
       顯示所有網(wǎng)絡(luò)接口列表或者是指定的 iface 。

   --masquerade , -M
       顯示一份所有經(jīng)偽裝的會話列表。

   --statistics , -s
       顯示每種協(xié)議的統(tǒng)計(jì)信息。

選項(xiàng) OPTIONS
   --verbose , -v
       詳細(xì)模式運(yùn)行。特別是打印一些關(guān)于未配置地址族的有用信息。

   --numeric , -n
       顯示數(shù)字形式地址而不是去解析主機(jī)、端口或用戶名。

   --numeric-hosts
       顯示數(shù)字形式的主機(jī)但是不影響端口或用戶名的解析。

   --numeric-ports
       顯示數(shù)字端口號,但是不影響主機(jī)或用戶名的解析。

 --numeric-users
       顯示數(shù)字的用戶ID,但是不影響主機(jī)和端口名的解析。

   --protocol=family , -A
       指定要顯示哪些連接的地址族(也許在底層協(xié)議中可以更好地描述)。                             family
       以逗號分隔的地址族列表,比如 inet, unix, ipx, ax25, netrom, 和 ddp。 這樣和使用 --inet,  --unix
       (-x), --ipx, --ax25, --netrom, 和 --ddp 選項(xiàng)效果相同。

       地址族 inet 包括raw, udp 和tcp 協(xié)議套接字。

   -c, --continuous
       將使 netstat 不斷地每秒輸出所選的信息。

   -e, --extend
       顯示附加信息。使用這個選項(xiàng)兩次來獲得所有細(xì)節(jié)。

   -o, --timers
       包含與網(wǎng)絡(luò)定時器有關(guān)的信息。

   -p, --program
       顯示套接字所屬進(jìn)程的PID和名稱。

   -l, --listening
       只顯示正在偵聽的套接字(這是默認(rèn)的選項(xiàng))

   -a, --all
       顯示所有正在或不在偵聽的套接字。加上 --interfaces 選項(xiàng)將顯示沒有標(biāo)記的接口。

   -F
       顯示FIB中的路由信息。(這是默認(rèn)的選項(xiàng))

   -C
       顯示路由緩沖中的路由信息。
 delay
       netstat將循環(huán)輸出統(tǒng)計(jì)信息,每隔 delay 秒。
[root@965 ~]# 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      1296/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1116/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1117/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1300/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      13953/sshd: root@pt 
tcp        0     36 192.168.139.153:22      192.168.139.1:64738     ESTABLISHED 13953/sshd: root@pt 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1116/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1117/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1300/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      13953/sshd: root@pt

sshd 指遠(yuǎn)程訪問

可以結(jié)合檢索命令去針對性查看

[root@965 ~]# netstat -natp | grep "22"
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1296/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1116/sshd           
tcp        0     36 192.168.139.153:22      192.168.139.1:64738     ESTABLISHED 13953/sshd: root@pt 
tcp6       0      0 :::22                   :::*                    LISTEN      1116/sshd  
[root@965 ~]# netstat -natp | grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1116/sshd           
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      13953/sshd: root@pt 
tcp        0     36 192.168.139.153:22      192.168.139.1:64738     ESTABLISHED 13953/sshd: root@pt 
tcp6       0      0 :::22                   :::*                    LISTEN      1116/sshd           
tcp6       0      0 ::1:6010                :::*                    LISTEN      13953/sshd: root@pt
[root@965 ~]# netstat -naup 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
udp        0      0 192.168.139.153:40345   192.168.139.2:53        ESTABLISHED 129301/hostname     
udp        0      0 0.0.0.0:11836           0.0.0.0:*                           7860/dhclient       
udp        0      0 0.0.0.0:44612           0.0.0.0:*                           707/avahi-daemon: r 
udp      832      0 192.168.139.153:56951   192.168.139.2:53        ESTABLISHED 124308/ssh          
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1296/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1296/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           7860/dhclient       
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           707/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           732/chronyd         
udp6       0      0 ::1:323                 :::*                                732/chronyd         
udp6       0      0 :::49510                :::*                                7860/dhclient       
[root@965 ~]# netstat -naup | grep 22
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1296/dnsmasq
[root@965 ~]# netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.139.2   0.0.0.0         UG        0 0          0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U         0 0          0 ens33
[root@965 ~]# netstat -r
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.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U         0 0          0 ens33

五 : 跟蹤數(shù)據(jù)包 traceroute(中文譯路由跟蹤)

5.1 traceroute 命令

  • 測試從當(dāng)前主機(jī)到目的主機(jī)之間經(jīng)過的網(wǎng)絡(luò)節(jié)點(diǎn)
  • traceroute 目標(biāo)主機(jī)地址

5.2 實(shí)驗(yàn)示例

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

linux中網(wǎng)卡配置

[root@965 network-scripts]# vim /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"
UUID="0f432513-5d7a-455c-88b4-257a9a1dbb45"
DEVICE="ens33"
ONBOOT="yes"

修改為

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"  '將原來的dhcp自動分配ip地址協(xié)議改為靜態(tài)協(xié)議'
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="0f432513-5d7a-455c-88b4-257a9a1dbb45"
DEVICE="ens33"
ONBOOT="yes"
IPADDR=192.168.10.10    'ip地址為192.168.10.10'
NETMASK=255.255.255.0   '子網(wǎng)掩碼為255.255.255.0'
GATEWAY=192.168.10.1    '網(wǎng)關(guān)為192.168.10.1'

:wq

配置完畢后重啟網(wǎng)絡(luò)服務(wù)

[root@965 network-scripts]# systemctl restart network
[root@965 network-scripts]# service network restart 
Restarting network (via systemctl):                [  確定  ]
[root@965 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.10  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
        RX packets 121888  bytes 9724226 (9.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 161138  bytes 29666777 (28.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

host1 配置完畢,接下來配置host2

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

然后配置R1和R2

R1#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R1(config)#int f0/0
R1(config-if)#ip add 192.168.10.1 255.255.255.0
R1(config-if)#no shut
*Mar  1 00:12:04.515: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar  1 00:12:05.515: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R1(config-if)#int f0/1
R1(config-if)#ip add 192.168.20.1 255.255.255.0
R1(config-if)#no shut
*Mar  1 00:12:19.323: %LINK-3-UPDOWN: Interface FastEthernet0/1, changed state to up
*Mar  1 00:12:20.323: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/1, changed state to up
R1(config-if)#ip route 0.0.0.0 0.0.0.0 192.168.20.20
R2#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
R2(config)#int f0/0
R2(config-if)#ip add 192.168.20.20 255.255.255.0
R2(config-if)#no shut
R2(config-if)#int f0/1
*Mar  1 00:12:46.323: %LINK-3-UPDOWN: Interface FastEthernet0/0, changed state to up
*Mar  1 00:12:47.323: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/0, changed state to up
R2(config-if)#ip add 192.168.30.1 255.255.255.0
R2(config-if)#no shut
R2(config-if)#ip route 0.0.0.0 0.0.0.0 192.168.20.1
[root@965 ~]# ping 192.168.30.30
PING 192.168.30.30 (192.168.30.30) 56(84) bytes of data.
64 bytes from 192.168.30.30: icmp_seq=3 ttl=126 time=47.3 ms
64 bytes from 192.168.30.30: icmp_seq=4 ttl=126 time=43.7 ms
64 bytes from 192.168.30.30: icmp_seq=5 ttl=126 time=43.4 ms
64 bytes from 192.168.30.30: icmp_seq=6 ttl=126 time=42.4 ms
^C
--- 192.168.30.30 ping statistics ---
6 packets transmitted, 4 received, 33% packet loss, time 5013ms
rtt min/avg/max/mdev = 42.432/44.244/47.386/1.881 ms
[root@965483 ~]# traceroute 192.168.30.30
traceroute to 192.168.30.30 (192.168.30.30), 30 hops max, 60 byte packets
 1  gateway (192.168.10.1)  3.021 ms  12.829 ms  23.697 ms
 2  192.168.20.20 (192.168.20.20)  33.630 ms  43.493 ms  54.322 ms
 3  * * *
 4  * * *
 5  * * *
 6  * * *
 7  * * *
 8  * * *
 9  * * *
10  * * *
11  * * *
12  * * *
13  * * *
14  * * *
15  * * *
16  * * *
17  * * *
18  * * *
19  * * *
20  * * *
21  * * *
22  * * *
23  * * *
24  * * *
25  * * *
26  * * *
27  * * *
28  * * *
29  * * *
30  * * *

啟用server中的相關(guān)配置,可以實(shí)現(xiàn)遠(yuǎn)程終端鏈接

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置
理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

中間IP地址自動改變,要及時調(diào)整

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

六 : 域名解析 nslookup

6.1 nslookup 命令

  • 測試DNS命令
  • nslookup 目標(biāo)主機(jī)地址 [DNS服務(wù)器地址]

6.2 示例

[root@localhost ~]# nslookup www.google.com
Server:     192.168.139.2   'dns服務(wù)器地址'
Address:    192.168.139.2#53

Non-authoritative answer:   '反饋的解析結(jié)果'
Name:   www.google.com
Address: 74.86.226.234

windows中的檢測

C:\Users\GSY>nslookup www.google.com
服務(wù)器:  ns.gwbnnj.net.cn  'DNS服務(wù)器地址'
Address:  211.162.31.80

非權(quán)威應(yīng)答:
名稱:    www.google.com
Addresses:  2404:6800:4012::2004    'ipv6的地址'
          74.86.226.234 '反饋的結(jié)果'
C:\Users\GSY>nslookup www.taobao.com
服務(wù)器:  ns.gwbnnj.net.cn
Address:  211.162.31.80

非權(quán)威應(yīng)答:
名稱:    www.taobao.com.danuoyi.tbcache.com   'taobao實(shí)際的域名'
Addresses:  2409:8c28:2808:5:3::3fa
          2409:8c28:801:3:3::3fa
          2409:8c28:2808:5:3::3f9
          2409:8c28:801:3:3::3f9
          111.3.79.235
          111.3.79.234
          211.138.124.237
Aliases:  www.taobao.com        '別名'

服務(wù)器指的是DNS解析服務(wù)器,后面nj是南京的意思

可以把解析后的地址復(fù)制到瀏覽器中去訪問,百度的可以,但是淘寶的不行,這是因?yàn)樽隽税踩雷o(hù),dns服務(wù)器也做了相應(yīng)的防護(hù)

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

七 設(shè)置網(wǎng)絡(luò)參數(shù)的方式

7.1 臨時配置—— 使用命令調(diào)整網(wǎng)絡(luò)參數(shù)

  • 簡單、快速,可直接修改運(yùn)行中的網(wǎng)絡(luò)參數(shù)
  • 一般只適合在調(diào)試網(wǎng)絡(luò)的過程中使用
  • 系統(tǒng)重啟以后,所做的修改將會失效

7.2 固定設(shè)置——— 通過配置文件修改網(wǎng)絡(luò)參數(shù)

  • 修改各項(xiàng)參數(shù)的配置文件
  • 適合對服務(wù)器設(shè)置固定參數(shù)時使用
  • 需要重載網(wǎng)絡(luò)服務(wù)或者重啟以后才會生效

7.3 設(shè)置網(wǎng)絡(luò)接口參數(shù)命令 ifconfig

  • 設(shè)置網(wǎng)絡(luò)接口的IP地址、子網(wǎng)掩碼

    ifconfig 接口名(網(wǎng)卡名) ip地址 [netmask 子網(wǎng)掩碼]

    ifconfig 網(wǎng)絡(luò)接口 ip地址[/掩碼長度]

  • 禁用或重新激活網(wǎng)卡

    ifconfig 網(wǎng)絡(luò)接口 up

    ifconfig 網(wǎng)絡(luò)接口 down

  • 設(shè)置虛擬網(wǎng)絡(luò)接口

    ifconfig 接口名:序號 IP地址

接口名 ens33

[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.139.157'ip地址'  netmask 255.255.255.0 '子網(wǎng)掩碼' broadcast 192.168.139.255  '廣播地址'
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
        RX packets 4494  bytes 300077 (293.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 144  bytes 15965 (15.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

臨時修改,只用與維護(hù)時使用,

序號,0-123456,相當(dāng)于邏輯接口,

當(dāng)一臺主機(jī)需要用多臺IP地址時,可以使用邏輯接口配置

[root@localhost ~]# ifconfig ens33 8.8.8.8
[root@localhost ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        'inet 8.8.8.8  netmask 255.0.0.0  broadcast 8.255.255.255'
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
        RX packets 4544  bytes 305199 (298.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 162  bytes 19317 (18.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig ens33 8.8.8.8/24
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        'inet 8.8.8.8  netmask 255.255.255.0  broadcast 8.8.8.255'
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
        RX packets 4568  bytes 308317 (301.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 170  bytes 21475 (20.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig ens33:1 192.168.9.9
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 8.8.8.8  netmask 255.255.255.0  broadcast 8.8.8.255
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
        RX packets 4663  bytes 315910 (308.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 197  bytes 25003 (24.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.9.9  netmask 255.255.255.0  broadcast 192.168.9.255
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
[root@localhost ~]# ifconfig ens33:1 down   ‘關(guān)掉ens33:1’
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 8.8.8.8  netmask 255.255.255.0  broadcast 8.8.8.255
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
        RX packets 4683  bytes 317292 (309.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 203  bytes 25510 (24.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig ens33:1 up '再次開啟會發(fā)現(xiàn)無法找到這個地址'
SIOCSIFFLAGS: 無法指定被請求的地址
[root@localhost ~]# ifconfig ens33 down '關(guān)掉物理網(wǎng)卡ens33'
[root@localhost ~]# ifconfig 
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 1672  bytes 144872 (141.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1672  bytes 144872 (141.4 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:e1:7d:d2  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@localhost ~]# ifconfig ens33 up   '可以成功開啟'
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.139.157  netmask 255.255.255.0  broadcast 192.168.139.255
        inet6 fe80::413b:c9ad:e0e:1afc  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:d6:c0:8a  txqueuelen 1000  (Ethernet)
        RX packets 4739  bytes 322970 (315.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 229  bytes 29270 (28.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

邏輯子接口(例:ens33:1)的down相當(dāng)于把IP地址釋放掉了

八 :設(shè)置路由記錄 route

8.1 添加到指定網(wǎng)段的路由記錄

  • route add -net 網(wǎng)段地址 gw IP地址

8.2 刪除到網(wǎng)段的路由記錄

  • route del -net 網(wǎng)段地址 gw IP地址

8.3 刪除路由表中的默認(rèn)網(wǎng)關(guān)記錄

  • route del defalut gw IP地址

8.4 向路由表中添加默認(rèn)網(wǎng)關(guān)記錄

  • route add default gw IP地址

gw 下一跳地址

-net 目標(biāo)網(wǎng)段

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
'0.0.0.0         192.168.139.2   0.0.0.0         UG    100    0        0 ens33
默認(rèn)網(wǎng)關(guān),即可以變相理解為默認(rèn)路由
[root@localhost ~]# route add -net 192.168.10.0 gw 192.168.139.157
SIOCADDRT: 無效的參數(shù)
[root@localhost ~]# route add -net 192.168.10.0/24 gw 192.168.139.157
[root@localhost ~]# 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.10.0    localhost.local 255.255.255.0   UG    0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.139.2   0.0.0.0         UG    100    0        0 ens33
192.168.10.0    192.168.139.157 255.255.255.0   UG    0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@localhost ~]# route add -net 192.168.100.0/24 gw 192.168.139.157
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway '下一跳'    Genmask     Flags Metric Ref    Use Iface
0.0.0.0         192.168.139.2   0.0.0.0         UG    100    0        0 ens33
192.168.10.0    192.168.139.157 255.255.255.0   UG    0      0        0 ens33
'192.168.100.0   192.168.139.157 255.255.255.0   UG    0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@localhost ~]# route del -net 192.168.100.0/24 gw 192.168.139.157
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.139.2   0.0.0.0         UG    100    0        0 ens33
192.168.10.0    192.168.139.157 255.255.255.0   UG    0      0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.139.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

九 : 修改主機(jī)名 hostname

9.1 hostname 命令

  • 設(shè)置主機(jī)名

hostname 主機(jī)名稱

9.2 示例

[root@localhost ~]# hostname gsy    '第一種方法'
[root@localhost ~]# su
[root@gsy ~]# hostnamectl set-hostname hostname '第二種方法'
[root@gsy ~]# su    '或者bash'
[root@hostname ~]# 

第三種方法:vim /etc/hostname 修改主機(jī)名,然后保存,關(guān)機(jī)重啟即可

第四種方法:vim /etc/sysconfig/networkl 修改主機(jī)名,關(guān)機(jī)重啟(不適用于7,適用于6)

十 : 網(wǎng)絡(luò)接口配置文件

10.1 /etc/sysconfig/network-scripts/ 目錄下

  • ifcfg-ens33:第一塊以太網(wǎng)卡的配置文件
  • ......
[root@dabendan ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE="Ethernet" '類型,以太網(wǎng)卡'
PROXY_METHOD="none"     '代理方法'
BROWSER_ONLY="no"       '瀏覽器'
BOOTPROTO="dhcp"    '雇傭,ip地址獲取,dhcp獲取'
DEFROUTE="yes"          '默認(rèn)路由'
IPV4_FAILURE_FATAL="no"     'ipv4失敗驗(yàn)證     否'
IPV6INIT="yes"  'ipv6 初始化   開啟'
IPV6_AUTOCONF="yes" 'ipv6自動配置     開啟'
IPV6_DEFROUTE="yes" 'ipv6 默認(rèn)配置 開啟'
IPV6_FAILURE_FATAL="no" 'ipv6失敗驗(yàn)證   否'
IPV6_ADDR_GEN_MODE="stable-privacy" 'ipv6地址協(xié)議模式v6為穩(wěn)定私有'
NAME="ens33"    '名稱'
UUID="0f432513-5d7a-455c-88b4-257a9a1dbb45"
DEVICE="ens33"  '設(shè)備'
ONBOOT="yes"    '在boot內(nèi)核啟動時網(wǎng)卡開啟'

可以修改為靜態(tài)IP地址網(wǎng)卡
BOOTPROTO="static"  '靜態(tài)獲取'
IPADDR=指定ip地址
NETMASK=指定子網(wǎng)掩碼
GAREWAY=指定網(wǎng)關(guān)

10.2 重啟 network 網(wǎng)絡(luò)服務(wù)

  • [root@localhost ~]# systemctl restart network

10.3 禁用網(wǎng)絡(luò)接口

  • [root@localhost ~]# ifdown ens33

10.4 啟用網(wǎng)絡(luò)接口

  • [root@localhost ~]# ifup ens33

10.5 雙網(wǎng)卡配置

1.添加網(wǎng)卡,然后自動識別(不需要重啟)

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

禁用啟用網(wǎng)絡(luò)接口也可以在配置文件中修改,就是把ONBOOT=no

十一 : 配置文件

11.1 /etc/sysconfig/network 主機(jī)名稱配置文件

  • 保存全局網(wǎng)絡(luò)配置,主要包括主機(jī)信息
vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=chenggong
:wq
reboot '重啟生效'

這個第四種方法不能用于centos 7 ,在centos 6 中可以使用

11.2 /etc/resolv.conf 域名解析配置文件

  • 保存本機(jī)需要使用DNS服務(wù)器的IP地址
[root@dabendan ~]# vim /etc/resolv.conf

# Generated by NetworkManager
search localdomain'主機(jī)域'
nameserver 192.168.139.2    '默認(rèn)網(wǎng)關(guān),也可以手動配置'

11.3 /etc/hosts 本地主機(jī)映射文件

  • 保存主機(jī)名與IP地址的映射文件
[root@dabendan ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
119.75.218.70   www.baidu.com
  • hosts文件和DNS服務(wù)器的比較
    • 默認(rèn)情況下,系統(tǒng)首先從hosts文件查找解析記錄
    • hosts文件只對當(dāng)前的主機(jī)有效
    • hosts文件可減少DNS查詢過程,從而加快訪問速度

域名解析會優(yōu)先去查看/etc/hosts 映射文件,若是沒有,就會再去找dns服務(wù)器

windows中hosts的文件所在位置
理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

理論:linux網(wǎng)絡(luò)服務(wù) -linux 網(wǎng)絡(luò)設(shè)置

十二:總結(jié):

  • 查看網(wǎng)絡(luò)配置

    • ifconfig、hostname、route、netstat
  • 測試網(wǎng)絡(luò)連接

    • ping、traceroute、nslookup
  • 設(shè)置網(wǎng)絡(luò)地址參數(shù)

    • 臨時配置、永久配置
  • 相關(guān)的配置文件

    • /etc/sysconfig/network主機(jī)名配置文件

    • /etc/resolv.conf 域名解析配置文件

    • /etc/hosts 映射文件

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文標(biāo)題:理論:linux網(wǎng)絡(luò)服務(wù)-linux網(wǎng)絡(luò)設(shè)置-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://bm7419.com/article48/cdiihp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、外貿(mào)建站、全網(wǎng)營銷推廣品牌網(wǎng)站制作定制開發(fā)、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)