10.11 Linux网络相关
ifconfig
ifconfig查看网卡状况以及IP等 //yum install -y net-tools安装ifconfig
ip addr用于查看所有的网卡
ifdown ens33 //关闭网卡ens33
ifup ens33 //打开网卡ens33,用这种方式可以单独重启指定的网卡,而不是重启整个网络服务
ifdown ens33 &&ifup ens33 //这样重启的效果更好
给一个网卡设定多个IP
[root@centos-01 ~]# cd /etc/sysconfig/network-scripts
[root@centos-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33:1 //复制一个配置文件
[root@centos-01 network-scripts]# vim ifcfg-ens33:1 //编辑配置文件,device名和name名后加:1,并更改IP地址
[root@centos-01 network-scripts]# ifdown ens33&&ifup ens33
[root@centos-01 network-scripts]# ifconfig //重启后可以看到多了一个网卡ens33:1
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.27.128 netmask 255.255.255.0 broadcast 192.168.27.255
inet6 fe80::57c0:b138:acc2:fc0f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1c:97:11 txqueuelen 1000 (Ethernet)
RX packets 1132 bytes 103087 (100.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 596 bytes 93024 (90.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.27.150 netmask 255.255.255.0 broadcast 192.168.27.255
ether 00:0c:29:1c:97:11 txqueuelen 1000 (Ethernet)
查看网卡连接状态
[root@centos-01 network-scripts]# mii-tool ens33 //link ok表示连接正常
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@centos-01 network-scripts]# ethtool ens33 //末尾link detected:yes 表示连接正常
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
MDI-X: off (auto)
Supports Wake-on: d
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
更改主机名
[root@centos-01 network-scripts]# hostname //查看主机名
centos-01
[root@centos-01 network-scripts]# hostname centos-011 //更改主机名,但是这仅仅只是保存在内存中,重启后失效
[root@centos-01 network-scripts]# hostname
centos-011
vim /etc/hostname //更改这个文件才是永久的办法
[root@centos-01 network-scripts]# hostnamectl set-hostname centos-011 //这个命令可以直接更改/ect/hostname文件
[root@centos-01 network-scripts]# hostname
centos-011
[root@centos-01 network-scripts]# cat /etc/hostname //显示文件已更改
centos-011
改DNS
在网卡配置文件中改DNS是永久更改, /etc/sysconfig/network-scripts/ifcfg-ens33
但是在/etc/resolv.conf中也可以更改DNS,这里的更改是临时的。一般可以在此设置多个DNS,以做备用。
[root@centos-01 network-scripts]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
/etc/hosts文件可以临时更改域名指向的IP,有时候会很有用。例如:
[root@centos-01 network-scripts]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.3.5 www.qq.com //添加qq的域名
[root@centos-01 network-scripts]# ping www.qq.com //ping qq.com发现指向了刚刚我们指定的ip
PING www.qq.com (192.168.3.5) 56(84) bytes of data.
/hosts文件中,一行只能有一个ip, 一个ip后面可以跟多个域名,如果多行中出现同样的域名(ip不同),会以写在前面的为准