配置网络

VMnet8 设置静态 IP

本地IP设置成静态

Centos 网络设配器为 NAT 模式

NAT 模式

VMware 虚拟网络设置

虚拟网络

进入CentOS7,查看IP

1
[root@localhost ~]# ip addr

发现虚拟网卡是ens33

修改ens33的ip地址

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33

# 添加内容如下
修改
BOOTPROTO=static #把动态获取IP修改为静态的

新增
IPADDR=192.168.5.100 # 设置 IP
NETMASK=255.255.255.0 # 子网掩码
GATEWAY=192.168.5.2 # 网关
DNS1=192.168.5.2 # DNS
ONBOOT=yes #重启后生效

重启 network

1
2
3
[root@localhost ~]# systemctl restart network
或者
[root@localhost ~]# service network restart

如果重启失败,尝试关闭NetworkManger服务,然后再尝试重启network

1
2
[root@localhost ~]# service NetworkManager stop      # 关闭服务
[root@localhost ~]# chkconfig NetworkManager off # 并且禁止开机启动

关闭防火墙

下面是red hat/CentOs7关闭防火墙的命令!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1:查看防火状态
systemctl status firewalld
service iptables status

2:暂时关闭防火墙
systemctl stop firewalld
service iptables stop

3:永久关闭防火墙
systemctl disable firewalld
chkconfig iptables off

4:重启防火墙
systemctl enable firewalld
service iptables restart

如果不想关闭,只为开放端口

1
2
3
4
5
# 关闭80端口
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT

# 保存配置
/etc/rc.d/init.d/iptables save

如果不想关闭,只为开放服务

1
2
3
4
# 放行cockpit服务
firewall-cmd --add-service=cockpit --permanent

firewall-cmd --reload

修改 hostname

ssh 到 centos 设备,然后使用下面命令修改配置文件

1
[root@VM-20-10-centos ~]# sudo vi /etc/sysconfig/network

可以看到文件的内容

1
2
3
# Created by cloud-init on instance boot automatically, do not edit.
#
NETWORKING=yes

追加数据HOSTNAME=Peng
然后使用 wq!保存

然后修改 hosts 文件来匹配修改后的 hostname

1
[root@VM-20-10-centos ~]# vi /etc/hosts

修改成这样

1
2
3
4
5
6
7
127.0.0.1 Peng Peng
127.0.0.1 localhost.localdomain localhost
127.0.0.1 localhost4.localdomain4 localhost4

::1 Peng Peng
::1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6

然后使用 hostnamectl 命令来修改 hostname

1
[root@VM-20-10-centos ~]# hostnamectl set-hostname Peng

这时我们可以使用 hostname 命令来查看是否修改成功

1
2
[root@VM-20-10-centos ~]# hostname
peng

最后使用下面的命令来重启 networking 设置来确认我们所做的修改是永久性的。

1
2
[root@VM-20-10-centos ~]# systemctl restart network.service
[root@VM-20-10-centos ~]# systemctl restart systemd-hostnamed

然后 logout 之后再 login 就是新的 hostname 了。