Linux配置网络

配置网路


一、常见的网络接口
接口
eth0 以太网接口
wlan0 无线接口
lo 本地环回接口
bond0  bonding接口 eth0和eth1绑定之后的一个虚拟接口,为了提高网速
virbr0 Kvm桥接接口 <虚拟交换机>
br0 Kvm桥接接口 <虚拟交换机
vmnet0 VMware虚拟机网卡接口

二、查看网络信息
IP、网关
[root@localhost ~]# ip addr //查看IP、掩码、MAC...
[root@localhost ~]# ifconfig
[root@localhost ~]# ip addr show eth0 //只显示eth0的信息
[root@localhost ~]# ifconfig eth0
[root@localhost ~ ]# ip route //查看本机路由表
[root@localhost ~]# route -n //查看路由表信息


三、修改网络信息
临时:立即生效,重启失效
1. 添加临时IP
# ifconfig eth0 192.168.2.252/24 up //修改eth0的IP
# ifconfig eth0:0 192.168.2.253/24 up //给eth0增加IP
# ifconfig eth0:1 192.168.2.251/24 up //给eth0增加IP
# ip addr add dev eth0 192.168.2.252/24 //增加新的临时IP
# ip addr add 192.168.3.252/24 dev eth0
# ip addr show eth0

2.临时添加删除网关
# route add default gw 192.168.0.254
# route del default gw  192.168.0.254
或者是# ip r del

3.临时的方式添加的ip,重启网络服务之后失效
重启网络服务的方式由两种:
[root@localhost ~]# /etc/init.d/network restart
[root@localhost ~]# service network restart

NetworkManager: RHEL5/6
[root@localhost ~]# /etc/init.d/NetworkManager stop //开着它有很多高级的网络服务用不了
[root@localhost ~]# chkconfig NetworkManager off

Iptables:
[root@localhost ~]# iptables -nL //查看防火墙的规则
[root@localhost ~]# iptables -F  //清空防火墙的规则
[root@localhost ~]# service iptables save
[root@localhost ~]# /etc/init.d/iptables stop //停掉服务
[root@localhost ~]# chkconfig iptables off //设置开机不启动
[root@localhost ~]# chkconfig iptables --list //查看各个运行级别的状态
iptables 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭


SELinux: 安全增强的Linux

[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# getenforce
Disabled

# init [0-6] 切换运行级别
0:关机
1:单用户(类似于windows操作系统的安全模式)
2:多用户状态没有网络服务     
3:多用户状态有网络服务(在做开发时,通常设置成这个启动级别,直接进入到命令行的界面)
4:系统未使用保留给用户      
5:图形界面(这是linux默认的启动级别,直接进入图形界面)
6:系统重启

[root@localhost ~]# vim /etc/inittab 与系统的启动级别相关的配置文件
id:5:initdefault: //默认的运行级别

永久:配置文件
方法一:命令行的方式
1. IP/掩码/默认网关:/etc/sysconfig/network-scripts/ifcfg-eth0
静态(手工)
DEVICE=eth0 设备名
BOOTPROTO= none 启动协议: static | none | dhcp
BROADCAST=192.168.2.255 广播地址
HWADDR=00:E0:4C:41:95:DB MAC地址
TYPE=Ethernet 网卡类型
UUID=1581f655-1c2c-4578-bcc6-e0622a4578dc 网卡的UUID
NM_CONTROLLED=yes 是否接受NetworkManager管理,no表示关闭网卡的管理软件
IPADDR=192.168.2.253 IP
PREFIX=24 子网掩码。NETMASK=255.255.255.0
ONBOOT=yes 是否随服务启动而启动,不开启他相当于没有网卡
GATEWAY=192.168.2.254 默认网关

注释:静态的方式配置ip地址等网络信息,只需要由红色的几行,黑色的几行信息是可有可无的,但若想ping通非本网段的主机,就需要有网关,如果不需要ping通非本网段的主机,就不需要添加

==动态(通过DHCP服器分配)
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

[root@localhost ~]# service network restart //影响IP、DNS和网关的
[root@localhost ~]# /etc/init.d/network restart

方法二:文本图形工具 setup
[root@localhost ~] service network restart
[root@localhost ~] /etc/init.d/network restart

扩展:为一个网卡绑定多个IP

方法一:
[root@localhost ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:0
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
DEVICE=eth0:0
ONBOOT=yes
BOOTPROTO=none
IPADDR=10.10.10.1
NETMASK=255.255.255.0
[root@localhost ~] service network restart

方法二:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0 设备名
ONBOOT=yes 是否启用该接口
BOOTPROTO=none 静态: static, none 动态:dhcp, dynamic
IPADDR 1 =172.16.60.14
PREFIX 1 =24
IPADDR 2 =10.10.10.1
PREFIX 2 =24 

注:需要重启之后才能永久生效

[root@localhost ~]# service network restart
[root@Server ~]# ip a show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:56:c7:2f brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.102/24 brd 192.168.2.255 scope global eth0
    inet6 fe80::20c:29ff:fe56:c72f/64 scope link 
       valid_lft forever preferred_lft forever

双网卡配置方法:
为虚拟机添加一块网卡
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth1
[root@localhost network-scripts]# vim ifcfg-eth1
DEVICE="eth1"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR= 192.16.140.3
PREFIX=24
[root@localhost network-scripts]# /etc/init.d/network restart

四、其它一些管理工具
# ethtool eth0
Link detected: yes //查看网络物理连接,看最后一行是否是 yes
# mii-tool eth0 //查看网络物理连接,看是否出现 link ok
eth0: negotiated 100baseTx-FD flow-control, link ok

# ifdown eth0 //停用eth0
# ifup eth0 //启用eth0
# dhclient //动态获取ip地址

虚拟机中修改网卡接口的配置文件:

[root@localhost network-scripts]# vim /etc/udev/rules.d/70-persistent-net.rules 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值