常用网络管理操作

前言:

前面学习了对linux的基本的操作,比如用户与用户组的管理、文件权限管理、文件查找、压缩与解压文件、环境变量的配置、管道以及常见的linux资源管理(如进程,时间,资源管理),通过不断的练习以及网上查找相关资料进行阅读,基本上掌握以上的操作,但是(又是这个可恨的但是)前面的学习只是满足操作一个不连接互联网的linux系统,接下来学习一些常用的网络管理命令。

1.ifconfig命令:

Linux下ifconfig命令可以查看/使能/禁用网卡,修改IP地址、MAC地址、子网掩码等:

查看使能的网卡:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ ifconfig  
查看所有网卡,包括禁用的:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ ifconfig -a  
使能网卡:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ifconfig eth0 up  
禁用网卡:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ifconfig eth0 down  
设置eth0的IP,使用默认子网掩码:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ifconfig eth0 192.168.1.110  
  2. [user1@xianzan dir]$ ifconfig  
  3. eth0      Link encap:Ethernet  HWaddr 00:0C:29:F0:3E:B4    
  4.           inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0  
一张网卡eth0绑定多个IP:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ifconfig eth0:1 192.168.1.120  
  2. [user1@xianzan dir]$ ifconfig  
  3. eth0      Link encap:Ethernet  HWaddr 00:0C:29:F0:3E:B4    
  4.           inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0  
  5. eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:F0:3E:B4    
  6.           inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0  
为eth0网卡设置ip与网关:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ifconfig eth0 192.168.111 netmask 255.255.255.255  
  2. [user1@xianzan dir]$ ifconfig  
  3. eth0      Link encap:Ethernet  HWaddr 00:0C:29:F0:3E:B4    
  4.           inet addr:192.168.0.111  Bcast:192.168.0.111  Mask:255.255.255.255  
设置MAC地址:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ifconfig eth0 down  
  2. [user1@xianzan dir]$ sudo ifconfig eth0 hw ether 00:0C:29:F0:3E:BB  
  3. [user1@xianzan dir]$ sudo ifconfig eth0 up  
  4. [user1@xianzan dir]$ ifconfig   
  5. eth0      Link encap:Ethernet  HWaddr 00:0C:29:F0:3E:BB    

注意:

设置网卡的MAC地址的时候要先禁用网卡。


2.route命令:

Linux下route命令可以查看、添加、删除网关:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo route   
  2. Kernel IP routing table  
  3. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  4. 192.168.122.0   *               255.255.255.0   U     0      0        0 virbr0  
  5. [user1@xianzan dir]$ sudo route -n  
  6. Kernel IP routing table  
  7. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  8. 192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0  

说明:

route -n对于默认网关以数字形式显示。

添加默认路由:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo route add default gw 192.168.1.1  
  2. [user1@xianzan dir]$ route -n  
  3. Kernel IP routing table  
  4. Destination     Gateway         Genmask         Flags Metric Ref    Use Iface  
  5. 192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0  
  6. 192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0  
  7. 0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0  
删除默认路由:

[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo route del default  
上面直接用route命令添加路由,机器重启或者网卡重启后路由就失效。

设置永久生效的方法是修改配置文件:
 在 /etc/rc.local里添加:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo vim /etc/rc.local   


3.ping命令:

Linux下ping命令可以诊断网络连接情况:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ ping 192.168.1.110  
  2. PING 192.168.1.110 (192.168.1.110) 56(84) bytes of data.  
  3. 64 bytes from 192.168.1.110: icmp_seq=1 ttl=64 time=9.58 ms  
  4. 64 bytes from 192.168.1.110: icmp_seq=2 ttl=64 time=0.058 ms  
常用ping的选项:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ping -c 1 -s 1 4.2.2.2 -I eth0  
说明:
-c:ping的次数;
-s:指定字节数;
-I:知道网卡。

4.ip命令:

Linux下的IP命令包含了ifconfig和route命令的所有功能,提供更加强大的网络管理:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ip link set dev eth0 up/down  
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ip link set dev eth0 address 00:01:4f:00:15:f1  
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ip addr add 192.168.3.2/24 brd + dev eth0 labeleth0:1  
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ip address show  
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ip addr del 192.168.3.2/24 brd + dev eth0 labeleth0:1  
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ip route show[user1@xianzan dir]$ sudo ip route del default  
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo ip route add default via 192.168.2.1  
 
 

5.Linux网络配置文件:

上述直接用命令对网络进行配置后只是当前系统临时生效,系统重启后配置将会丢失。如果想让这些配置保存下来,则需要保存到系统的相应网卡配置文件中去。Linux网卡配置文件 /etc/sysconfig/network-scripts/ifcfg-eth0
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0   
Linux DNS配置文件:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ vim /etc/resolv.conf   

6.Linux网络监控netstat命令:

netstat这个命令常用在网络监控方面。利用这个命令,可以查看当前系统监听的服务和已经建立的服务,以及相应的端口、协议等信息。netstat参数虽然很多,但是常用的不多。主要有以下几个参数:netstat -[atunlp]
-a :all,表示列出所有的连接,服务监听,Socket资料-t : tcp,列出tcp协议的服务-u :udp,列出udp协议的服务-n :port number, 用端口号来显示-l : listening,列出当前监听服务-p :program,列出服务程序的PID
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo netstat -tlp  
  2. Active Internet connections (only servers)  
  3. Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name     
  4. tcp        0      0 *:sunrpc                    *:*                 

7.Linux网络防火墙iptables:

防火墙是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种。无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘。而我们的任务就是需要去定义到底防火墙如何工作,这就是防火墙的策略,规则,以达到让它对出入网络的IP、数据进行检测。
防火墙服务关闭:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo service iptables stop  
  2. iptables:将链设置为政策 ACCEPT:filter                    [确定]  
  3. iptables:清除防火墙规则:                                 [确定]  
  4. iptables:正在卸载模块:                                   [确定]  
防火墙服务开启:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo service iptables start  
  2. iptables:应用防火墙规则:                                 [确定]  
列出表中的所有规则:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo iptables -L  
清除链中的所有规则:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo iptables -F  
清除用户自定义表:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo iptables -X  
计数器清零:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo iptables -Z  
允许数据输出:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo iptables -P OUTPUT ACCEPT  
说明:-P修改默认的表的设置,OUTPUT:数据输出,ACCEPT:允许。
通过网卡的数据输入丢掉:
[plain]  view plain  copy
  1. <span style="font-size:14px;"></span><pre name="code" class="plain"><span style="font-size:14px;">[user1@xianzan dir]</span>$ sudo iptables -P INPUT DROP<span style="font-size:14px;"><span style="font-family: Arial, Helvetica, sans-serif;"> </span></span>  
 DROP:丢掉数据包。
 
修改防火墙策略:
[plain]  view plain  copy
  1. <span style="font-size:14px;"></span><pre name="code" class="plain"><span style="font-size:14px;">[user1@xianzan dir]</span>$ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT  
 
  说明:
 
-A: 添加防火墙规则    -p:TCP协议    --d:指定端口     -j:表示设置策略
这样设置的效果为:启动对应的tcp协议的22端口打开,SSH远程连接软件可以连接到你的服务主机。
同理,防火墙接收http协议的80端口数据:
[plain]  view plain  copy
  1. <span style="font-size:14px;"></span><pre name="code" class="plain"><span style="font-size:14px;">[user1@xianzan dir]</span>$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT  
 能ping到别的主机,主要是对方防火墙已经允许相应的icmp的数据流进来:
 
[plain]  view plain  copy
  1. <span style="font-size:14px;"></span><pre name="code" class="plain"><span style="font-size:14px;">[user1@xianzan dir]</span>$ sudo iptables -A INPUT -p icmp -j ACCEPT  
 保存已经修改的防火墙策略:
 
[plain]  view plain  copy
  1. <span style="font-size:14px;"></span><pre name="code" class="plain"><span style="font-size:14px;">[user1@xianzan dir]</span>$ sudo service iptables save  
 
 
查看防火墙表的状态:
[plain]  view plain  copy
  1. [user1@xianzan dir]$ sudo service iptables status  
iptables详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值