首頁 > 軟體

Linux基礎網路設定

2020-06-16 17:39:14

檢視Linux網路引數

  • ifconfig—-檢視網路介面
> [root@localhost ~]# ifconfig
eth1      Link encap:Ethernet  HWaddr 00:0C:29:33:4A:0C  
          inet addr:192.168.4.115  Bcast:192.168.4.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe33:4a0c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8133 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5861 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:818322 (799.1 KiB)  TX bytes:835205 (815.6 KiB)

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:14 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:946 (946.0 b)  TX bytes:946 (946.0 b)
  • hostname—-檢視主機名
[root@localhost ~]# hostname
localhost
  • route—-檢視路由表
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     *               255.255.255.0   U     1      0        0 eth1
default         localhost       0.0.0.0         UG    0      0        0 eth1
  • netstat—檢視網路連線情況
    -a 所有, -n 數位, -r路由表,-l監聽,-t TCP, -u UDP, -p 進程號
[root@localhost ~]# netstat -antput 
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:875                 0.0.0.0:*                   LISTEN      2262/rpc.rquotad    
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1963/rpcbind        
tcp        0      0 0.0.0.0:33073               0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:40337               0.0.0.0:*                   LISTEN      2266/rpc.mountd     
tcp        0      0 0.0.0.0:53586               0.0.0.0:*                   LISTEN      2010/rpc.statd      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      2345/sshd           
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2084/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2590/master         
tcp        0      0 0.0.0.0:35161               0.0.0.0:*                   LISTEN      2266/rpc.mountd     
tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN      -                   
tcp        0      0 0.0.0.0:42759               0.0.0.0:*                   LISTEN      2266/rpc.mountd     
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      3041/mysqld         
tcp        0    756 192.168.4.115:22            192.168.4.105:52347         ESTABLISHED 2788/sshd           
tcp        0      0 :::111                      :::*                        LISTEN      1963/rpcbind        
tcp        0      0 :::53461                    :::*                        LISTEN      2266/rpc.mountd     
tcp        0      0 :::22                       :::*                        LISTEN      2345/sshd           
tcp        0      0 ::1:631                     :::*                        LISTEN      2084/cupsd          
tcp        0      0 ::1:25                      :::*                        LISTEN      2590/master         
tcp        0      0 :::60442                    :::*                        LISTEN      2266/rpc.mountd     
  • Ping —-測試連通性
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (123.125.65.78) 56(84) bytes of data.
64 bytes from 123.125.65.78: icmp_seq=1 ttl=56 time=141 ms
64 bytes from 123.125.65.78: icmp_seq=2 ttl=56 time=18.9 ms
64 bytes from 123.125.65.78: icmp_seq=3 ttl=56 time=17.3 ms
64 bytes from 123.125.65.78: icmp_seq=4 ttl=56 time=19.9 ms
  • Nslookup—-測試DNS域名解析
[root@localhost ~]# nslookup
> www.baidu.com 
Server:     192.168.1.1
Address:    192.168.1.1#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
Name:   www.a.shifen.com
Address: 123.125.65.78
Name:   www.a.shifen.com
Address: 123.125.65.82
  • Traceroute—路由跟蹤
[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (119.75.218.70), 30 hops max, 60 byte packets
 1  192.168.40.1 (192.168.40.1)  1.223 ms  3.555 ms  4.307 ms
 2  117.100.128.1 (117.100.128.1)  8.126 ms  8.927 ms  8.623 ms
 3  124.205.97.48 (124.205.97.48)  8.430 ms  8.248 ms  8.100 ms
 4  219.239.249.25 (219.239.249.25)  7.906 ms  7.953 ms  7.887 ms
 5  14.197.177.9 (14.197.177.9)  7.874 ms 14.197.178.45 (14.197.178.45)  7.672 ms 14.197.178.41 (14.197.178.41)  7.485 ms
 6  14.197.253.49 (14.197.253.49)  11.445 ms * *

設定linux網路引數

一. 臨時設定:一般用命令來設定,馬上生效,重新啟動無效

  • 修改網絡卡的地址,狀態 ifconfig 網路介面名稱 IP地址 netmask 子網掩碼
[root@localhost ~]# ifconfig eth1 192.168.40.20 netmask 255.255.255.0
  • ifconfig 網路介面名稱 ip地址/子網掩碼長度
[root@localhost ~]# ifconfig eth1 192.168.40.20/32
  • ifconfig eth0 down/up 或者 ifdown/up eth0 或者service network
    restart
[root@localhost ~]# ifconfig eth1 down
[root@localhost ~]# ifconfig eth1 up
[root@localhost ~]# ifdown eth1
[root@localhost ~]# ifup eth1
[root@localhost ~]# service network restart
  • ifconfig eth0:0 10.1.1.1 設定啟動後生效 複製eth0的組態檔給eth0:0,並進行設定
[root@localhost ~]# ifconfig eth1:0 10.1.1.1 
  • 新增靜態路由:Route add –net ip地址/掩碼 gw 下一跳地址
[root@localhost ~]# route add -net 192.148.40.0/32 gw 192.148.40.1
  • 刪除路由:route del –net 目標網段/掩碼
[root@localhost ~]# route del -net 192.148.40.0/32
  • 新增預設閘道器:route add default gw ip地址 刪除預設閘道器:route del default gw ip地址
[root@localhost ~]# route add default gw 192.168.4.1 
[root@localhost ~]# route del default gw 192.168.4.1
  • Hostname 新主機名 修改後可輸入bash呼叫新的Shell檢視,或者exit登出後重新登入
[root@localhost ~]# hostname linuxidc
[root@localhost ~]# bash
[root@linuxidc ~]# 

二. 固定設定:一般是更改組態檔,需要重新啟動生效

  • 網絡卡的組態檔:/etc/sysconfig/network-scripts/ifcfg-ethX。設定ip地址、子網掩碼、預設閘道器等。
DEVICE=eth0        #裝置名字
ONBOOT=yes        #是否隨系統啟動而啟動
BOOTPROTO=static (dhcp) # 網路引數設定方式
IPADDR=192.168.4.1    #ip地址
NETMASK=255.255.255.0  # 子網掩碼
GATEWAY=192.168.4.2    # 預設閘道器
DNS1=202.106.0.20      # DNS地址
  • 主機名的組態檔:/etc/sysconfig/network
[root@linuxidc ~]# vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=localhost
  • DNS伺服器組態檔:/etc/resolv.conf
[root@linuxidc ~]# vim /etc/resolv.conf

# Generated by NetworkManager
nameserver 192.168.1.1
nameserver 192.168.4.1
  • ip與主機對映:/etc/hosts
[root@linuxidc ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  • 重新啟動所有網絡卡:service network restart
[root@linuxidc ~]# service network restart
  • 重新啟動個別網絡卡:ifdown eth0 ifup eth0
[root@linuxidc ~]# ifdown eth1
[root@linuxidc ~]# ifup eth1

本文永久更新連結地址http://www.linuxidc.com/Linux/2016-06/132393.htm


IT145.com E-mail:sddin#qq.com