歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Linux基礎網絡設置

Linux基礎網絡設置

日期:2017/3/6 9:39:42   编辑:學習Linux

Linux基礎網絡設置


Linux基礎網絡設置


查看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 bkjia
[root@localhost ~]# bash
[root@bkjia ~]# 

二. 固定配置:一般是更改配置文件,需要重啟生效

  • 網卡的配置文件:/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@bkjia ~]# vim /etc/sysconfig/network

NETWORKING=yes
HOSTNAME=localhost
  • DNS服務器配置文件:/etc/resolv.conf
[root@bkjia ~]# vim /etc/resolv.conf

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

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

本文永久更新鏈接地址:

http://xxxxxx/Linuxjc/1135369.html TechArticle

Copyright © Linux教程網 All Rights Reserved