如何屏蔽服务,彻底禁止被启动?
美国、香港服务器
如何屏蔽服务,彻底禁止被启动?
07-03 来源:
一、先搞懂:disable 和 mask 的核心区别
systemctl disable:仅禁止开机自动启动,依然可以手动 systemctl start 启动,也可能被其他服务依赖触发拉起,安全力度弱。
systemctl mask(强制屏蔽):彻底禁止服务以任何方式启动(手动执行启动、开机自启、依赖调用全部失效),原理是把服务软链接指向 /dev/null,相当于让服务配置直接失效。
二、标准屏蔽操作步骤(推荐安全流程)
1. 先停止当前正在运行的服务
bash
运行
systemctl stop 服务名
示例:
bash
运行
systemctl stop vsftpd
2. 强制屏蔽服务(永久禁止启动)
bash
运行
systemctl mask 服务名
执行后提示:
Created symlink from /etc/systemd/system/vsftpd.service to /dev/null.
3. 一步到位(停止 + 屏蔽同时执行)
bash
运行
systemctl mask --now 服务名
4. 验证是否屏蔽成功
bash
运行
# 查看服务状态
systemctl is-enabled 服务名
# 返回 masked 代表屏蔽成功
此时手动启动会直接报错:
Failed to start xxx.service: Unit is masked.
无论手动启动、开机启动、其他服务依赖调用,全部无法运行。
三、取消屏蔽(需要恢复服务时)
bash
运行
# 解除屏蔽
systemctl unmask 服务名
# 重载systemd配置生效
systemctl daemon-reload
# 之后才能正常enable/start
systemctl start 服务名
systemctl enable 服务名
四、临时屏蔽(重启后自动失效)
只当前会话屏蔽,服务器重启后自动解除屏蔽,适合临时安全测试:
bash
运行
systemctl mask --runtime 服务名
五、非 systemd 程序的彻底屏蔽(恶意木马 / 第三方程序)
很多挖矿、陌生进程没有 .service 单元文件,无法用 mask,需要多维度拦截:
杀掉恶意进程
bash
运行
kill -9 进程PID
清理所有自启方式
bash
运行
# 1. 清理当前用户定时任务
crontab -r
# 2. 全局定时任务检索恶意脚本
grep -rE "curl|wget|http" /etc/cron.*
# 3. 清理rc.local开机脚本
vi /etc/rc.local
# 4. 检查init.d旧开机服务
ls /etc/init.d/
删除恶意可执行文件
bash
运行
rm -rf /tmp/恶意程序 /var/tmp/恶意程序
防火墙永久拦截对应端口,双重兜底防护。
六、安全加固最佳实践(高危服务三重防护)
第一步:systemctl mask --now 服务名 强制屏蔽;
第二步:防火墙永久拦截该端口入站访问;
第三步:云服务器安全组删除端口放行规则。
示例:彻底屏蔽 FTP 服务
bash
运行
# 停止+强制屏蔽
systemctl mask --now vsftpd
# 防火墙拦截21端口
firewall-cmd --permanent --remove-port=21/tcp
firewall-cmd --reload
七、批量查看所有被屏蔽的服务
bash
运行
systemctl list-unit-files --type=service | grep masked
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快