hellopasswd
Linux网络相关
- ifconfig #查看网卡ip(yum install net-tools)
- ifup ens33/ifdown ens33
- 设定虚拟网卡ens33:1
- mii-tool ens33 #查看网卡是否连接
- ethtool ens33 #查看网卡是否连接
- 更改主机名hostnamectl set-hostname Linux1
- DNS配置文件/etc/resolv.conf
- /etc/hosts文件
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
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: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:0a:e7:fc brd ff:ff:ff:ff:ff:ff
inet 192.168.9.134/24 brd 192.168.9.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe0a:e7fc/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# yum reinstall -y net-tools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* epel: mirrors.tongji.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.22.20131004git.el7 will be reinstalled
--> Finished Dependency Resolution
Dependencies Resolved
===================================================================================================
Package Arch Version Repository Size
===================================================================================================
Reinstalling:
net-tools x86_64 2.0-0.22.20131004git.el7 base 305 k
Transaction Summary
===================================================================================================
Reinstall 1 Package
Total download size: 305 k
Installed size: 917 k
Downloading packages:
net-tools-2.0-0.22.20131004git.el7.x86_64.rpm | 305 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.22.20131004git.el7.x86_64 1/1
Installed:
net-tools.x86_64 0:2.0-0.22.20131004git.el7
Complete!
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.9.134 netmask 255.255.255.0 broadcast 192.168.9.255
inet6 fe80::20c:29ff:fe0a:e7fc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0a:e7:fc txqueuelen 1000 (Ethernet)
RX packets 99609 bytes 41662012 (39.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 88315 bytes 21184319 (20.2 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 0 (Local Loopback)
RX packets 26 bytes 1884 (1.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 1884 (1.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# ifconfig -a
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.9.134 netmask 255.255.255.0 broadcast 192.168.9.255
inet6 fe80::20c:29ff:fe0a:e7fc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0a:e7:fc txqueuelen 1000 (Ethernet)
RX packets 99641 bytes 41664576 (39.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 88331 bytes 21186991 (20.2 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 0 (Local Loopback)
RX packets 26 bytes 1884 (1.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 1884 (1.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在终端中关闭网卡
[root@localhost ~]# ifdown eno16777736
在终端关闭网卡时,会发现终端连接异常,并且ifconfig可发现ip异常
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:0a:e7:fc txqueuelen 1000 (Ethernet)
RX packets 99609 bytes 41662012 (39.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 88315 bytes 21184319 (20.2 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 0 (Local Loopback)
RX packets 26 bytes 1884 (1.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 1884 (1.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
在主机中启动网卡
[root@localhost ~]# ifup eno16777736
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/17)
[root@localhost ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.9.134 netmask 255.255.255.0 broadcast 192.168.9.255
inet6 fe80::20c:29ff:fe0a:e7fc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0a:e7:fc txqueuelen 1000 (Ethernet)
RX packets 100990 bytes 42069350 (40.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 88716 bytes 21235310 (20.2 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 0 (Local Loopback)
RX packets 26 bytes 1884 (1.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 1884 (1.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
当一台设备或者服务器有多个网卡时,在调试中可以使用ifup 网卡/ifdown 网卡
[root@localhost ~]# ifdown eno16777736 && ifup eno16777736
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/21)
设置虚拟网卡
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-eno16777736 ifdown-isdn ifdown-tunnel ifup-isdn ifup-Team
ifcfg-lo ifdown-post ifup ifup-plip ifup-TeamPort
ifdown ifdown-ppp ifup-aliases ifup-plusb ifup-tunnel
ifdown-bnep ifdown-routes ifup-bnep ifup-post ifup-wireless
ifdown-eth ifdown-sit ifup-eth ifup-ppp init.ipv6-global
ifdown-ippp ifdown-Team ifup-ippp ifup-routes network-functions
ifdown-ipv6 ifdown-TeamPort ifup-ipv6 ifup-sit network-functions-ipv6
[root@localhost network-scripts]# cp ifcfg-eno16777736 ifcfg-eno16777736\:0
[root@localhost network-scripts]# vi !$
vi ifcfg-eno16777736\:0
HWADDR=00:0C:29:0A:E7:FC
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777736:0
UUID=18ff95f5-b055-4ad1-baad-74bd36e18654
DEVICE=eno16777736:0
ONBOOT=yes
IPADDR=192.168.9.139
NETMASK=255.255.255.0
NETMASK=255.255.255.0
GATEWAY=192.168.9.2
[root@localhost network-scripts]# ifdown eno16777736 && ifup eno16777736
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/22)
[root@localhost network-scripts]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.9.134 netmask 255.255.255.0 broadcast 192.168.9.255
inet6 fe80::20c:29ff:fe0a:e7fc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0a:e7:fc txqueuelen 1000 (Ethernet)
RX packets 106590 bytes 42541671 (40.5 MiB)
RX errors 0 dropped 9 overruns 0 frame 0
TX packets 91427 bytes 21585449 (20.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.9.139 netmask 255.255.255.0 broadcast 192.168.9.255
ether 00:0c:29:0a:e7:fc txqueuelen 1000 (Ethernet)
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 0 (Local Loopback)
RX packets 26 bytes 1884 (1.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 1884 (1.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
网卡名字不可以更改,若配置信息里没有DEVICE,则添加一个
宿主机ping虚拟机ip
C:\Users\Administrator>ping 192.168.9.134
正在 Ping 192.168.9.134 具有 32 字节的数据:
来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.9.134 的回复: 字节=32 时间<1ms TTL=64
192.168.9.134 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
C:\Users\Administrator>ping 192.168.9.139
正在 Ping 192.168.9.139 具有 32 字节的数据:
来自 192.168.9.139 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.9.139 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.9.139 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.9.139 的回复: 字节=32 时间<1ms TTL=64
192.168.9.139 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
查看网卡是否连接
[root@localhost ~]# mii-tool eno16777736
eno16777736: negotiated 1000baseT-FD flow-control, link ok
[root@localhost ~]# mii-tool eno16777736:0
eno16777736:0: negotiated 1000baseT-FD flow-control, link ok
有些版本不支持,可以使用下面的命令查看
[root@localhost ~]# ethtool eno16777736
Settings for eno16777736:
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@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# hostnamectl set-hostname linux001
[root@localhost ~]# hostname
linux001
[root@localhost ~]# bash
[root@linux001 ~]#
[root@linux001 ~]# exit
[root@localhost ~]# cat /etc/hostname
linux001
[root@localhost ~]# logout
Last login: Tue Nov 28 06:41:02 2017 from 192.168.9.1
[root@linux001 ~]#
DNS配置文件
[root@localhost ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
nameserver 114.114.114.114
在网卡的配置文件中定义的
/etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
配置lnmp访问自定义域名
[root@localhost ~]# ping www.qq.com
PING www.qq.com (120.198.201.156) 56(84) bytes of data.
64 bytes from 120.198.201.156: icmp_seq=1 ttl=128 time=13.1 ms
64 bytes from 120.198.201.156: icmp_seq=2 ttl=128 time=20.3 ms
64 bytes from 120.198.201.156: icmp_seq=3 ttl=128 time=25.7 ms
^C
--- www.qq.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 13.105/19.734/25.731/5.175 ms
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.9.134 www.qq.com
[root@localhost ~]# ping www.qq.com
PING www.qq.com (192.168.9.134) 56(84) bytes of data.
64 bytes from www.qq.com (192.168.9.134): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from www.qq.com (192.168.9.134): icmp_seq=2 ttl=64 time=0.040 ms
64 bytes from www.qq.com (192.168.9.134): icmp_seq=3 ttl=64 time=0.044 ms
^C
--- www.qq.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.018/0.034/0.044/0.011 ms
只对本机生效,一个ip可以对应多个域名,hosts解析是先解析的优先级高,以先出现的记录为主,从上到下依次执行的,一旦匹配到一个域名就直接解析了,即使后面再出现这域名也不生效
修改于 171129