linux如何激活网卡

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux如何激活网卡相关的知识,希望对你有一定的参考价值。

参考技术A 我用的是centos5.3,上网方法应该是和redhat一样的
我的方法是:
#cd
/sbin
#./adsl-setup
设置网络参数
设置好之后,就可以用ifup
ppp0开启网络,ifdown
ppp0关闭网络
在非root下,最好是修改.bashrc
在最后几行加入
export
PATH="$PATH:/bin:/sbin:/usr/bin:/usr/sbin"
个人感觉这样可以更方便的使用各种命令
设置好之后
在终端只要用以下操作就很方便的开启、关闭网络
$su
密码
#ifup
ppp0(开启网络)
#ifdown
ppp0(关闭网络)
参考技术B 激活步骤写如下
1、输入:netconfig
ip地址
service
network
start
进入IP配置画面,输入IP、子网掩码、网关后,确定。
2、回到命令行界面,输入:
ifdown
eth0
ifup
eth0
网卡生效,网络配置成功。
让Linux开机自动激活网卡
方法一

vi
/etc/sysconfig/network-scripts/ifcfg-eth0
修改
ONBOOT=yes
方法二

在/etc/rc.d/rc.local
里面加入
ifup
eth0

ifconfig
eth0
up
参考技术C 激活步骤写如下
1、输入:netconfig
ip地址
service
network
start
进入ip配置画面,输入ip、子网掩码、网关后,确定。
2、回到命令行界面,输入:
ifdown
eth0
ifup
eth0
网卡生效,网络配置成功。
让linux开机自动激活网卡
方法一

vi
/etc/sysconfig/network-scripts/ifcfg-eth0
修改
onboot=yes
方法二

在/etc/rc.d/rc.local
里面加入
ifup
eth0

ifconfig
eth0
up
可以多关注《linux就该这么学》这本书,我都是从这里面学到的

Linux基础之网络配置

一、查看网络配置

1.查看网络接口状态

[root@node1 ~]# ifconfig ens224
ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.11  netmask 255.255.255.0  broadcast 192.168.8.255
        inet6 fe80::1344:2c57:66a9:99a6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:bc  txqueuelen 1000  (Ethernet)
        RX packets 139628  bytes 15557286 (14.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 156135  bytes 20632958 (19.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


2.查看网关路由信息

[root@node1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.200.2   0.0.0.0         UG    100    0        0 ens160
0.0.0.0         192.168.8.1     0.0.0.0         UG    101    0        0 ens224
0.0.0.0         192.168.9.1     0.0.0.0         UG    102    0        0 ens256
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.8.0     0.0.0.0         255.255.255.0   U     101    0        0 ens224
192.168.9.0     0.0.0.0         255.255.255.0   U     102    0        0 ens256
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.200.0   0.0.0.0         255.255.255.0   U     100    0        0 ens160

3.查看dns信息

[root@node1 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 192.168.200.150
nameserver 192.168.8.1
nameserver 192.168.9.1
[root@node1 ~]# 

二、ifconfig网络管理工具使用

1.查看已激活网卡信息

[root@node1 ~]# ifconfig 
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:3bff:fe27:6e  prefixlen 64  scopeid 0x20<link>
        ether 02:42:3b:27:00:6e  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28  bytes 3955 (3.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.200.135  netmask 255.255.255.0  broadcast 192.168.200.255
        inet6 fe80::79d4:c66c:4dfd:8050  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:b2  txqueuelen 1000  (Ethernet)
        RX packets 63413  bytes 4855353 (4.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 51247  bytes 3972067 (3.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.11  netmask 255.255.255.0  broadcast 192.168.8.255
        inet6 fe80::1344:2c57:66a9:99a6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:bc  txqueuelen 1000  (Ethernet)
        RX packets 141667  bytes 15779234 (15.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 158353  bytes 20924018 (19.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens256: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.9.11  netmask 255.255.255.0  broadcast 192.168.9.255
        inet6 fe80::98f4:d12d:141:ed83  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:c6  txqueuelen 1000  (Ethernet)
        RX packets 20102  bytes 4249700 (4.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14190  bytes 1560464 (1.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 3834  bytes 280418 (273.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3834  bytes 280418 (273.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethe519a37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::98e4:e9ff:feda:7799  prefixlen 64  scopeid 0x20<link>
        ether 9a:e4:e9:da:77:99  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 6321 (6.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethef87674: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::3c42:a8ff:fef3:e316  prefixlen 64  scopeid 0x20<link>
        ether 3e:42:a8:f3:e3:16  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 6321 (6.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:00:3f:0a  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.查看所有网卡(包括未激活)网卡信息

[root@node1 ~]# ifconfig -a

3.设置网卡激活及禁用

[root@node1 ~]# ifconfig ens224 up
[root@node1 ~]# ifconfig ens224 down

4.配置网卡IP地址

[root@node1 ~]# ifconfig ens224 172.25.25.21/24

三、nmcli网络管理工具使用

1.新加网卡配置文件

[root@node1 ~]# nmcli connection add ifname ens224 type ethernet con-name ens224-con224 ipv4.addresses  192.168.8.11/24 ipv4.gateway 192.168.8.1 ipv4.dns 192.168.8.1 ipv4.method manual
[root@node1 ~]# nmcli connection reload 
[root@node1 ~]# nmcli connection up ens224-con224 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@node1 ~]# 

2.修改网卡配置文件

[root@node1 ~]# nmcli connection modify ens224-con224 ipv4.addresses 192.168.8.11/24 ipv4.gateway 192.168.8.1 ipv4.dns 192.168.8.1 ipv4.method manual 
[root@node1 ~]# nmcli connection reload 
[root@node1 ~]# nmcli connection up ens224-con224 

3.新加网卡IP地址

nmcli connection modify ens224-con224 +ipv4.addresses 192.168.8.111/24
[root@node1 ~]# nmcli con up ens224-con224 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/10)
[root@node1 network-scripts]# ip addr show ens224
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.11/24 brd 192.168.8.255 scope global noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet 192.168.8.111/24 brd 192.168.8.255 scope global secondary noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet6 fe80::1344:2c57:66a9:99a6/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@node1 network-scripts]#

4.查看网卡配置文件状态

[root@node1 network-scripts]# nmcli connection show 
NAME           UUID                                  TYPE      DEVICE  
docker0        ff8dae9c-631f-4e9d-961b-d5e09c85dbaa  bridge    docker0 
ens160         142fe7fb-a214-497f-a638-accac6a8407d  ethernet  ens160  
ens224-con224  1244a360-7311-472f-b6bd-25186c342c67  ethernet  ens224  
ens224-con256  a87770e8-3b7a-4cb4-9fbe-45bc08388cb8  ethernet  ens256  
virbr0         d7a40851-e7dc-477d-b479-e26eb4895d2b  bridge    virbr0  
[root@node1 network-scripts]# 

5.查看网卡设备状态

[root@node1 network-scripts]# nmcli device  status 
DEVICE       TYPE      STATE      CONNECTION    
ens160       ethernet  connected  ens160        
ens224       ethernet  connected  ens224-con224 
docker0      bridge    connected  docker0       
ens256       ethernet  connected  ens224-con256 
virbr0       bridge    connected  virbr0        
vethe519a37  ethernet  unmanaged  --            
vethef87674  ethernet  unmanaged  --            
lo           loopback  unmanaged  --            
virbr0-nic   tun       unmanaged  --            
[root@node1 network-scripts]# 

6.连接或断开网卡设备

[root@node1 network-scripts]# nmcli device disconnect/connect ens224

四、IP网络管理工具使用

1.查看网卡设备信息

[root@node1 network-scripts]# ip link  show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:4e:fd:b2 brd ff:ff:ff:ff:ff:ff
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:4e:fd:bc brd ff:ff:ff:ff:ff:ff
4: ens256: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:4e:fd:c6 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default 
    link/ether 02:42:3b:27:00:6e brd ff:ff:ff:ff:ff:ff
7: vethef87674@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default 
    link/ether 3e:42:a8:f3:e3:16 brd ff:ff:ff:ff:ff:ff link-netnsid 1
9: vethe519a37@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default 
    link/ether 9a:e4:e9:da:77:99 brd ff:ff:ff:ff:ff:ff link-netnsid 0
10: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:00:3f:0a brd ff:ff:ff:ff:ff:ff
11: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:00:3f:0a brd ff:ff:ff:ff:ff:ff

2.激活或禁用网卡

[root@node1 network-scripts]# ip link set ens224 up/down

3.查看网络地址

[root@node1 network-scripts]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:b2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.135/24 brd 192.168.200.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::79d4:c66c:4dfd:8050/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.11/24 brd 192.168.8.255 scope global noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet 192.168.8.111/24 brd 192.168.8.255 scope global secondary noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet6 fe80::1344:2c57:66a9:99a6/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: ens256: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:c6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.9.11/24 brd 192.168.9.255 scope global noprefixroute ens256
       valid_lft forever preferred_lft forever
    inet6 fe80::98f4:d12d:141:ed83/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:3b:27:00:6e brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:3bff:fe27:6e/64 scope link 
       valid_lft forever preferred_lft forever
7: vethef87674@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 3e:42:a8:f3:e3:16 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::3c42:a8ff:fef3:e316/64 scope link 
       valid_lft forever preferred_lft forever
9: vethe519a37@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 9a:e4:e9:da:77:99 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::98e4:e9ff:feda:7799/64 scope link 
       valid_lft forever preferred_lft forever
10: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:00:3f:0a brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
11: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:00:3f:0a brd ff:ff:ff:ff:ff:ff
[root@node1 network-scripts]# 

4.网卡新增IP和删除IP地址

[root@node1 network-scripts]# 
[root@node1 network-scripts]# ip addr add 192.168.8.222/24 dev ens224
[root@node1 network-scripts]# ip addr del 192.168.8.222/24 dev ens224

5.管理路由表

①查看路由表

[root@node1 ~]# ip route show
default via 192.168.200.2 dev ens160 proto static metric 100 
default via 192.168.9.1 dev ens256 proto static metric 102 
default via 192.168.8.1 dev ens224 proto static metric 103 
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.11 metric 103 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.111 metric 103 
192.168.9.0/24 dev ens256 proto kernel scope link src 192.168.9.11 metric 102 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
192.168.200.0/24 dev ens160 proto kernel scope link src 192.168.200.135 metric 100

②.新增静态路由

[root@node1 ~]# ip route add 192.168.120.0/24 via 192.168.8.1 
[root@node1 ~]# ip route 
default via 192.168.200.2 dev ens160 proto static metric 100 
default via 192.168.9.1 dev ens256 proto static metric 102 
default via 192.168.8.1 dev ens224 proto static metric 103 
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.11 metric 103 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.111 metric 103 
192.168.9.0/24 dev ens256 proto kernel scope link src 192.168.9.11 metric 102 
192.168.120.0/24 via 192.168.8.1 dev ens224 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
192.168.200.0/24 dev ens160 proto kernel scope link src 192.168.200.135 metric 100 
[root@node1 ~]# 

③.删除路由

[root@node1 ~]# ip route del 192.168.120.0/24 via 192.168.8.1

五、网络其他命令

1.查看网卡配置文件

[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls
ifcfg-ens160  ifcfg-ens224-con224  ifcfg-ens224-con256
[root@node1 network-scripts]# cat ifcfg-ens224-con224 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.8.11
PREFIX=24
GATEWAY=192.168.8.1
DNS1=192.168.8.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens224-con224
UUID=1244a360-7311-472f-b6bd-25186c342c67
DEVICE=ens224
ONBOOT=yes
IPADDR1=192.168.8.111
PREFIX1=24
[root@node1 network-scripts]# 

2.查看及设置主机名

[root@node1 network-scripts]# hostname
node1

[root@node1 network-scripts]# hostnamectl set-hostname test

以上是关于linux如何激活网卡的主要内容,如果未能解决你的问题,请参考以下文章

linux无法激活网卡?

linux 网卡开机自动禁用

Linux双网卡内外网服务器IP配置?

linux eth1网卡激活不了解决方法

Linux基础之网络配置

linux系统网络激活不了