网站建设资讯

NEWS

网站建设资讯

Linux网络服务之网络设置相关命令-创新互联

Linux网络设置

  1. 查看网络设置

    昌黎ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
  2. 测试网络连接

  3. 使用网络配置命令
  4. 修改网络配置文件

​ 本文主要介绍Linux中一些基础常用的Linux网络设置命令与一些脚本文件配置步骤。

一、查看及测试网络配置

1.ifconfig——查看网络接口地址

格式:ifconfig [网络接口名] [ip地址](子网掩码)

[root@lokott ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 3556  bytes 232528 (227.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 104  bytes 16400 (16.0 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 1  (Local Loopback)
        RX packets 56  bytes 6144 (6.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 56  bytes 6144 (6.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:71:37:09  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@lokott ~]# ifconfig ens33
ens33: flags=4163  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 3560  bytes 232768 (227.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 104  bytes 16400 (16.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2.hostname ——查看主机名

格式:hostname

[root@lokott ~]# hostname
lokott
[root@lokott ~]# cat /etc/hostname
lokott
[root@lokott ~]# echo "local" > /etc/hostname 
[root@lokott ~]# cat /etc/hostname
local
[root@lokott ~]#需要重启才可以设置成功,另一种方法是使用hostnamectl set-hostname 主机名^c
[root@lokott ~]#reboot
[root@local ~]# hostnamectl set-hostname lokott
[root@local ~]# su
[root@lokott ~]# 换为lokott了^c

3.route——查看路由条目

-n:数字显示(centos6中,7中加不加几乎一样)

[root@lokott ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@lokott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.68.2    0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

4.netstat——查看网络连接情况

-n 序列号

-p 端口号

-a 所有服务端口

-r 显示路由表信息

-l 显示处于监听状态的网络连接及端口信息

-t tcp

-u udp

[root@lokott ~]# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1490/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      941/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      939/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1186/master         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      941/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      939/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1186/master         

[root@lokott ~]# netstat -nuap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
udp        0      0 0.0.0.0:18066           0.0.0.0:*                           3935/dhclient       
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1490/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1490/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           3935/dhclient       
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           553/avahi-daemon: r 
udp        0      0 0.0.0.0:50467           0.0.0.0:*                           553/avahi-daemon: r 
udp6       0      0 :::61206                :::*                                3935/dhclient    

[root@lokott ~]# netstat -r       //与route一样
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U         0 0          0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0

5.traceroute—— 测试从当前主机到目标主机之间经过的网络节点

[root@lokott ~]# traceroute 127.0.0.1
traceroute to 127.0.0.1 (127.0.0.1), 30 hops max, 60 byte packets
 1  localhost (127.0.0.1)  0.030 ms  0.007 ms  0.035 ms
[root@lokott ~]# traceroute 192.168.10.11
traceroute to 192.168.10.11 (192.168.10.11), 30 hops max, 60 byte packets
 1  gateway (192.168.68.2)  0.154 ms  0.085 ms  0.087 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *
 7  *^C
[root@lokott ~]#

6.nslookup—— 测试DNS域名解析

[root@lokott ~]# nslookup www.taobao.com
Server:     192.168.68.2
Address:    192.168.68.2#53

Non-authoritative answer:
www.taobao.com  canonical name = www.taobao.com.danuoyi.tbcache.com.
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.235
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 111.3.79.234
Name:   www.taobao.com.danuoyi.tbcache.com
Address: 211.138.124.238

补充:在Windows系统的dos界面也一样适用

二、设置络地址参数

1.临时配置——调试网络时使用

2.固定配置——更改配置文件,需要重启network服务或重启主机后才会生效,相当于永久配置。

7.ifdown 和ifup 禁用、激活网络接口

[root@lokott ~]# ifconfig ens33:0 192.168.155.55  //虚接口配置
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163  mtu 1500
        inet 192.168.155.55  netmask 255.255.255.0  broadcast 192.168.155.255
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)

[root@lokott ~]# ifconfig ens33:0 down  
[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163  mtu 1500
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)

[root@lokott ~]# ifconfig ens33:0 192.168.155.55
[root@lokott ~]# ifdown ens33:0 
用法:ifdown <设备名>
[root@lokott ~]# ifdown ens33 
成功断开设备 'ens33'。
[root@lokott ~]# ifconfig ens33
ens33: flags=4163  mtu 1500
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 5048  bytes 329861 (322.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 490  bytes 46907 (45.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@lokott ~]# ifconfig ens33:0
ens33:0: flags=4163  mtu 1500
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
[root@lokott ~]# ifup ens33 
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/6)
[root@lokott ~]# ifconfig ens33
ens33: flags=4163  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 5082  bytes 332183 (324.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 514  bytes 50940 (49.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

8.route添加、删除静态路由记录

[root@lokott ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@lokott ~]# route add -net 192.168.10.0/24 gw 192.168.122.1
[root@lokott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.68.2    0.0.0.0         UG    100    0        0 ens33
192.168.10.0    192.168.122.1   255.255.255.0   UG    0      0        0 virbr0
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
[root@lokott ~]# route del -net   192.168.10.0/24    //删除路由记录
[root@lokott ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.68.2    0.0.0.0         UG    100    0        0 ens33
192.168.68.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

[root@lokott ~]# route del default gw 192.168.68.2  //删除默认网关
[root@lokott ~]# route add default gw 192.168.68.2  //添加默认网关

9.网络接口配置文件

[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE="Ethernet"                     //类型为以太网
PROXY_METHOD="none"                 
BROWSER_ONLY="no"
BOOTPROTO="dhcp"                      //动态获取
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"      
NAME="ens33"                        //网卡名字为ens33
UUID="5785d9d2-2603-4b14-bbfb-d95f6545e260"
DEVICE="ens33"                      //设置网络接口名称为ens33
ONBOOT="yes"                        //设置网络接口在Linux系统启动时激活
[root@lokott ~]#

当然我们可以设置静态ip地址如下(务必重启网络服务):

[root@lokott ~]# vim  /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@lokott ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="5785d9d2-2603-4b14-bbfb-d95f6545e260"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.68.130"
NETMASK="255.255.255.0"
GATEWAY="192.168.68.2"
[root@lokott ~]# systemctl restart network
[root@lokott ~]# ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.68.130  netmask 255.255.255.0  broadcast 192.168.68.255
        inet6 fe80::7eb1:2dde:8a54:6927  prefixlen 64  scopeid 0x20
        ether 00:0c:29:56:d3:4a  txqueuelen 1000  (Ethernet)
        RX packets 5803  bytes 377581 (368.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 552  bytes 55914 (54.6 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 1  (Local Loopback)
        RX packets 168  bytes 16496 (16.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 168  bytes 16496 (16.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:71:37:09  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@lokott ~]#

10.域名解析配置文件

1)指定为本机提供DNS解析的服务器地址信息

[root@lokott ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 192.168.68.2
[root@lokott ~]#

2)本地主机映射文件

[root@lokott ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

​ 一般可以将经常需要访问的主机信息存放在这里,根据这里的映射表可以查找是否有与之对应的记录,没有再去DNS服务器查询,从而提高上网速度。但如果错误添加就会出现异常。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:Linux网络服务之网络设置相关命令-创新互联
链接URL:http://njwzjz.com/article/cchhji.html