GrandOB

Linux网络配置设置

网络配置文件位置

文件类型 文件位置 文件作用
网卡文件 /etc/sysconfig/network-scripts/ifcfg-* 修改IP地址、DNS、网关、获取方式等设置
主机名 /etc/hostname 永久修改主机名,只有第一行有效
DNS文件 /etc/resolv.conf 记录了DNS服务地址
路由 /etc/sysconfig/network-scripts/route-ens33 永久记录了路由

网络配置设置命令

ifconfig

命令 作用
ifconfig 查看所有打开的网卡信息
ifconfig -a 查看所有网卡信息(包括关闭的)
ifconfig 网卡名 查看单独的网卡信息
ifconfig 网卡名 up 开启网卡
ifconfig 网卡名 down 关闭网卡
ifconfig 网卡名:0 ip/子网 临时配置网卡
ifconfig 网卡名 ip/子网 临时给网卡配置
ifconfig 网卡名 0 取消网卡配置

永久配置网卡

#配置网卡文件
[root@7_1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
#配置获取方式 静态:static none 动态:DHCP
BOOTPROTO=static
#注释名字(不造成影响)
NAME=eth0
#网卡命名要与文件名相对应
DEVICE=eth0
#是否打开配置
ONBOOT=yes
#手动配置的IP
IPADDR=172.16.87.10
#手动配置子网掩码
NETMASK=255.255.255.0
#手动配置的网关
GATEWAY=172.16.87.2
#手动配置的DNS至少有一个用于连接外网
DNS1=218.2.135.1
DNS2=114.114.114.114
DNS3=8.8.8.8

[root@7_1 ~]# vim /etc/default/grub ----第六行最后添加net.ifnames=0
1 GRUB_TIMEOUT=5
  2 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
  3 GRUB_DEFAULT=saved
  4 GRUB_DISABLE_SUBMENU=true
  5 GRUB_TERMINAL_OUTPUT="console"
  6 GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb     quiet net.ifnames=0"
  7 GRUB_DISABLE_RECOVERY="true"

[root@7_1 ~]# grub2-mkconfig -o /boot/grub2/grub.cfg ----重新生成 grub的配置文件
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-b36091ce8917443388c79301e650887c
Found initrd image: /boot/initramfs-0-rescue-b36091ce8917443388c79301e650887c.img
done


#重启后生效
[root@7_1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.87.10  netmask 255.255.255.0  broadcast 172.16.87.255
        inet6 fe80::20c:29ff:fe18:fc02  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:18:fc:02  txqueuelen 1000  (Ethernet)
        RX packets 309  bytes 42299 (41.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 139  bytes 15759 (15.3 KiB)
        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 1  (Local Loopback)
        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

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:11:c2:c6  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


IP

查看本机网络配置

[root@7_1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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 qlen 1000
    link/ether 00:0c:29:18:fc:02 brd ff:ff:ff:ff:ff:ff
    inet 172.16.87.10/24 brd 172.16.87.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d93d:12d9:c437:3ee1/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:11:c2:c6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:11:c2:c6 brd ff:ff:ff:ff:ff:ff

查看网卡启用情况

[root@7_1 ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:18:fc:02 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:11:c2:c6 brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen 1000
    link/ether 52:54:00:11:c2:c6 brd ff:ff:ff:ff:ff:ff

route

临时配置

命令 作用
route -n 显示路由配置,-n以数字形式显示,可以查看网关信息
route add -net 网段/子网掩码 gw 网关 添加静态路由
route del -net 网段/子网掩码 删除静态路由
#查看路由配置
[root@7_1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.87.2     0.0.0.0         UG    100    0        0 eth0
172.16.87.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

#添加静态路由
[root@7_1 ~]# route add -net 10.0.0.0/8 gw 172.16.87.2
[root@7_1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.87.2     0.0.0.0         UG    100    0        0 eth0
10.0.0.0        172.16.87.2     255.0.0.0       UG    0      0        0 eth0
172.16.87.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

#删除路由
[root@7_1 ~]# route del -net 10.0.0.0/8
[root@7_1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.87.2     0.0.0.0         UG    100    0        0 eth0
172.16.87.0     0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

#永久添加路由
[root@7_1 ~]# vim /etc/sysconfig/network-scripts/route-ens33
10.0.0.0/24 via 172.16.87.2
[root@7_1 ~]# systemctl restart network

hostname

命令 作用
hostname 查看主机名
hostname 主机名 临时修改主机名
vim /etc/hostname 永久修改主机名,只有第一行生效
hostnamectl srt-hostname 名字 永久修改主机名

查看网络连接情况

netstat (慢)

-a:显示主机中所有活动的网络连接信息(包括监听、非监听状态的服务端口)
-n:以数字的形式显示相关的主机地址、端口等信息 
-r:显示路由表信息
-l:显示处于监听(Listening)状态的网络连接及端口信息 
-t:查看 TCP(Transmission Control Protocol,传输控制协议)相关的信息 
-u:显示 UDP(User Datagram Protocol,用户数据报协议)协议相关的信息
-p:显示与网络连接相关联的进程号、进程名称信息(该选项需要 root 权限)

[root@7_1 ~]# netstat -natp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      742/ngin: master p         
[root@7_3 ~]# netstat -naup |grep dhcpd
udp        0      0 0.0.0.0:67              0.0.0.0:*                           7744/dhcpd   

ss( socket statistics)(快)

ss命令是Linux CentOS 7中iproute软件包的一部分,默认已经安装。

#选项:
-t: tcp协议相关   只显示  tcp协议
-u: udp协议相关   只显示   udp
-w: 裸套接字相关
-x:unix sock相关
-l: listen状态的连接
-a: 所有状态                        
-n: 数字格式
-p: 相关的程序及PID
-e: 扩展的信息
-m:内存用量
-o:计时器信息
-r: --resolve 把 IP 解释为域名,把端口号解释为协议名称。 dns 服务

#TCP常用命令(-natp)
[root@7_1 ~]# ss -natp | grep nginx
LISTEN     0      128          *:80                       *:*                   users:((nginx",pid=745,fd=6),("nginx",pid=742,fd=6))

#DUP常用命令(-naup)
[root@7_3 ~]# ss -naup |grep dhcpd
UNCONN     0      0            *:67                       *:*                   users:((dhcpd",pid=7744,fd=7))

posted on 2024-05-04 22:54  OB书写  阅读(97)  评论(0编辑  收藏  举报

导航