linux网络管理

linux网络配置

1.ifconfig命令

ifconfig ##查看或设定网络接口
ifconfig device ip/24 ##在对应的网卡上临时设定ip
ifconfig device down ##关闭网卡设备
ifconfig device up ##开启网卡设备
ifconfig eth0 arp ##开启网卡eth0 的arp协议;
ifconfig eth0 -arp ##关闭网卡eth0 的arp协议;

注意:device是你的网卡名

[root@server ~]# ifconfig 
eth0: flags=4163  mtu 1500
        inet 172.25.74.124  netmask 255.255.255.0  broadcast 172.25.74.255
        inet6 fe80::5054:ff:fe00:4a0b  prefixlen 64  scopeid 0x20
        ether 52:54:00:00:4a:0b  txqueuelen 1000  (Ethernet)
        RX packets 505  bytes 42601 (41.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 95  bytes 12266 (11.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 0  (Local Loopback)
        RX packets 8  bytes 556 (556.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 556 (556.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

说明:

  • eth0 网卡名
  • inet 172.25.74.124 ip地址
  • mtu 1500 最大传输单元
  • netmask 255.255.255.0 子网掩码
  • broadcast 172.25.74.255 广播地址
  • inet6 fe80::5054:ff:fe00:4a0b prefixlen 64 ipv6的ip地址和子网掩码
  • ether 52:54:00:00:4a:0b mac地址

lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 HTTPD服务器的指定到回坏地址,在浏览器输入 127.0.0.1 就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。


2.ip命令

ip addr add ip/24 dev eth0 #为eth0网卡添加一个ip
ip addr show eth0 #查看eth0网卡的ip
ip addr del ip/24 dev eth0 #删除eth0网卡指定ip


3.nmcli命令

nmcli ##NetworkManager必须开启

nmcli device connect eth0 ##启用eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
nmcli device status eth0 ##查看网卡服务接口信息

用nm-connection-editor改名称为westos

nmcli connection show
nmcli connection down westos
nmcli connection up westos
nmcli connection delete westos
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.100/24 ##对应nm-connection-editor来写
nmcli connection modify westos ipv4.method auto
nmcli connection modify westos ipv4.method manual
nmcli connection modify westos ipv4.addresses 172.25.254.200/24


4.管理网络配置文件

位置:网络配置目录/etc/sysconfig/network-scripts/

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

以我的br0网卡为例介绍

DEVICE=br0                 #网卡接口名称
NAME="Bridge br0"      #在图形中的网卡名,网络连接的名字
ONBOOT=yes              #网络服务开启时自动激活网卡
NM_CONTROLLED=yes
BOOTPROTO=none      #设备工作方式    设置为none禁止DHCP,设置为static启用静态IP地址,设置为dhcp开启DHCP服务
IPADDR0=172.25.254.74     #第一个ip地址
PREFIX0=24              #第一个ip的子网掩码
DEFROUTE=yes        
ZONE=trusted     
IPADDR1=172.25.74.250
PREFIX1=24

5.虚拟机联网

1.首先设置真实主机为路由器,作为虚拟机的网关

[root@foundation74 network-scripts]# firewall-cmd --permanent --add-masquerade     开启地址伪装
success
[root@foundation74 network-scripts]# firewall-cmd --reload   重新加载
success
[root@foundation74 network-scripts]# firewall-cmd --list-all
trusted (active)
  target: ACCEPT
  icmp-block-inversion: no
  interfaces: br0 wlp3s0
  sources: 
  services: 
  ports: 
  protocols: 
  masquerade: yes     <<<地址伪装功能开启,真实主机变成路由器
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

2.设置虚拟机的网关

第一种方法:
vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效
GATEWAY=真机的ip

第二种方法:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY0=真机的ip ##档网卡中设定的IP有多个时,指定对于那个IP生效
GATEWAY=真机的ip ##档网卡中设定的IP只有一个时

[root@server ~]# route -n    查看虚拟机的路由表
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.254.74   0.0.0.0         UG    1024   0        0 eth0
172.25.254.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

网关设置成功后,在真机连网的前提下,就可以ping通外网的ip了,说明可以上网了。

这时你在虚拟机内ping域名是不通的,因为我们还没有配置域名,虚拟机只认识ip,下面我通过真机得到百度的ip,在使用虚拟机测试。

真机

[root@foundation74 network-scripts]# ping www.baidu.com
PING www.a.shifen.com (180.149.131.98) 56(84) bytes of data.
64 bytes from 180.149.131.98 (180.149.131.98): icmp_seq=1 ttl=55 time=26.8 ms

得到百度的ip是180.149.131.98,现在使用虚拟机

[root@server ~]# ping 180.149.131.98
PING 180.149.131.98 (180.149.131.98) 56(84) bytes of data.
64 bytes from 180.149.131.98: icmp_seq=4 ttl=54 time=23.7 ms

测试成功,也可以打开浏览器,把ip写入地址栏里测试,一样可以打开百度的主页。

3.配置DNS

DNS简单来说就是可以根据域名解析得到ip
domain name server == 域名解析服务 ##解析就是把域名变成IP

第一种配置域名的方法:
vim /etc/hosts ##本地解析文件
ip 域名 在这个文件里加入你常用的ip以及他的域名
180.149.131.98 www.baidu.com

[root@server ~]# systemctl restart network     //修改了配置文件要重起服务
[root@server ~]# ping www.baidu.com         //直接ping域名
PING www.baidu.com (180.149.131.98) 56(84) bytes of data.
64 bytes from www.baidu.com (180.149.131.98): icmp_seq=1 ttl=54 time=25.7 ms

你也可以不叫baidu
180.149.131.98 www.bd.com

[root@server ~]# ping www.bd.com
PING www.bd.com (180.149.131.98) 56(84) bytes of data.
64 bytes from www.bd.com (180.149.131.98): icmp_seq=1 ttl=54 time=31.6 ms

通过域名访问成功。

第二种配置域名的方法(测试前先把刚刚修改的配置恢复成初始状态):
vim /etc/resolv.conf ##域名解析配置文件
nameserver 114.114.114.114 ##电信运营的公用dns
nameserver 218.30.19.50 ##陕西省本地dns

[root@server ~]# systemctl restart network
[root@server ~]# ping www.baidu.com
PING www.a.shifen.com (180.149.132.151) 56(84) bytes of data.
64 bytes from 180.149.132.151: icmp_seq=1 ttl=54 time=23.3 ms

通过域名访问成功。

你可能感兴趣的:(linux网络管理)