linux基础-第十四单元 Linux网络原理及基础设置

第十四单元 Linux网络原理及基础设置 

三种网卡模式图

使用ifconfig命令来维护网络
ifconfig命令的功能
ifconfig命令的用法举例
使用ifup和ifdown命令启动和停止网卡
ifup命令的功能
ifdown命令的功能
ifup命令的用法举例
ifdown命令的用法举例
网络配置文件
网卡对应的网络配置文件
什么是网络配置
ip命令
ping命令
setup命令
课后作业
【本节内容】
1. 使用ifconfig命令来维护网络(详见linux系统管理P422)
1) 掌握ifconfig命令的功能:显示所有正在启动的网卡的详细信息或设定系统中网卡的IP地址。
2) 灵活应用ifconfig命令设定网卡的IP地址:
例如:修改eth0网卡的IP地址为172.18.48.56和子网掩码255.255.255.128的命令:
ifconfig eth0 172.18.48.56 netmask 255.255.255.128
2. 使用ifup和ifdown命令启动和停止网卡(详见linux系统管理P424)
1) 掌握ifup命令的功能:启动系统上指定的网卡。
2) 掌握ifdown命令的功能:停止系统上指定的网卡。
3) 掌握使用ifup命令启用指定网卡的举例:
例如:启动eth0网卡的命令:ifup eth0
4) 掌握使用ifdown命令停用指定网卡的举例:
只能作用于配置文件
例如:停用eth0网卡的命令:ifdown eth0
3. 网络配置文件 (详见linux系统管理P425)
1) 了解网卡的配置文件存放在/etc/sysconfig/network-scripts目录中,且每个网卡对应的配置文件的文件名以ifcfg-开始,如eth0这个网卡对应的网络配置文件就是ifcfg-eth0。
补:虚拟机新添加网卡没有配置文件需手动添加配置或图形添加网卡
λ 掌握网络配置的定义:所谓的网络配置(设定)就是要修改网卡所对应的网络配置文件,可以通过使用文字编辑器(vi)直接编辑网络配置文件来重新配置网络。

临时配置网络ip地址

 1 [root@template ~]# ifconfig eth0 192.168.1.103/24
 2 
 3 [root@template ~]# ifconfig eth0
 4 eth0   Link encap:Ethernet  HWaddr 00:0C:29:45:CA:65  
 5           inet addr:192.168.1.102  Bcast:192.168.1.255  Mask:255.255.255.0
 6           inet6 addr: fe80::20c:29ff:fe45:ca65/64 Scope:Link
 7           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 8           RX packets:1902597 errors:0 dropped:0 overruns:0 frame:0
 9           TX packets:4990 errors:0 dropped:0 overruns:0 carrier:0
10           collisions:0 txqueuelen:1000 
11           RX bytes:2416682435 (2.2 GiB)  TX bytes:482159 (470.8 KiB)

重启网卡的几种常见方式

 1 法一:
 2 推荐这种方式
 3 [root@template ~]# ifdown && ifup eth0 
 4 
 5 
 6 法二:
 7 尽量不要用这条命令,这条命令会影响所有网卡
 8 
 9 [root@template ~]# service network restart
10 正在关闭接口 eth1: 
11 
12 法三:
13 尽量不要用这条命令,这条命令会影响所有网卡
14 [root@template ~]#/etc/init.d/network restart

关闭守护进程方法:

1 关闭守护进程
2 [root@template ~]# service NetworkManager stop
3 停止 NetworkManager 守护进程:[确定]
4 
5 关闭开机自启动守护进程服务
6 [root@template ~]# chkconfig --level 35 NetworkManager off
7 [root@template ~]# chkconfig --list|grep 'Network'
8 NetworkManager  0:关闭  1:关闭  2:启用  3:关闭  4:启用  5:关闭  6:关闭    #注意安装操作系统init 设置的启动模式  (1---6)

关闭某块网卡的方法

 1 关闭eth1
 2 
 3 [root@template ~]# ifconfig eth1 down
 4 
 5 
 6 开启eth1
 7 [root@template ~]# ifconfig eth1 up
 8 
 9 
10 查看ipadd
11 [root@template ~]# ifconfig
12 eth1      Link encap:Ethernet  HWaddr 00:0C:29:45:CA:65  
13           inet addr:192.168.11.31  Bcast:192.168.11.255  Mask:255.255.255.0
14           inet6 addr: fe80::20c:29ff:fe45:ca65/64 Scope:Link
15           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
16           RX packets:1899415 errors:0 dropped:0 overruns:0 frame:0
17           TX packets:3251 errors:0 dropped:0 overruns:0 carrier:0
18           collisions:0 txqueuelen:1000 
19           RX bytes:2414954677 (2.2 GiB)  TX bytes:331129 (323.3 KiB)

配置DNS

1 [root@template ~]# cat /etc/resolv.conf                                          
2 # Generated by NetworkManager
3 search com
4 nameserver 192.168.11.254
5 nameserver 202.106.0.20
6 nameserver 8.8.8.8

检测本地arp网卡冲突地址命令

1 [root@template ~]# arping -I eth1 192.168.11.11

查看网关命令

1 [root@template ~]# route -n
2 Kernel IP routing table
3 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
4 192.168.11.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

#临时添加网关,临时生效

1 route add default gw 192.168.11.254

4. 课后作业(用时35分钟)
1) 查看当前所有正在启动的网卡的详细信息

ifconfig eth0

2) 激活系统的第一块网卡

ifup eth0


3) 为系统临时设置ip为192.168.1.10/24

ifconfig eth0 192.168.1.10/24

or

ifconfig eth0 192.168.1.10/24 netmask 255.255.255.0


4) 查看第一块网卡的详细信息

ipconfig


5) 修改网卡配置文件将系统的第一块网卡的ip地址设定为192.168.3.2,子网掩码设置为255.255.255.0,是否启动设置为yes

[root@template ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
TYPE=Ethernet
HWADDR=00:0c:29:45:ca:65
IPADDR=192.168.3.2
NETMASK=255.255.255.0
GATEWAY=192.168.3.1
DNS1=8.8.8.8
IPV6INIT=no
ONBOOT=no
USERCTL=no


6) 使用setup命令为系统第一块网卡设置ip为10.0.0.1/8

 

原文地址:https://www.cnblogs.com/nulige/p/6061498.html