centos 接口,centos开放端口
linux/centos如何查看网卡是100M还是1000M
可以使用ethtool命令进行查看,使用格式ethtool网络接口名。
以下为例,eth0为网卡名,使用ifconfig查看当前使用的网卡。
Speed表示网卡带宽,Duplex表示工作模式,Supported link modes表示支持的工作模式。
扩展资料:
ethtool命令的其它用法:
1、查询网络端口位置:ethtool-p ethX
这个命令多用于管理多网口的服务器主机,由于背板的网络端口数量大,有时候不知道那个物理端口对应eth0或其他设备号,我们就可以使用这个命令来查询。
用法:ethtool-p eth0
效果:设备号eth0对应的物理端口的两个指示灯会闪烁
2、修改网络端口速率:ethtool-s
这个命令多用于手工设置网络速率,一般千兆网卡支持10|100|1000三个速率,单位是Mbps。
用法:ethtool-s eth0 speed 1000 duplex full autoneg off
效果:将设备号eth0对应的物理端口设置为速率为1000Mbps,全双工工作模式,同时关闭自动协商。
CentOS的网络配置的命令详解
我们在进行对CentOS的网络配置时,一般会从IP地址(IPADDR)、子网掩码(NETMASK)、网关(Gateway)、主机名(HOSTNAME)、DNS服务器等方面入手。而在CentOS中,又有着不同的命令或配置文件可以完成这些配置操作,接下来,我们将从ifcfg系命令,iproute2系命令以及配置文件3个方面来简析网络配置的方法。
一、ifcfg系命令
ifcfg系命令包括ifconfig,route,netstat和hostname。
1、ifconfig命令
用来配置一个网络接口。它的使用格式如下:
a):ifconfig [-a] [interface]
-a:表示显示所有的网络接口信息,包括inactive状态的接口
~]#ifconfig-a
b):ifconfig IFACE(网络接口名,下同) IPADDR netmask NETMASK [up|down]
表示通过ifconfig命令直接修改接口的ip地址和netmask,并可启停该接口。
该命令会将配置参数立即送往内核中的TCP/IP协议栈,所以会立即生效,但是重启服务后会无效。
~]#ifconfigeth1172.16.7.14netmask255.255.0.0~]#ifconfigeth1172.16.7.14/16down
2、route命令
显示和配置ip路由表。它的使用格式如下:
a):route [-n]
表示查看全部的路由表,并以数字显示ip地址
~]#route-n
b):route add [-net|-host] target [netmask Nm] [gw Gw][[dev] If]
添加相应接口的路由
~]#routeadd-host172.16.7.14gw172.16.0.1deveth1#主机路由~]#routeadd-net172.16.7.0/24gw172.16.0.1deveth1#网段路由~]#routeadddefaultgw172.16.0.1deveth1#默认路由
c):route del [-net|-host] target [netmask Nm][[dev] If]
删除路由
~]#routedel-host172.16.7.14deveth1~]#routedel-net172.16.7.0/24deveth1~]#routedeldefaultdeveth1
3、netstat命令
显示网络连接,路由表,接口状态等。它的使用格式如下:
netstat [-n] [-r] [-p] [-e] [-a] [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [-i] [-I]
-n:以数字格式显示ip和端口号,不做地址转换;
-r:显示内核路由表
-p:显示相关的进程和PID
-e:显示扩展格式
-a:显示所有的连接状态
-t:显示tcp协议的相关连接的状态
-u:显示udp协议的相关连接的状态
-w:显示raw socket相关连接的状态
-l:显示处于监听状态的连接
-i:显示所有接口状态
-I:显示特定的接口状态
以上个选项可组合使用,常用组合有-tan,-uan,-tln,-uln,-tunlp等
~]#netstat-tan~]#netstat-Ieth1
4、hostname命令
显示配置主机名。它的使用格式如下:
hostname:显示当前主机名
hostname HOSTNAME:修改当前主机名为HOSTNAME,仅当前有效,重启无效。
~]#hostname~]#hostnameTEST
5、hostnamectl命令(CentOS7新增)
显示配置主机名。它的使用格式如下:
hostnamectl [status]:显示当前主机名信息
hostnamectl set-hostname:设定主机名,永久有效
~]#hostnamectlset-hostnameCentOS7
二、iproute2系命令
iproute2系命令有ip link,ip addr,ip route,ip netns和ss等。
1、ip link命令
配置网络接口属性。它的使用格式如下:
a):ip link set [dev] IFACE [up|down] [multicast on|off] [name IFACE] [mtu NUMBER]
up and down:启动或禁止选定的网络接口
multicast on or multicast off:启动或禁止组播功能
name NAME:重命名接口(命名前需先down掉接口)
mtu NUMBER:设置接口的mtu大小,默认为1500
~]#iplinkseteth1nameeth2up~]#iplinkseteth2multicastoffmtu1000
b):ip link [show|list]
显示全部网络接口的属性。
~]#iplinkshow
2、ip addr命令
配置网络接口的ip地址。它的使用格式如下:
a):ip addr add IPADDR dev IFACE[label NAME][broadcast ADDRESS][scope SCOPE_VALUE]
增加一个ip地址。
[label NAME]:为额外添加的地址指明接口别名
[broadcast ADDRESS]:会根据ip和netmask自动计算,不用填
[scope SCOPE_VALUE]:gloal全局可用;link接口可用;host:本机可用
~]#ipaddradd172.16.7.14/24deveth1labeleth1:0
b):ip addr delete IPADDR dev IFACE
删除一个ip地址。
~]#ipaddrdel172.16.7.14/24deveth1:0
c):ip addr show [dev IFACE|label IFACE:#]
仅显示指定的接口地址。
~]#ipaddrshowlabeleth1:0~]#ipaddrshoweth1
d):ip addr flush [dev]{IFACE|label IFACE:#}
清空指定的所有地址或别名的地址。
~]#ipaddrflushlabeleth1:0~]#ipaddrflusheth1
3、ip route命令
配置接口的路由表信息。它的使用格式如下:
a):ip route addTYPE PREFIX(目标网络)via GateWay [dev IFACE] [src SOURCE_IP]
添加路由表。
~]#iprouteadd172.16.7.14via172.16.0.1deveth1~]#iprouteadd172.16.7.0/24via172.16.0.1deveth1~]#iprouteadddefaultvia172.16.0.1deveth1
b):ip route del TYPE PREFIX
删除路由表。
~]#iproutedel172.16.7.14~]#iproutedel172.16.7.0/24
c):ip route show
显示路由表。
~]#iprouteshow
d):ip route flush dev IFACE
清空指定接口的所有路由表。
~]#iprouteflushdeveth1
4、ss命令
和netstat命令一样用来查看网络状态,其选项参数和netstat命令基本相同,但是其查询速度比netstat命令要快。它的使用格式如下:
ss [options][filter]
filter:过滤器 FILTER:= [ state TCP-STATE ] [ EXPRESSION ]
选项:
-t:TCP协议的相关连接
-u:UDPF相关的连接
-w:raw socket相关的连接
-l:处于监听状态的连接
-a:所有状态
-n:以数字格式显示IP和Port
-e:扩展格式
-p:显示相关的进程及PID
-m:内存用量
-o:计时器信息
EXPRESSION:
dport:目标端口
sport:源端口
‘( dport=:22 or sport=:22)’
~]#ss-tan#查看当前网络所以tcp的连接状态
三、网络配置文件
在CentOS系统上我们可以通过对修改不同的网络配置文件来使得配置网络接口的各种属性,不过要使其永久有效,还需使内核重读修改后的配置文件。
1、IP/NETMASK/GW/DNS等属性的配置文件:
其路径为:/etc/sysconfig/network-scripts/ifcfg-IFACE
其格式为:
DEVICE:此配置文件对应的设备的名称;
ONBOOT:在系统引导过程中,是否激活此接口;
NETBOOT:是否支持网络引导;
UUID:设备的唯一标识;
IPv6INT:是否初始化IPv6;
BOOTPROTO:激活此接口时使用什么协议来配置属性,常用dhcp、bootp、static、none
TYPE:接口类型,常见的有ethernet、bridge;
DNS1:第一DNS服务器指向;
DNS2:备用DNS服务器指向;
DOMAIN:DNS搜索域;
GATEWAY:默认网关;
IPADDR:本机ip地址;
NETMASK:子网掩码;CentOS7支持使用PREFIX以长度指明子网掩码;
USERCTL:是否允许普通用户控制此设备;
PEERDNS:如果BOOTPROTO的值为“dhcp”,是否运行dhcp server分配的dns服务器指向覆盖本地手动指定的DNS服务器指向;默认为允许
HWADDR:设备的MAC地址;
NM_CONTROLLED:是否使用NetworkManager服务来控制接口;
network-scripts]#catifcfg-eth1 DEVICE="eth1" ONBOOT=yes NETBOOT=yes IPADDR=172.16.7.14 DNS=172.16.0.1 GATEWAY=172.16.0.1 NETMASK=255.255.0.0 IPV6INIT=no BOOTPROTO=none TYPE=Ethernet
2、路由的相关配置文件:
其路径为:/etc/sysconfig/network-scripts/route-IFACE
其格式为:(不可混用)
a):每行一个路由条目:
TARGET via GW
b):每三行一个路由条目:
ADDRESS#=TARGET
NETMASK#=MASK
GATEWAY#=NEXTHOP
network-scripts]#catroute-eth1 172.16.7.0/24via172.16.0.1
3、主机名的相关配置文件:
其路径为:/etc/sysconfig/network
其格式为:
HOSTNAME=
sysconfig]#catnetwork#Createdbyanaconda HOSTNAME=centos7.1
4、DNS服务器指向配置文件:
其路径为:/etc/resolv.conf
其格式为:
nameserver DNS_SERVER_IP
etc]#catresolv.conf
#GeneratedbyNetworkManager
searchmagelinux.com
nameserver172.16.0.1
四、结语
在学习Linux的路上,网络是非常重要的一环,其命令也在不断改进着,很多命令有着相同的作用,可以选择先记忆比较简单和高效的命令和参数。除上述命令之外,在CentOS7中也有着如nmtui这样直接显示图形界面的还用命令。
CentOS7网络设置修复
服务器有两个网卡enp7s0和enp8s0
服务器需要通过enp8s0和互联网联通,互联网为笔记本转接
通过centos7界面修改ip,失败;
通过centos7的网络配置文件/etc/sysconfig/network-scripts/ifcfg-enp8s0修改ip设置
设置如下:
1.启动 service network start报错,通过journalctl-xe查看如下
-- Defined-By: systemd
-- Support:
--
-- Unit fprintd.service has begun starting up.
12月 29 19:31:12 crrchost dbus-daemon[851]: dbus[851]: [system] Successfully activated service'net.reactivated.Fprint'
12月 29 19:31:12 crrchost dbus[851]: [system] Successfully activated service'net.reactivated.Fprint'
12月 29 19:31:12 crrchost systemd[1]: Started Fingerprint Authentication Daemon.
-- Subject: Unit fprintd.service has finished start-up
-- Defined-By: systemd
-- Support:
--
-- Unit fprintd.service has finished starting up.
--
-- The start-up result is done.
12月 29 19:31:12 crrchost fprintd[6685]: Launching FprintObject
12月 29 19:31:12 crrchost fprintd[6685]:** Message: D-Bus service launched with name: net.reactivated.Fprint
12月 29 19:31:12 crrchost fprintd[6685]:** Message: entering main loop
12月 29 19:31:18 crrchost polkitd[835]: Operator of unix-session:1 successfully authenticated as unix-user:windfarm to gain TEMPORARY authorization for action org.freedesktop.systemd1.manage-units for system-bus
12月 29 19:31:18 crrchost systemd[1]: Starting LSB: Bring up/down networking...
-- Subject: Unit network.service has begun start-up
-- Defined-By: systemd
-- Support:
--
-- Unit network.service has begun starting up.
12月 29 19:31:18 crrchost network[6693]:正在打开环回接口: [ 确定 ]
12月 29 19:31:18 crrchost network[6693]:正在打开接口 enp7s0:
12月 29 19:31:24 crrchost network[6693]:正在确定 enp7s0的 IP信息...失败:不存在链接。检查电缆?
12月 29 19:31:24 crrchost network[6693]: [失败]
12月 29 19:31:24 crrchost network[6693]:正在打开接口 enp8s0: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: [ 确定 ]
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost network[6693]: RTNETLINK answers: File exists
12月 29 19:31:25 crrchost systemd[1]: network.service: control process exited, code=exited status=1
12月 29 19:31:25 crrchost systemd[1]: Failed to start LSB: Bring up/down networking.
-- Subject: Unit network.service has failed
-- Defined-By: systemd
-- Support:
--
-- Unit network.service has failed.
--
-- The result is failed.
12月 29 19:31:25 crrchost systemd[1]: Unit network.service entered failed state.
12月 29 19:31:25 crrchost systemd[1]: network.service failed.
12月 29 19:31:25 crrchost polkitd[835]: Unregistered Authentication Agent for unix-process:6669:319312(system bus name:1.263, object path/org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8)(d
3.systemctl status network.service查看如下:
● network.service- LSB: Bring up/down networking
Loaded: loaded(/etc/rc.d/init.d/network; bad; vendor preset: disabled)
Active: failed(Result: exit-code) since六 2018-12-29 18:38:17 CST; 1min 24s ago
Docs: man:systemd-sysv-generator(8)
Process: 909 ExecStart=/etc/rc.d/init.d/network start(code=exited, status=1/FAILURE)
12月 29 18:38:06 crrchost systemd[1]: Starting LSB: Bring up/down networking...
12月 29 18:38:06 crrchost network[909]:正在打开环回接口: [ 确定 ]
12月 29 18:38:06 crrchost network[909]:正在打开接口 enp7s0:
12月 29 18:38:12 crrchost network[909]:正在确定 enp7s0的 IP信息...失败:…?
12月 29 18:38:12 crrchost network[909]: [失败]
12月 29 18:38:17 crrchost network[909]:正在打开接口 enp8s0: [ 确定 ]
12月 29 18:38:17 crrchost systemd[1]: network.service: control process exi...=1
12月 29 18:38:17 crrchost systemd[1]: Failed to start LSB: Bring up/down n...g.
12月 29 18:38:17 crrchost systemd[1]: Unit network.service entered failed...e.
12月 29 18:38:17 crrchost systemd[1]: network.service failed.
Hint: Some lines were ellipsized, use-l to show in full.
1.参考网络方法停用NetworkManager,依旧报错
systemctl stop NetworkManager
systemctl disable NetworkManager
2.比对两个网卡的MAC地址和配置文件的MAC地址
a.查看mac地址配置,ipconfig
b.查看配置文件中的macd地址是否匹配,ifcfg-enp8s0和ifcfg-enp7s0,
按照网络方法增加一行HWADDR=ipconfig查询到的对应网卡MAC地址;
c.重启network服务依旧报错;
3.通过GUI界面查看网络,提示“系统的网络服务与此版本的网络管理器不兼容”
这与之前停掉NetworkManager有关,此处不表;
4.因为ifcfg-enp7s0实际上没有插网线,修正其配置文件如下:
ONBOOT=no
重新启动服务后网络正常;
5.检查网络
1.network和NetworkManager不同,通过界面和通过配置文件,可能会导致配置混乱,且NetworkManager有些程序不支持,需要停用;
2.BOOTPROTO=none虽然可以表示静态ip,但是如果要启用,必须设置其为
BOOTPROTO=static或BOOTPROTO=dhcp
3.一个网卡没哟被启用,也就是没有设置动态ip或静态ip,是无法被初始化的,不能设置ONBOOT=yes
In Centos7.0 disabling NetworkManager will leave a dhcp client running configured for NetworkManager. This causes the error message RTNETLINK answers: File exists when the network service is started.
The stale dhclient process has the additional"benefit" that when the lease expires your dhclientwill choke, since it cannot reach NetWorkManager, thus removing your IP address.
If you grep for it, you will see that it points to a NetWorkManager configuration file.
[root@host~]# ps-ef| grep dhc
root 1865 792 0 Apr28? 00:00:00/sbin/dhclient-d-sf\
/usr/libexec/nm-dhcp-helper-pf/var/run/dhclient-eno1.pid-lf\
/var/lib/NetworkManager/dhclient-c96e56d3-a4c9-4a87-85ca-93dc0ca268f2-eno1.lease\
-cf/var/lib/NetworkManager/dhclient-eno1.conf eno1
So what you can do is kill the dhclient and only then start your network service.