Centos7 单用户模式
centos7里再也不有0-6启动级别,而是4个target
graphical.target 多人模式,支持图形和命令行两种登陆,对应以前的3,5级别
multi-user.target 多人模式,只支持命令行登陆,对应以前的3级别
rescue.target 单人模式,对应以前的1级别
emergency.target 单人模式,不过系统进入后根目录是只读的
centos7采用的是grub2,和以前的方式有所不一样
在对应的内核条目上,按下"e",会进入edit模式,搜寻ro那一行,以linux16开头的,按“end”键到最后,输入rd.break,再按ctrl+x进入
进去后从新挂载 /sysroot/,增长写权限
mount -o remount,rw /sysroot/
切换到原始系统下
chroot /sysroot/
passwd
修改密码输入新密码
touch /.autorelabel //这句是为了selinux生效
退出单用户的方法是
先ctrl+d 退出,而后reboot
1.3 centos7救援模式
设置光驱启动
选择Troubleshooting
选择Rescure a centos system
选择continue
chroot /mnt/sysimage
passwd
1.4 设置IP
网卡名字再也不是eth0、eth1
dhclient先自动获取ip
没有 ifconfig命令,要使用 ip add 查看ip
要想继续使用 ifconfig,须要安装 net-tools
yum install -y net-tools
编辑配置文件
vi /etc/sysconfig/network-scripts/ifcfg-eno16777736
里面配置和centos6同样
1.5 设置主机名 && 设置命令自动补全
hostname 查看主机名
hostnamectl set-hostname test1
hostnamectl status //查看主机名状态
cat /etc/hostname //查看配置文件
centos7支持命令参数补全
安装 bash-completion
yum install -y bash-completion
source /etc/profile
1.6 centos7服务相关
centos7 再也不有chkconfig工具了,而是使用systemd
使某服务自启动 systemctl enable httpd.service
不自启动 systemctl disable httpd.service
查看服务状态 systemctl status httpd.service
查看全部服务 systemctl list-units --type=service
启动服务 systemctl start httpd.service
中止服务 systemctl stop httpd.service
重启服务 systemctl restart httpd.service
启动脚本路径 /usr/lib/systemd/system/
检查一个服务是否为开机启动 systemctl is-enabled httpd
1.7 centos7 systemd-unit
systemd管理服务的机制的特色:
支持服务并列启动,不用顺序启动,从而开机时间缩短
支持自动检测服务依赖的服务
Systemd 能够管理全部系统资源。不一样的资源统称为 Unit(单位)。
Unit 一共分红12种类型
Service unit:系统服务 Target unit:多个 Unit 构成的一个组
Device Unit:硬件设备 Mount Unit:文件系统的挂载点
Automount Unit:自动挂载点 Path Unit:文件或路径
Scope Unit:不是由 Systemd 启动的外部进程
Slice Unit:进程组 Snapshot Unit:Systemd 快照,能够切回某个快照
Socket Unit:进程间通讯的 socket Swap Unit:swap 文件
Timer Unit:定时器
列出正在运行的 Unit systemctl list-units
列出全部Unit,包括没有找到配置文件的或者启动失败的
systemctl list-units --all
列出全部没有运行的 Unit systemctl list-units --all --state=inactive
列出全部加载失败的 Unit systemctl list-units --failed
列出全部正在运行的、类型为 service 的 Unit
systemctl list-units --type=service
显示某个 Unit 是否正在运行 systemctl is-active application.service
显示某个 Unit 是否处于启动失败状态
systemctl is-failed application.service
显示某个 Unit 服务是否创建了启动连接
systemctl is-enabled application.service
1.8 centos7 systemd-target
target相似于centos6里面的启动级别,但target支持多个target同时启动。target实际上是多个unit的组合,系统启动说白了就是启动多个unit,为了管理方便,就使用target来管理这些unit。
查看当前系统的全部 Target
systemctl list-unit-files --type=target
查看一个 Target 包含的全部 Unit
systemctl list-dependencies multi-user.target
查看启动时的默认 Target systemctl get-default
设置启动时的默认 Target systemctl set-default multi-user.target
切换 Target 时,默认不关闭前一个 Target 启动的进程,
systemctl isolate 命令改变这种行为,关闭前一个 Target 里面全部不属于后一个 Target 的进程
systemctl isolate multi-user.target
1.9 centos7 target关联unit
主配置文件 /etc/systemd/system.conf
开机会先加载/etc/systemd/system/default.target
全部的service和target在 /usr/lib/systemd/system/
ls -l /etc/systemd/system/defaut.target 是一个软链接
软链接到了/usr/lib/systemd/system/multi-user.target
它会加载/usr/lib/systemd/system/multi-user.target.wants下面的service
查看一个service属于哪一个target,须要cat 具体的service文件
cat /usr/lib/systemd/system/sshd.service
看里面[install]部分
2.0 centos7 继续使用centos6的iptables管理机制
centos7再也不使用iptables,而是使用firewalld
若不想使用firewalld,继续使用iptables,能够停掉firewalld,而且安装iptables-services包
systemctl stop firewalld
systemctl disable firewalld
yum install -y iptables-services
systemctl enable iptables
systemctl start iptables
firewalld
的使用
centos6上使用的静态防火墙,规则保存在/etc/sysconfig/iptalbes, 更新规则,须要从新加载或者重启iptables服务,至关因而把以前的规则所有清空,再把配置文件里面的规则加载。
而centos7使用了动态防火墙,即firewalld,这种防火墙方案再也不像以前,更改了规则不须要从新加载配置。
不管是firewalld仍是以前的iptables服务,底层都是使用iptables工具来配置规则的。
fiewalld有zone和service的概念
每个zone里面的iptables规则不同,默认有9个zone。
centos7的默认zone是public
使用命令 firewall-cmd --get-zones 获取全部的zone
使用命令 firewall-cmd --get-default-zone 获取默认的zone
2.1 centos7 firewalld的zone
drop(丢弃) 任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络链接。
block(限制)任何接收的网络链接都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝。
public(公共)在公共区域内使用,不能相信网络内的其余计算机不会对您的计算机形成危害,只能接收通过选取的链接。
external(外部)特别是为路由器启用了假装功能的外部网。您不能信任来自网络的其余计算,不能相信它们不会对您的计算机形成危害,只能接收通过选择的链接。
dmz(非军事区)用于您的非军事区内的电脑,此区域内可公开访问,能够有限地进入您的内部网络,仅仅接收通过选择的链接。
work(工做)用于工做区。您能够基本相信网络内的其余电脑不会危害您的电脑。仅仅接收通过选择的链接。
home(家庭)用于家庭网络。您能够基本信任网络内的其余计算机不会危害您的计算机。仅仅接收通过选择的链接。
internal(内部) 用于内部网络。您能够基本上信任网络内的其余计算机不会威胁您的计算机。仅仅接受通过选择的链接。
trusted(信任)可接受全部的网络链接。
指定其中一个区域为默认区域是可行的。当接口链接加入了 NetworkManager,它们就被分配为默认区域。安装时,firewalld 里的默认区域被设定为公共区>域。
2.2 centos7 firewalld的zone相关命令
设置默认的zone firewalld-cmd --set-default-zone=work
查看指定网卡所在的zone
firewall-cmd --get-zone-of-interface=eno16777736
给指定的网卡设置zone firewall-cmd --zone=public --add-interface=lo
针对网卡更改zone firewall-cmd --zone=dmz --change-interface=lo
也能够针对网卡删除zone
firewall-cmd --zone=dmz --remove-interface=lo
查看全部网卡所在的zone firewall-cmd --get-active-zones
2.3 centos7 firewalld service操做
在 /usr/lib/firewalld/services/ 目录中,还保存了另一类配置文件,每一个文件对应一项具体的网络服务,如 ssh 服务等
与之对应的配置文件中记录了各项服务所使用的 tcp/udp 端口,在最新版本的 firewalld 中默认已经定义了 70+ 种服务供咱们使用
zone就是调用了不一样的service而实现了不一样的效果
列出全部的service: firewall-cmd --get-services
列出当前zone下加载的service: firewall-cmd --list-services
查看某个zone下面的service:firewall-cmd --zone=public --list-services
给一个zone下面添加一个service:
firewall-cmd --zone=public --add-service=http
这个改动不会保存到配置文件里,要想保存到配置文件中,须要加上--permanent
firewall-cmd --zone=public --add-service=http –-permanent
既然能够增长,一样也能够删除
firewall-cmd --zone=public --remove-service=http --permanent
2.4 centos7 firewalld 增长规则
假设自定义的 ssh 端口号为 12222,使用下面的命令来添加新端口的防火墙规则
firewall-cmd --add-port=12222/tcp --permanent
若是须要使规则保存到 zone 配置文件,则须要加参数 --permanent
防火墙配置文件也能够手动修改,修改后记得重载,从新加载的命令是
firewall-cmd --reload 这种状况下从新加载防火墙并不会中断用户链接,也能够完全加载,这时候会中断用户链接,也会丢弃状态信息
firewall-cmd --complete-reload
还能够添加一个端口范围
firewall-cmd --add-port=2000-4000/tcp
针对某个zone添加端口
firewall-cmd --permanent --zone=home --add-port=443/tcp
启用端口转发,例如把22端口转发到127.0.0.2
firewall-cmd --permanent --zone=home --add-forward-port=port=22:proto=tcp:toaddr=127.0.0.2
2.5 centos7 firewalld 更改配置文件
两个路径/etc/firewalld/和/usr/lib/firewalld
系统使用的是/etc/firewalld下面的配置文件
在/etc/firewalld/zones下面只有一个public.xml,若是给另一个zone作一些改动,并永久保存,那么会自动生成对应的配置文件
好比,给work zone增长一个端口
firewall-cmd --permanent --zone=work --add-port=1000/tcp
此时就会生成一个work.xml配置文件
需求: 修改ftp的端口,默认ftp端口是21,须要改成1121
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
vi /etc/firewalld/services/ftp.xml //把21改成1121
vi /etc/firewalld/zones/work.xml //work为默认zone,因此要编辑这个,增长一行 <service name="ftp"/>
firewall-cmd --reload //从新加载