linux子网掩码
① linux配置网络参数之IP地址、子网掩码、网关地址,DNS
修改网卡命令规则 (eth0、eth1、eth2……)
打开grub内核引导程序,在……quiet 后面添加:
让网卡命名规则生效
nmcli命令删除错误网卡命名
nmcli命令添加网卡命名
解析: nmcli connection 添加 类型 以太网设备 网卡设备名为eth0 nmcli命令的命名为eth0
修改IP地址、子网掩码、网关地址、DNS
解析:nmcli connection 修改 网卡名 ipv4.方法 手工配置 ipv4.地址192.168.4.7/24 ipv4.网关 192.168.4.254 每次开机自动启用以上所有参数
查看网卡配置文件
查看网关地址信息
② Linux怎么设置IP,子网掩码和网关
linux一般使用ifconfig命令修改linux主机的ip、网关或子网掩码。 1.命令格式: ifconfig [网络设备] [参数] 2.命令功能: ifconfig 命令用来查看和配置网络设备。当网络环境发生改变时可通过此命令对网络进行相应的配置。 3.命令参数: up 启动指定网络设备/网卡。 down 关闭指定网络设备/网卡。该参数可以有效地阻止通过指定接口的IP信息流,如果想永久地关闭一个接口,我们还需要从核心路由表中将该接口的路由信息全部删除。 arp 设置指定网卡是否支持ARP协议。 -promisc 设置是否支持网卡的promiscuous模式,如果选择此参数,网卡将接收网络中发给它所有的数据包 -allmulti 设置是否支持多播模式,如果选择此参数,网卡将接收网络中所有的多播数据包 -a 显示全部接口信息 -s 显示摘要信息(类似于 netstat -i) add 给指定网卡配置IPv6地址 del 删除指定网卡的IPv6地址 <硬件地址> 配置网卡最大的传输单元 mtu<字节数> 设置网卡的最大传输单元 (bytes) netmask<子网掩码> 设置网卡的子网掩码。掩码可以是有前缀0x的32位十六进制数,也可以是用点分开的4个十进制数。如果不打算将网络分成子网,可以不管这一选项;如果要使用子网,那么请记住,网络中每一个系统必须有相同子网掩码。 tunel 建立隧道 dstaddr 设定一个远端地址,建立点对点通信 -broadcast<地址> 为指定网卡设置广播协议 -pointtopoint<地址> 为网卡设置点对点通讯协议 multicast 为网卡设置组播标志 address 为网卡设置IPv4地址 txqueuelen<长度> 为网卡设置传输列队的长度 4.使用实例: 实例1:显示网络设备信息(激活状态的) 命令: ifconfig 输出: [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20 inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB) 说明: eth0 表示第一块网卡, 其中 HWaddr 表示网卡的物理地址,可以看到目前这个网卡的物理地址(MAC地址)是 00:50:56:BF:26:20 inet addr 用来表示网卡的IP地址,此网卡的 IP地址是 192.168.120.204,广播地址, Bcast:192.168.120.255,掩码地址Mask:255.255.255.0 lo 是表示主机的回坏地址,这个一般是用来测试一个网络程序,但又不想让局域网或外网的用户能够查看,只能在此台主机上运行和查看所用的网络接口。比如把 HTTPD服务器的指定到回坏地址,在浏览器输入 127.0.0.1 就能看到你所架WEB网站了。但只是您能看得到,局域网的其它主机或用户无从知道。 第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址) 第二行:网卡的IP地址、子网、掩码 第三行:UP(代表网卡开启状态)RUNNING(代表网卡的网线被接上)MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节 第四、五行:接收、发送数据包情况统计 第七行:接收、发送数据字节数统计信息。 实例2:启动关闭指定网卡 命令: ifconfig eth0 up ifconfig eth0 down 输出: 说明: ifconfig eth0 up 为启动网卡eth0 ;ifconfig eth0 down 为关闭网卡eth0。ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。 实例3:为网卡配置和删除IPv6地址 命令: ifconfig eth0 add 33ffe:3240:800:1005::2/64 ifconfig eth0 del 33ffe:3240:800:1005::2/64 输出: 说明: ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0配置IPv6地址; ifconfig eth0 add 33ffe:3240:800:1005::2/64 为网卡eth0删除IPv6地址; 练习的时候,ssh登陆linux服务器操作要小心,关闭了就不能开启了,除非你有多网卡。 实例4:用ifconfig修改MAC地址 命令: ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE 输出: [root@localhost ~]# ifconfig eth0 down //关闭网卡 [root@localhost ~]# ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE //修改MAC地址 [root@localhost ~]# ifconfig eth0 up //启动网卡 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:AA:BB:CC:DD:EE inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB) [root@localhost ~]# ifconfig eth0 hw ether 00:50:56:BF:26:20 //关闭网卡并修改MAC地址 [root@localhost ~]# ifconfig eth0 up //启动网卡 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:20 inet addr:192.168.120.204 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8700857 errors:0 dropped:0 overruns:0 frame:0 TX packets:31533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:596390239 (568.7 MiB) TX bytes:2886956 (2.7 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:68 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2856 (2.7 KiB) TX bytes:2856 (2.7 KiB) 说明: 实例5:配置IP地址 命令: 输出: [root@localhost ~]# ifconfig eth0 192.168.120.56 [root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 [root@localhost ~]# ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255 说明: ifconfig eth0 192.168.120.56 给eth0网卡配置IP地:192.168.120.56 ifconfig eth0 192.168.120.56 netmask 255.255.255.0 给eth0网卡配置IP地址:192.168.120.56 ,并加上子掩码:255.255.255.0 ifconfig eth0 192.168.120.56 netmask 255.255.255.0 broadcast 192.168.120.255 /给eth0网卡配置IP地址:192.168.120.56,加上子掩码:255.255.255.0,加上个广播地址: 192.168.120.255 实例6:启用和关闭ARP协议 命令: ifconfig eth0 arp ifconfig eth0 -arp 输出: [root@localhost ~]# ifconfig eth0 arp [root@localhost ~]# ifconfig eth0 -arp 说明: ifconfig eth0 arp 开启网卡eth0 的arp协议; ifconfig eth0 -arp 关闭网卡eth0 的arp协议; 实例7:设置最大传输单元 命令: ifconfig eth0 mtu 1500 输出: [root@localhost ~]# ifconfig eth0 mtu 1480 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1480 Metric:1 RX packets:8712395 errors:0 dropped:0 overruns:0 frame:0 TX packets:36631 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:597062089 (569.4 MiB) TX bytes:2643973 (2.5 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:9973 errors:0 dropped:0 overruns:0 frame:0 TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB) [root@localhost ~]# ifconfig eth0 mtu 1500 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:50:56:BF:26:1F inet addr:192.168.120.203 Bcast:192.168.120.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8712548 errors:0 dropped:0 overruns:0 frame:0 TX packets:36685 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:597072333 (569.4 MiB) TX bytes:2650581 (2.5 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:9973 errors:0 dropped:0 overruns:0 frame:0 TX packets:9973 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:518096 (505.9 KiB) TX bytes:518096 (505.9 KiB) [root@localhost ~]# 说明: 设置能通过的最大数据包大小为 1500 bytes 备注:用ifconfig命令配置的网卡信息,在网卡重启后机器重启后,配置就不存在。要想将上述的配置信息永远的存的电脑里,那就要修改网卡的配置文件了。
③ 1、Linux系统基本网络
1.1、服务器注意事项:
远程服务器不允许关机,只能重启
重启时应该先关闭服务
不要在服务器访问高峰运行高负载命令
远程配置防火墙时不要把自己踢出服务器
指定合理的密码规范并定期更新
合理分配权限
定期备份重要的数据和日志
1.2、设置网络桥接命令和(ANT模式):
systemctl restart network------重启网卡service network restart---------重启网络服务
systemctl stop NetworkManager 临时暂停网络管理器systemctl disable NetworkManager 永久关闭网络管理器
systemctl start NetworkManager 拥有root用户的可执行权限
systemctl stop NetworkManager 停止并禁用虚拟机 NetworkManager 服务
systemctl disable NetworkManager
注意:修改网络配置文件后,需要重新加载网络连接,如果是通过network.service则使用命令:systemctl restart network;如果是通过NetworkManager.service则使用nmcli命令:nmcli connection reload。
设置网络主要操作(桥接模式和)
[root@localhost network-scripts]# cd etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vi ifcfg-ens33
systemctl restart network------重启网卡
service network restart---------重启网络服务
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static 设置静态
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=10c17057-e9c9-4831-a8ff-0757ea0abc0b
DEVICE=ens33
ONBOOT=yes #开机重启
#IPADDR=192.168.43.168 #ip地址(需要跟主机同一个网段,不同一个IP)
IPADDR=10.63.73.20
#NETMASK=255.255.255.0 #子网掩码(下面三个都跟主机一样)
NETMASK=255.255.0.0
#GATEWAY=192.168.43.1 #网关
GATEWAK=10.63.255.254
#GATEWAK=10.200.0.3
#DNS1=192.168.43.1 #DNS服务
设置网络主要步骤(Nat模式)
1.打开Vm,点击编辑->虚拟网络编辑
2.选择VMnet8,将VMnet信息改为NAT模式,比如我的本机IP是192.168.138.1,子网掩码为255.255.255.0
所以我将下面的IP配置成192.168.138.0 子网掩码配置成255.255.255
3.再点击NAT设置将网管配置成192.168.138.2(ps:网关不要配置成和自己IP地址一样)
然后再应用确定
4.右键点击虚拟机设置,网络适配器改为自定义(选择Vmnet8 Nat模式)
5.接下来开启虚拟机
cd /etc/sysconfig/network-scripts/
vim ifcfg-eno16777736
然后再执行命令:/etc/init.d/network restart
1.3、设置防火墙
1.3.1、防火墙命令
停止防火墙:
systemctl stop firewalld.service #停止firewall 防火墙
service iptables stop #(centos7版本之前)
永久关闭防火墙:
systemctl disable firewalld.service #禁止firewall开机启动 ( 永久关闭防火墙 )
chkconfig iptables off #永久关闭防火墙
开始防火墙:
systemctl start firewalld #启动防火墙
systemctl restart iptables.service #重启防火墙使配置生效
systemctl enable iptables.service #设置防火墙开机启动(重启)
查看防火墙状态:
systemctl status firewalld
service iptables status # (7版本之前)
1.3.2、Linux chkconfig 命令
Linux chkconfig 命令用于检查,设置系统的各种服务。
这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务。
语法
chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]
参数 :
--add 增加所指定的系统服务,让 chkconfig 指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。
--del 删除所指定的系统服务,不再由 chkconfig 指令管理,并同时在系统启动的叙述文件内删除相关数据。
--level<等级代号> 指定读系统服务要在哪一个执行等级中开启或关毕。
实例
列出chkconfig 所知道的所有命令,可以用chkconfig –list查看所有的服务及其在每个级别的开启状态。
# chkconfig --list
开启服务
# chkconfig telnet on //开启 Telnet 服务
# chkconfig --list //列出 chkconfig 所知道的所有的服务的情况
关闭服务
# chkconfig telnet off // 关闭 Telnet 服务
# chkconfig --list // 列出 chkconfig 所知道的所有的服务的情况
[root@cent01 sbin]# chkconfig --level 3 network off //关闭3级别的network服务
[root@cent01 sbin]# chkconfig --list //3级别已关闭
network 0:关 1:关 2:开 3:关 4:开 5:开 6:关
[root@cent01 sbin]# chkconfig network on //不输入级别,默认打开2,3,4,5级别
[root@cent01 sbin]# chkconfig --list
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@cent01 sbin]# chkconfig --del network //删除network
[root@cent01 sbin]# chkconfig --list //network已消失
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
[root@cent01 sbin]# chkconfig --add network //增加network服务
[root@cent01 sbin]# chkconfig --list //network服务又恢复了
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@cent01 sbin]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
注意: 但是这里只显示了SysV管理的服务,centos6及之前都是这个管理工具,但是在centos7用的是systemd管理,所以systemd管理的服务在这里没有显示出来。
运行级别为系统启动级别,具体含义如下:
0 shutdown关机
1 单用户模式
2 无NFS支持的多用户模式
3 完全多用户模式,常用的命令行模式
4 保留给用户自定义
5 图形界面登录,比3多了一个图形界面
6 重启
1.3.2、Linux systemd命令
systemd是管理开机启动程序的工具(SysV启动开机进程时一次只能启动一个,而systemd则一次可以启动多个服务,这样就导致systemd的开机速度会更快。)
[root@localhost ~]# systemctl list-units --all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coremp hook
abrt-oops.service loaded active running ABRT kernel log watcher
UNIT LOAD ACTIVE SUB DESCRIPTION
[root@cent01 sbin]# ls /usr/lib/systemd/system //启动的脚本文件目录
[root@cent01 ~]# systemctl list-units //列出正在运行的unit
[root@cent01 ~]# systemctl list-units --all //列出所有的unit,包括active和inactive
[root@cent01 ~]# systemctl list-units --all --state=inactive //列出inactive的unit
[root@cent01 ~]# systemctl list-units --all --type=service //列出所有状态的service
[root@cent01 ~]# systemctl list-units --type=service //列出active的service
[root@cent01 ~]# systemctl is-active crond.service //查看某个unit是否active
systemctl enable crond.service //开机启动 .service可以省略
systemctl disable crond.service //禁止开机启动
systemctl status crond.service //查看服务状态
systemctl start crond.service //启动服务
systemctl stop crond.service //停止服务
systemctl restart crond.service //重启服务
systemctl is-enabled crond.service //查看某个服务是否开机启动
unit /usr/lib/systemd/system 此目录下列出了很多文件,这些文件都是unit。类别如下:
service 系统服务target 多个unit组成的组device 硬件设备mount 文件系统挂载点automount 自动挂载点path 文件或路径scope 不是由systemd启动的外部进程slice 进程组snapshot systemd快照socket 进程间通信的套接字swap swap文件timer 定时器
target target类似于centos6的启动级别,target内又包含多个unit的组合,当然target内也可以包含target。启动target就是启动多个unit,用target来管理这些unit。
[root@cent01 ~]# systemctl list-units --all --type=target //查看当前所有的target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
bluetooth.target loaded active active Bluetooth
[root@localhost ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
[root@localhost ~]# systemctl get-default //查看系统默认的target
multi-user.target
systemctl set-default multi-user.target //设置默认的target
multi-user.target等同于centos6的运行级别3。他们的对应关系如下:
SysV运行级别systemd target备注
0poweroff.target关闭系统
1rescure.target单用户模式
2multiuser.target用户自定义级别,通常识别为3
3multiuser.target多用户命令行模式
4multiuser.target用户自定义级别,通常识别为3
5graphical.target多用户图形界面,比级别3只多一个GUI
6reboot.target重启
所以总结起来,一个service属于一种unit,多个unit组成一个target,当然target里面也可以包含target。
1.4、ifconfig命令配置IP网络参数
格式:
[root@localhost /]#ifconfig [网络设备] [ip地址] [MAC地址] [netmask掩码地址] [broadcast广播地址(NDC)] [up/down]
[root@localhost /]#ifconfig eth0 192.168.74.130 netmask 255.255.255.0 up
用ifconfig命令配置eth0别名设备,为eth0绑定多个IP地址。
[root@localhost /]#ifconfig eth0:1 192.168.74.130
[root@localhost /]#ifconfig eth0:2 192.168.73.130
1.5、使用routedel命令添加路由
格式:
[root@localhost /]#routedel [-net|host] [网域或主机] netmask [mask] [gw]
[root@localhost /]#route #查看路由信息
功能:添加路由
-net : 表示后面接的路由为一个网络。
-host : 表示后面接的为连接到单部主机的路由。
netmask : 与网络有关,可以设定netmask决定网路的大小。
gw : gateway (网关)的简写,后面接的是ip地址。
1.6、使用hostname命令修改主机名称
[root@localhost /]#hostname service.jw.com
hostnamectl set-hostname nod1
④ 在LINUX下如何用命令 配置ip地址 子网掩码和网管
1、首先进入到Linux系统中,输入用户名和密码完成登录。