CentOS 网络配置

CentOS 网络配置

  • 查看网卡信息

    [root@localhost ~]# ip a s
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
        link/ether 00:0c:29:f9:6c:67 brd ff:ff:ff:ff:ff:ff
        inet 192.168.159.129/24 brd 192.168.159.255 scope global noprefixroute dynamic ens33
           valid_lft 1225sec preferred_lft 1225sec
        inet6 fe80::2227:1ed6:6865:8578/64 scope link noprefixroute 
           valid_lft forever preferred_lft forever
    
  • 查看网卡流量

    [root@localhost ~]# ip -s link  show ens33
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
        link/ether 00:0c:29:f9:6c:67 brd ff:ff:ff:ff:ff:ff
        RX: bytes  packets  errors  dropped overrun mcast   
        541259676  441861   0       0       0       0       
        TX: bytes  packets  errors  dropped carrier collsns 
        8109079    133817   0       0       0       0
    

系统开机到现在总共收发字、数据包数量

  • netstat命令

    # 打印网络连接、路由表、网络接口统计
    # -s 显示各种协议数据统计信息
    # -n 使用数字形式的IP、端口号、用户ID替代主机、协议、用户等名称信息。
    # -p 显示进程名称及对应进程ID号
    # -l 仅显示正在监听的sockets接口信息
    # -u 查看upd连接信息
    # -t 查看tcp连接信息
    [root@localhost ~]# netstat -nutlp
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1272/sshd           
    tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1271/cupsd            
    tcp6       0      0 :::22                   :::*                    LISTEN      1272/sshd           
    tcp6       0      0 ::1:631                 :::*                    LISTEN      1271/cupsd           
    udp        0      0 0.0.0.0:904             0.0.0.0:*                           735/rpcbind         
    udp        0      0 0.0.0.0:50225           0.0.0.0:*                           777/avahi-daemon: r  
    udp6       0      0 :::904                  :::*                                735/rpcbind         
    udp6       0      0 :::111                  :::*                                735/rpcbind      
    [root@localhost ~]# netstat -s
    Ip:
        147945 total packets received
        0 forwarded
        0 incoming packets discarded
        147577 incoming packets delivered
        134128 requests sent out
        44 outgoing packets dropped
        346 dropped because of missing route
    ... 省略 ...
    
    • 命令行设置网络参数

    网络接口参数---ifconfig

    # 显示全量网卡信息
    [root@localhost ~]# ifconfig
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.159.129  netmask 255.255.255.0  broadcast 192.168.159.255
            inet6 fe80::2227:1ed6:6865:8578  prefixlen 64  scopeid 0x20<link>
    
    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>
    # 显示指定网卡信息        
    [root@localhost ~]# ifconfig ens33
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.159.129  netmask 255.255.255.0  broadcast 192.168.159.255
            inet6 fe80::2227:1ed6:6865:8578  prefixlen 64  scopeid 0x20<link>
    # 关闭网卡
    [root@localhost ~]# ifconfig ens33 down
    # 关闭后查看网卡信息
    [root@localhost ~]# ifconfig
    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 1000  (Local Loopback)
            
    # 开启网卡
    [root@localhost ~]# ifconfig ens33 up
    # 开启后查看网卡信息
    [root@localhost ~]# ifconfig 
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.159.129  netmask 255.255.255.0  broadcast 192.168.159.255
            inet6 fe80::2227:1ed6:6865:8578  prefixlen 64  scopeid 0x20<link>
    
    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>
    

    主机名参数hostnamectl

# 显示系统主机名称
[root@localhost ~]# hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: f17c01b36a694a24ac23d87c18bcd8be
           Boot ID: cebdc9dbb5804db187c08c5dcbfcabf3
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64
# 设置主机名称
[root@localhost ~]# hostnamectl set-hostname moke
# 查看主机名称
[root@localhost ~]# hostnamectl
   Static hostname: moke
         Icon name: computer-vm
           Chassis: vm
        Machine ID: f17c01b36a694a24ac23d87c18bcd8be
           Boot ID: cebdc9dbb5804db187c08c5dcbfcabf3
    Virtualization: vmware
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64

路由参数---route

# 显示当前路由表
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.159.2   0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.159.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

# 添加默认路由网关
[root@localhost ~]# route add default gw 192.168.159.2
# 添加指定网段网关
[root@localhost ~]# route add -net 172.16.0.0/16 gw 192.168.159.2 dev ens33
# 删除默认网关
[root@localhost ~]# route del default gw 192.168.159.2
# 删除指定网段网关
[root@localhost ~]# route del -net 172.16.0.0/16
  • 文件修改网络参数
# 网卡配置修改
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
# 重启网络服务使新设置生效
[root@localhost ~]# systemctl restart network
# 主机名修改
[root@localhost ~]# vim /etc/hostname
# 路由信息设置(默认文件不存在,配置时创建)
[root@localhost network-scripts]# vim /etc/sysconfig/network-scripts/route-ens33
# 重启网络服务使新设置生效
[root@localhost ~]# systemctl restart network

感谢阅读,欢迎交流

posted @ 2021-03-14 22:43  墨客moke  阅读(182)  评论(0编辑  收藏  举报