Linux网卡配置

linux网卡配置

我们可以使用 ipifconfig 命令来获取网卡信息

ip 命令

ip命令:

介绍:直接查看IP地址信息

格式:ip [option]

用法:ip addr

[root@localhost ~]# 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:66:07:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.127.130/24 brd 192.168.127.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d44c:c57b:11b7:9bd5/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

列出所有的网卡以及网卡的IP地址。

ifconfig 命令

ifconfig 命令:

介绍:设置和查看网卡信息但需要使用 yum install -y net-tools 安装 net-tools 软件包后才能使用。

格式:ifconfig [device] [option]

用法:

  • ifconfig :直接使用查看网卡

  • ifconfig device ip network :设置网卡IP地址

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.127.130  netmask 255.255.255.0  broadcast 192.168.127.255
        inet6 fe80::d44c:c57b:11b7:9bd5  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:66:07:5c  txqueuelen 1000  (Ethernet)
        RX packets 129  bytes 11710 (11.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 96  bytes 10961 (10.7 KiB)
        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 68  bytes 5920 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5920 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig ens33 192.168.3.127/24
[root@localhost ~]# ifconfig ens33 192.168.3.127 255.255.255.0

dhclient 命令

dhclient 命令:

介绍:临时获取一个IP地址

格式:dhclient

用法:

  • 直接使用

  • dhclient -r :取消自动获取的IP地址

[root@localhost ~]# dhclient
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.127.130  netmask 255.255.255.0  broadcast 192.168.127.255
        inet6 fe80::d44c:c57b:11b7:9bd5  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:66:07:5c  txqueuelen 1000  (Ethernet)
        RX packets 106  bytes 10777 (10.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 100  bytes 11907 (11.6 KiB)
        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 68  bytes 5920 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5920 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

但是使用一次之后再次使用会提示:

[root@localhost ~]# dhclient
dhclient(1220) is already running - exiting. 

This version of ISC DHCP is based on the release available
on ftp.isc.org.  Features have been added and other changes
have been made to the base software release in order to make
it work better with this distribution.

Please report for this software via the CentOS Bugs Database:
    http://bugs.centos.org/

exiting.

需要杀死dhclient的进程。

[root@localhost ~]# ps aux|grep dhclient 获取进程
root       1220  0.0  0.2 107380  2304 ?        Ss   23:14   0:00 dhclient
root       1224  0.0  0.0 112720   984 pts/0    R+   23:17   0:00 grep --color=auto dhclient
[root@localhost ~]# kill -9 1220   根据进程号杀死进程

修改网卡配置文件

以上修改方式都是临时的,系统重启之后就无效了。要想下次开机有效,需要修改网卡的配置文件。

配置文件在/etc/sysconfig/network-scripts/目录下:

[root@localhost ~]# ls /etc/sysconfig/network-scripts/
ifcfg-ens33  ifdown-ippp  ifdown-routes    ifup          ifup-ipv6   ifup-ppp       ifup-tunnel
ifcfg-lo     ifdown-ipv6  ifdown-sit       ifup-aliases  ifup-isdn   ifup-routes    ifup-wireless
ifdown       ifdown-isdn  ifdown-Team      ifup-bnep     ifup-plip   ifup-sit       init.ipv6-global
ifdown-bnep  ifdown-post  ifdown-TeamPort  ifup-eth      ifup-plusb  ifup-Team      network-functions
ifdown-eth   ifdown-ppp   ifdown-tunnel    ifup-ippp     ifup-post   ifup-TeamPort  network-functions-ipv6

存在的ifcfg-ens33就是需要修改的:

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE="Ethernet"    # 以太网网络类型 还有桥接 Bridge
PROXY_METHOD="none" 
BROWSER_ONLY="no"
BOOTPROTO="static" # 重要:ip的获取类型(static|dhcp|none)
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NM_CONTROLLED=yes  # 是否修改后自启动
NAME="ens33"       # 网卡名
UUID="61d3f945-5a5a-403d-be81-a669df2f0d6b"  # 通用唯一识别码/etc/udev/rules.d/70-persistent-ipoib.rules
DEVICE="ens33"  # 网卡设备名,要和网卡文件名后缀 ifcfg-ens33相同
HWADDR=00:0C:29:D8:3F:67  网卡硬件地址
ONBOOT="yes"   # 是否开机自启动
IPADDR="192.168.127.130" # 重要:ip地址
NETMASK="255.255.255.0"  # 重要:子网掩码
GATEWAY="192.168.127.2"  # 重要:网管地址
DNS1="114.114.114.114"   # DNS 如果没有设置的话需要在编辑 /etc/resolve.conf 才能上网

配置完后要重启网卡systemctl restart network

  • systemctl start xx.service 开启服务

  • systemctl stop xx.service 关闭服务

  • systemctl restart xx.service 重启服务

使用ssh工具连接linux

我们使用SecureCRTPortable来连接:

在使用中如果出现中文乱码的话。

最后更新于

这有帮助吗?