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 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

猜你喜欢

转载自www.linuxidc.com/Linux/2016-06/132393.htm