站点图标 谷姐靓号网

以WebHorizon的$3.99/年小鸡为例说下OVZ小鸡如何瘦身-Mr.Qin

Rate this post

以WebHorizon的$3.99/年小鸡为例说下OVZ小鸡如何瘦身,这个帖子可能比较长,也没啥卵用(加钱能解决的问题其实都不是问题,主要就是个玩而已),想到哪儿写到哪儿,就当做个人笔记了,

所用的小鸡就是
https://hostloc.com/thread-933355-1-1.html
这篇帖子中的荷兰小鸡

小鸡购买地址看这里
https://hostloc.com/thread-947558-1-1.html
但是现在应该都没有货,也不知道啥时候补

1. 前置说明
首先你得用官方Debian 11模版重装个初始系统,这个帖子中所说的一切都是基于官方模版原始系统的, 都是基于Debian 11的,其他发行版或者已经你已经【魔改】过的Debian 11系统不保证可用性和安全性
刚装完的系统硬盘占用314M, 内存占用19M,如下图所示:

2. 从命令开始
$ vi /etc/apt/sources.list
deb http://deb.debian.org/debian/ bullseye main non-free contrib
deb http://deb.debian.org/debian/ bullseye-updates main contrib non-free
deb http://deb.debian.org/debian/ bullseye-backports main contrib non-free
deb http://security.debian.org/debian-security bullseye-security main contrib non-free
$ apt-get update
$ apt-get install openssh-server --no-install-recommends
$ apt-get install bash-completion
$ echo "set nocompatible" > ~/.vimrc
$ vi /etc/ssh/sshd_config
Port 22
AddressFamily inet
PermitRootLogin yes
PasswordAuthentication yes
KerberosAuthentication no
GSSAPIAuthentication no
TCPKeepAlive yes
ClientAliveInterval 30
ClientAliveCountMax 3
UseDNS no
$ systemctl restart sshd
上面这段做了如下几件事:
设置了官方软件源并开启了官方backports软件库
更新了系统软件包索引
openssh-server软件包设置为了手动安装模式防止卸载其它软件包时被关联删除
添加了Bash命令自动完成支持
vim设置为了不兼容老古董vi模式
修改了sshd服务的监听端口,监听IP地址类型,允许root用户登录,允许密码登录和连接保活等,,,

3. 继续命令
$ apt-get install libpam-systemd --no-install-recommends
$ apt-get install dbus --no-install-recommends
$ systemctl start dbus.service
上面这段做了如下几件事:
添加了Systemd的pam支持, 这样就可以针对user.slice资源组(登录用户终端和会话, 具体看我历史帖子)设置资源配额了
安装并启动了dbus服务(systemd的很多功能都依赖这个服务)

4. 继续命令
$ vi /etc/locale.gen
zh_CN.UTF-8 UTF-8
$ ln -s /etc/locale.alias /usr/share/locale/locale.alias
$ locale-gen
$ localectl set-locale LANG=zh_CN.UTF-8
$ timedatectl set-timezone Asia/Shanghai
上面这段做了如下几件事:
添加了中文UTF-8区域和语言支持并重新编译了系统locales库文件
设置了系统默认区域和语言为zh_CN.UTF-8
设置了系统时区为亚洲上海
吐槽一下官方模版做的太差竟然把locale.alias都删除了导致locale-gen命令报错!!!

5. 继续命令
$ apt-get purge resolvconf
$ vi /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
$ vi /etc/gai.conf
precedence ::ffff:0:0/96100
上面这段做了如下几件事:
卸载了resolvconf软件包, 手工配置系统DNS即可
设置系统外发连接优先使用IPv4地址

6. 继续命令
$ systemctl mask systemd-journald.service --now
$ systemctl mask systemd-journal-flush.service --now
$ apt-get purge rsyslog
$ systemctl disable cron.service --now
上面这段做了如下几件事:
禁用了systemd的journald服务, 反正也不怎么看journal日志, 都是直接看nginx和mysql自己的error log, 内存能省则省吧
移除了rsyslog日志组件
禁用了cron定时任务服务, 用的时候再启用就好了, 不用还开着浪费内存

7. 继续命令
$ systemctl list-timers
$ systemctl disable exim4-base.timer --now
$ systemctl disable logrotate.timer --now
$ systemctl disable man-db.timer --now
$ systemctl disable apt-daily.timer --now
$ systemctl disable apt-daily-upgrade.timer --now
$ systemctl disable e2scrub_all.timer --now
上面这段禁用了一堆没啥用的systemd定时器服务
注意: 不要禁用systemd-tmpfiles-clean.timer这个定时器!!!

8. 继续命令
$ apt-get install net-tools curl wget ca-certificates --no-install-recommends
$ vi /etc/security/limits.conf
root soft nofile 1048576
root hard nofile 1048576
$ apt-get install deborphan
上面这段做了一下事情
安装了常用软件包
设置了root用户的最大文件句柄数
安装了系统精简辅助软件包deborphan, 这个是个好东东, 感兴趣的自行搜索用法吧

警告:此贴内容仅适用于OpenVZ虚拟化下的Debian 11,其他虚拟化和发行版不适用!!!

热议
来自 2#楼 Mr.Qin 14小时前

9. 继续命令
$ apt-get purge installation-report
$ apt-get purge ispell ienglish-common iamerican ibritish dictionaries-common wamerican emacsen-common
$ apt-get purge bluetooth bluez eject
$ apt-get purge firmware-linux-free
$ rm -rf /usr/lib/firmware/
$ apt-get purge iw wpasupplicant wireless-tools wireless-regdb
$ apt-get purge discover discover-data pciutils pci.ids powertop
$ apt-get purge tasksel tasksel-data task-english task-laptop
$ apt-get purge avahi-autoipd
$ apt-get purge console-setup console-setup-linux kbd xkb-data keyboard-configuration
$ apt-get purge apparmor
$ rm -rf /etc/apparmor.d/
$ apt-get purge laptop-detect os-prober shared-mime-info xdg-user-dirs
$ apt-get purge debconf-i18n util-linux-locales
$ apt-get purge anacron busybox dmidecode
$ apt-get purge sudo parted nano lsof logrotate sysstat
$ apt-get purge qemu-guest-agent
$ apt-get purge libtext-wrapi18n-perl libtext-iconv-perl liblocale-gettext-perl libtext-charwidth-perl
$ apt-get purge exim4-base exim4-config exim4-daemon-light
$ apt-get purge manpages man-db ncurses-term initramfs-tools initramfs-tools-core
$ rm -rf /etc/initramfs-tools/
$ apt-get purge traceroute ncal psmisc sharutils udev
$ rm -rf /etc/udev/
$ apt-get purge bsdextrautils cpio fdisk groff-base htop info
$ apt-get purge gettext-base libapt-inst2.0 libdns-export1104 libevent-2.1-6 libfdisk1
$ apt-get purge iptables libapt-pkg5.0 libffi6 libfl2 libgdbm6 libgnutls-dane0 libhogweed4 libidn11 libip4tc0 libip6tc0 libiptc0 libisc-export1100 libjson-c3 liblognorm5 libnss-nis libnss-nisplus libpipeline1 libpopt0 libprocps7 libuchardet0
$ apt-get purge libestr0 libfastjson4 libgcc1 libnetfilter-conntrack3 libnettle6 libnftnl11 libunbound8
$ apt-get purge libevent-2.1-7 libreadline7
$ apt-get purge libnfnetlink0 zip unzip
$ apt-get purge bsdmainutils uuid-runtime
$ apt-get install gcc-9-base
$ apt-get purge gcc-8-base
$ apt-get purge install-info
$ apt-get purge wide-dhcpv6-client vim vim-common vim-runtime
$ apt-get install vim-tiny
$ apt-get purge libgpm2
$ rm -rf ~/essentials ~/.screenrc ~/.viminfo ~/.bash_history
$ apt-get autoremove --purge
以上命令有点儿多, 有些是我新加的,有些是我从以前记录的笔记中复制过来的,有些软件包在webhorion的初始系统中并没有
安装,我也懒得整理了,运行一下也没啥副作用,总之一句话就是删除没用的软件包和文件
吐槽一下官方Debian 11模版竟然是用Debian 10升级而来的,带了那么多大便10中的软件包(这些软件包已过时, 大便11中已有新版本且已经包含在模版中)
也不给清理一下,模版质量属实太差!!!

10. 继续命令
$ ls --color=never /usr/share/locale | grep -v -E '^locale.alias$' | xargs -I{} rm -rf '/usr/share/locale/'{}
$ echo -n > /usr/share/locale/locale.alias
$ ls --color=never /usr/share/i18n/locales | grep -v -E '^C$|^en_GB|^en_US|^i18n|^iso14651|^POSIX$|^translit_|^zh_CN' | xargs -I{} rm -rf '/usr/share/i18n/locales/'{}
$ locale-gen
以上命令是删除用不到的locale和i18n相关内容, 都删了吧,反正也没啥用

11. 继续命令
$ vi /etc/dpkg/dpkg.cfg.d/01_nodoc
path-exclude /usr/share/doc/*
path-exclude /usr/share/doc-base/*
path-exclude /usr/share/man/*
path-exclude /usr/share/groff/*
path-exclude /usr/share/info/*
path-exclude /usr/share/lintian/*
path-exclude /usr/share/linda/*
以上命令是告诉dpkg别给我安装文档,我小鸡硬盘小不需要软件包中的文档, 我用--help参数看简化版的命令说明就OK了

12. 继续命令
$ echo -n > /etc/motd
$ vi /etc/sysctl.conf
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.venet0.rp_filter = 0
net.ipv4.ip_local_port_range = 10000 65535
net.netfilter.nf_conntrack_max = 20000
net.netfilter.nf_conntrack_tcp_timeout_established = 86400
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 60
net.core.somaxconn = 8192
net.ipv4.tcp_fastopen = 0
net.ipv4.ip_forward = 0
$ sysctl -p
以上命令做了以下事情
去除了登录欢迎消息说明
设置了常用内核参数,OpenVZ支持的内核参数太少了

来自 3#楼 Mr.Qin 14小时前

13. 继续命令
$ echo 'APT::Install-Recommends "0";' > /etc/apt/apt.conf.d/01norecommend
$ printf "Package: *\nPin: release a=bullseye-backports\nPin-Priority: 800\n" > /etc/apt/preferences.d/backports
以上命令做了如下事情
我不想每次安装软件都加--no-install-recommends参数,烦死人了
优先安装backports软件库中的软件包, 谁不想提前尝鲜下一主版中的新软件包呢,,,

14. 继续命令
。。。算了,还是装一下iptables和ipset吧,前面步骤都卸载掉iptables了, ,,,万一用到了呢(限制80, 443端口仅Cloudflare的IP段可访问,防止证书泄漏导致源站IP泄漏)
$ apt-get install iptables ipset netfilter-persistent iptables-persistent ipset-persistent
$ update-alternatives --set iptables /usr/sbin/iptables-legacy
$ update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
$ systemctl enable netfilter-persistent --now
$ echo -n > /etc/iptables/rules.v4
$ echo -n > /etc/iptables/rules.v6
注意: OVZ小鸡只支持iptables-legacy和ip6tables-legacy,其他虚拟化小鸡不要执行那两条命令

15. 继续命令
$ rm -rf /tmp/*
$ rm -rf /tmp/.*
$ rm -rf /var/tmp/*
$ rm -rf /var/cache/*
$ rm -rf /var/mail/*
$ rm -rf /media/*
$ for tmp in $(find / -name '*.ucf-dist'); do echo $tmp; done
$ rm 垃圾文件
$ for tmp in $(find / -name '*~'); do echo $tmp; done
$ rm 垃圾文件
$ for tmp in $(find / -name '*-old'); do echo $tmp; done
$ rm 垃圾文件
删除点儿没用的缓存和垃圾文件吧,一会儿重启一下系统就好了

来自 4#楼 Mr.Qin 14小时前

16. 搞个脚本批量删除,时不时的手动运行一下或者添加到cron任务里面也行
$ vi .bashrc
alias ls='ls --time-style="+%F %T" --color="auto"'
export PATH=$PATH:~/bin
$ source ~/.bashrc
$ mkdir ~/bin
$ touch ~/bin/cleanup
$ chmod +x ~/bin/cleanup
#!/bin/bash
rm -rf /initrd.img*
rm -rf /vmlinuz*
rm -rf /lost+found/
rm -rf /boot/grub/locale/*
rm -rf /usr/lib/firmware/
rm -rf /var/lib/apt/lists/*
rm -rf /var/backups/*
apt-get clean
rm -rf /var/log/apt/*
rm -rf /var/log/sysstat/*
echo > /var/log/alternatives.log
echo > /var/log/auth.log
echo > /var/log/btmp
echo > /var/log/daemon.log
echo > /var/log/debug
echo > /var/log/dpkg.log
echo > /var/log/faillog
rm -rf /var/log/journal/
echo > /var/log/kern.log
echo > /var/log/lastlog
echo > /var/log/messages
rm -rf /var/log/private/*
rm -rf /var/log/runit/*
echo > /var/log/syslog
echo > /var/log/wtmp
echo > ~/.bash_history
rm -rf /usr/share/doc/*
rm -rf /usr/share/doc-base/*
rm -rf /usr/share/man/*
rm -rf /usr/share/groff/*
rm -rf /usr/share/info/*
rm -rf /usr/share/lintian/*
rm -rf /usr/share/linda/*
rm -rf /usr/share/common-licenses/*
rm -rf /usr/share/zsh/*
rm -rf /usr/share/icons/*
rm -rf /usr/share/pixmaps/*
rm -rf /usr/share/dict/*
rm -rf /usr/share/bug/*
rm -rf /usr/share/applications/*
rm -rf /usr/share/vim/vim82/doc/*
rm -rf /var/lib/dhcp/*
rm -rf /var/lib/dpkg/*-old
rm -rf /var/lib/ucf/cache/*
rm -rf /var/lib/ucf/hashfile.*
rm -rf /var/lib/ucf/registry.*
ls --color=never /usr/share/locale | grep -v -E '^locale.alias$' | xargs -I{} rm -rf '/usr/share/locale/'{}
echo -n > /usr/share/locale/locale.alias
ls --color=never /usr/share/i18n/locales | grep -v -E '^C$|^en_GB|^en_US|^i18n|^iso14651|^POSIX$|^translit_|^zh_CN' | xargs -I{} rm -rf '/usr/share/i18n/locales/'{}
这个脚本也是我从以前笔记中复制来的,很多都是针对KVM的,懒得整理了,也没啥副作用,删删更健康,

17. 继续命令
$ reboot
$ cleanup
$ apt-get update
$ apt-get upgrade
$ cleanup
刚才竟然忘记更新系统了,,,,
没事儿多运行运行cleanup命令清理下小鸡,

18. 继续命令
???????
不写了,就这样吧,笔记太多一时半会儿的也整理不清楚,
有兴趣的自己研究把OpenSSH换成Dropbear可以把开机内存降低到10M以下?好像是这样的,我的128M内存的NAT小鸡都是用的Dropbear,

这个小鸡弄完后是这样的,也还凑合就不继续折腾了

5楼 konololi 14小时前

折腾半天也就减少几百M还不如不折腾

6楼 Mr.Qin 14小时前

折腾半天也就减少几百M还不如不折腾

确实没啥卵用,就是闲的,

7楼 SayWhat13 8小时前

顶一个

8楼 LEB 8小时前

直接转成alpine更省

wget https://www.moerats.com/usr/shell/alpine.sh && bash alpine.sh

9楼 hcyme 8小时前

他家博客有写的,md现在关闭博客,三哥写的都比较简单明了,没有咖喱味

10楼 yem 8小时前

有没有通用的瘦身法

12楼 钱宗鑫 8小时前

这种玩的深度值得我学习!

13楼 up93 7小时前

现在只有7块的了么

14楼 Mr.Qin 7小时前

现在只有7块的了么

嗯,$7是正常价格,前段时间活动是$3.99,现在优惠码过期了,也不知道还补不补优惠码,

15楼 Mr.Qin 7小时前

有没有通用的瘦身法

还没有,OVZ和KVM不太一样,KVM内核相关的软件包不能卸载,其它步骤应该差不多,像那些manpage啊info文档和用不到的locale和i8n文件啥的都可以删,

17楼 扬帆 7小时前

楼主辛苦了。
这么长,这么折腾。

18楼 重案组曹达华 22分钟前

发现楼主是论坛里的WebHorizon专家玩儿的比较透彻了

19楼 boq 10分钟前

能写个一键包吗?

申明:本文内容由网友收集分享,仅供学习参考使用。如文中内容侵犯到您的利益,请在文章下方留言,本站会第一时间进行处理。

退出移动版