本文主要是介绍Ubuntu卸载snap,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Ubuntu卸载snap
文章目录
- Ubuntu卸载snap
- 1.打开终端,输入`snap list`,查看已安装的软件
- 2.停止snapd服务
- 3.依次移除snap安装的软件
- 4.完全清除 snapd
- 5.删除缓存目录(如有)
- 6.防止apt update自动安装snap
- 7.一键ubuntu卸载snap脚本
最近由于复现实验新装了Ubuntu20.04,发现后台中snapd进程占用高。snap是Canonical公司推出的一款软件管理工具,在20.04以及之后版本Ubuntu中都有预装,但因其难用,资源占用高被广为诟病,特搜索整理资料将其卸载。
1.打开终端,输入snap list
,查看已安装的软件
root@ubuntu2204:~# snap list
Name Version Rev Tracking Publisher Notes
core20 20230622 1974 latest/stable canonical✓ base
lxd 5.0.2-838e1b2 24322 5.0/stable/… canonical✓ -
snapd 2.59.5 19457 latest/stable canonical✓ snapd
2.停止snapd服务
root@ubuntu2204:~# systemctl disable snapd.service && systemctl disable snapd.socket && systemctl disable snapd.seeded.service
3.依次移除snap安装的软件
运行 sudo snap remove --purge lxd
等命令依次删除前面列表中的各个软件,需要注意的是在上述列表notes列表明是base的表示是其他软件的依赖项,需要放在最后面删除。比如上图中的core20
等等
root@ubuntu2204:~# vi remove_snap_package.sh
#!/bin/bash
sum=$(snap list | awk 'NR>=2{print $1}' | wc -l)
while [ $sum -ne 0 ];dofor p in $(snap list | awk 'NR>=2{print $1}') ;dosnap remove --purge $pdonesum=$(snap list | awk 'NR>=2{print $1}' | wc -l)
doneroot@ubuntu2204:~# bash remove_snap_package.sh
# 提示如下,表明已经删除干净:
No snaps are installed yet. Try 'snap install hello-world'.
4.完全清除 snapd
root@ubuntu2204:~# apt -y autoremove --purge snapd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:snapd* squashfs-tools* ubuntu-server-minimal*
0 upgraded, 0 newly installed, 3 to remove and 33 not upgraded.
After this operation, 103 MB disk space will be freed.
(Reading database ... 74127 files and directories currently installed.)
Removing ubuntu-server-minimal (1.481.1) ...
Removing snapd (2.58+22.04.1) ...
Warning: Stopping snapd.service, but it can still be activated by:snapd.socket
Removing squashfs-tools (1:4.5-3build1) ...
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for man-db (2.10.2-1) ...
(Reading database ... 74018 files and directories currently installed.)
Purging configuration files for snapd (2.58+22.04.1) ...
rmdir: failed to remove '/etc/systemd/system/snapd.mounts.target.wants': No such file or directory
Discarding preserved snap namespaces
Final directory cleanup
Removing extra snap-confine apparmor rules
Removing snapd cache
Removing snapd state
5.删除缓存目录(如有)
root@ubuntu2204:~# rm -rf ~/snap && sudo rm -rf /snap && rm -rf /var/snap && rm -rf /var/lib/snapd && rm -rf /var/cache/snapd
6.防止apt update自动安装snap
即使使用以上命令移除了 Snap 软件包,但如果没有关闭 apt 触发器,sudo apt update
命令会再一次将 Snap 安装回来。
要关闭它,需要在/etc/apt/preferences.d/
目录下创建一个 apt 设置文件nosnap.pref
来阻止 Snap 服务自动下载
运行命令vi /etc/apt/preferences.d/nosnap.pref
,打开编辑器,输入以下内容并保存文件
root@ubuntu2004:~# cat > /etc/apt/preferences.d/no-snapd.pref << EOF
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOF
文件保存后运行sudo apt update
即可。
root@ubuntu2204:~# apt update
7.一键ubuntu卸载snap脚本
Shell脚本源码地址:
Gitee:https://gitee.com/raymond9/shell
Github:https://github.com/raymond999999/shell
可以去上面的Gitee或Github代码仓库拉取脚本。
root@ubuntu2204:~# cat remove_snap.sh
#!/bin/bash
#
#***************************************************************************************************
#Author: Raymond
#QQ: 88563128
#Date: 2024-05-06
#FileName: remove_snap.sh
#MIRROR: raymond.blog.csdn.net
#Description: remove_snap for Ubuntu 20.04/22.04
#Copyright (C): 2024 All rights reserved
#***************************************************************************************************
COLOR="echo -e \\033[01;31m"
END='\033[0m'ubuntu_remove_snap(){dpkg -s snapd &> /dev/null || { ${COLOR}"snap已卸载!"${END};exit; }systemctl disable snapd.service && systemctl disable snapd.socket && systemctl disable snapd.seeded.servicesum=$(snap list | awk 'NR>=2{print $1}' | wc -l)while [ $sum -ne 0 ];dofor p in $(snap list | awk 'NR>=2{print $1}'); dosnap remove --purge $pdonesum=$(snap list | awk 'NR>=2{print $1}' | wc -l)doneapt -y autoremove --purge snapdrm -rf ~/snap && sudo rm -rf /snap && rm -rf /var/snap && rm -rf /var/lib/snapd && rm -rf /var/cache/snapdcat > /etc/apt/preferences.d/no-snapd.pref << EOF
Package: snapd
Pin: release a=*
Pin-Priority: -10
EOFapt update
}main(){ubuntu_remove_snap
}
这篇关于Ubuntu卸载snap的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!