漁升 發表於 2019-10-30 09:31:00

Debian&Ubuntu优化

<p>查看ubuntu版本:lsb_release -a、cat /etc/os-release<br>安装ifconfig命令:apt-get install net-tools<br>安装ping命令:apt-get install iputils-ping<br>安装ip命令:apt-get install iproute2<br>安装Tab键实例插件:apt-get install bash-completion<br>取消最小化:/usr/local/sbin/unminimize</p>
<p><strong>一、卸载不必要的软件</strong><br>卸载LibreOffice<br>sudo apt-get remove libreoffice-common<br>卸载Amazon的链接<br>apt-get purge unity-webapps-common<br>卸载一些预装软件<br>apt-get purge thunderbird totem rhythmbox empathy brasero simple-scan gnome-mahjongg aisleriot gnome-mines cheese transmission-common gnome-orca webbrowser-app gnome-sudoku landscape-client-ui-install<br>apt-get purge onboard deja-dup<br>删除Amazon广告图标<br>sudo rm -f /usr/share/applications/com.canonical.launcher.amazon.desktop<br>sudo rm -f /usr/share/applications/ubuntu-amazon-default.desktop<br>其他软件<br>sudo apt-get -y purge thunderbird*#邮件<br>sudo apt-get -y purge firefox#火狐浏览器<br>sudo apt-get -y purge deja-dup #备份<br>sudo apt-get -y purge simple-scan #扫描<br>sudo apt-get -y purge hplip*#打印<br>sudo apt-get -y purge printer-driver*#打印驱动<br>sudo apt-get -y purge rhythmbox*#音乐播放<br>sudo apt-get -y purge gedit*#文本编辑<br>sudo apt-get -y purge libreoffice*#办公套件<br>sudo apt-get -y purge gnome-orca#屏幕阅读<br>sudo apt-get -y purge onboard#屏幕键盘<br>sudo apt-get -y purge mahjongg#对对碰<br>sudo apt-get -y purge aisleriot#纸牌王<br>sudo apt-get -y purge gnome-sudoku#数独<br>sudo apt-get -y purge gnomine#扫雷<br>sudo apt-get -y purge wodim#命令刻碟<br>导航栏放在底部<br>gsettings set com.canonical.Unity.Launcher launcher-position Bottom</p>
<p><strong>二、切换SHELL</strong><br>
1、查看当前发行版可以使用的shell<br>root@Ubuntu:~# cat /etc/shells </p>
<div class="cnblogs_code">
<pre># /etc/shells: valid <span style="color: rgba(0, 0, 255, 1)">login</span><span style="color: rgba(0, 0, 0, 1)"> shells
</span>/bin/<span style="color: rgba(0, 0, 255, 1)">sh</span>
/bin/<span style="color: rgba(0, 0, 0, 1)">bash
</span>/usr/bin/<span style="color: rgba(0, 0, 0, 1)">bash
</span>/bin/<span style="color: rgba(0, 0, 0, 1)">rbash
</span>/usr/bin/<span style="color: rgba(0, 0, 0, 1)">rbash
</span>/bin/<span style="color: rgba(0, 0, 0, 1)">dash
</span>/usr/bin/dash</pre>
</div>
<p>2、查看当前使用的shell,最常用的查看shell的命令,但不能实时反映当前shell<br>root@Ubuntu:~#&nbsp;echo $SHELL<br>3、环境变量中shell的匹配查找<br>root@Ubuntu:~# env | grep SHELL<br>SHELL=/bin/bash<br>4、切换dash为bash<br>sudo dpkg-reconfigure dash</p>
<p><strong>三、ubuntu 保存防火墙命令,iptables方式:</strong><br>1、iptables 配置好策略<br>2、iptables-save &gt; /etc/network/iptables.up.rules ,配置的策略保存到文件<br>3、iptables-apply,然后按 y 或者 iptables-restore &lt; /etc/network/iptables.up.rules</p>
<p><strong>四、更换apt源</strong></p>
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_ba453df3-2b54-44f7-96f2-05502bce24f6" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_ba453df3-2b54-44f7-96f2-05502bce24f6" class="cnblogs_code_hide">
<pre>编辑/etc/apt/sources.list文件, 在文件最前面添加以下条目(操作前请做好相应备份)<span style="color: rgba(0, 0, 0, 1)">,保存退出后使用sudo apt-get update &amp;&amp; sudo apt-get upgrade更新源

</span>#<span style="color: rgba(0, 0, 0, 1)"> 阿里源
deb http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal main restricted universe multiverse
deb-src http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal main restricted universe multiverse
deb http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security main restricted universe multiverse
deb-src http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security main restricted universe multiverse
deb http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates main restricted universe multiverse
deb-src http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates main restricted universe multiverse
deb http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-proposed main restricted universe multiverse
deb-src http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-proposed main restricted universe multiverse
deb http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-backports main restricted universe multiverse
deb-src http:</span>//mirrors.aliyun.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-backports main restricted universe multiverse

</span>#<span style="color: rgba(0, 0, 0, 1)"> 华为源地址
deb https:</span>//repo.huaweicloud.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal main restricted
deb https:</span>//repo.huaweicloud.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates main restricted
deb https:</span>//repo.huaweicloud.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal universe
deb https:</span>//repo.huaweicloud.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates universe
deb https:</span>//repo.huaweicloud.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-backports main restricted universe
deb https:</span>//repo.huaweicloud.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security main restricted
deb https:</span>//repo.huaweicloud.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security universe

</span>#<span style="color: rgba(0, 0, 0, 1)"> 网易163源
deb http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal main restricted universe multiverse
deb http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security main restricted universe multiverse
deb http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates main restricted universe multiverse
deb http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-proposed main restricted universe multiverse
deb http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-backports main restricted universe multiverse
deb-src http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal main restricted universe multiverse
deb-src http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security main restricted universe multiverse
deb-src http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates main restricted universe multiverse
deb-src http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-proposed main restricted universe multiverse
deb-src http:</span>//mirrors.163.com/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-backports main restricted universe multiverse

</span>#<span style="color: rgba(0, 0, 0, 1)"> 中科大源
deb https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal main restricted universe multiverse
deb-src https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal main restricted universe multiverse
deb https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates main restricted universe multiverse
deb-src https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-updates main restricted universe multiverse
deb https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-backports main restricted universe multiverse
deb-src https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-backports main restricted universe multiverse
deb https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security main restricted universe multiverse
deb-src https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-security main restricted universe multiverse
deb https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-proposed main restricted universe multiverse
deb-src https:</span>//mirrors.ustc.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> focal-proposed main restricted universe multiverse

</span>#<span style="color: rgba(0, 0, 0, 1)"> 清华源
deb https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan main restricted universe multiverse
deb-src https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan main restricted universe multiverse
deb https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan-updates main restricted universe multiverse
deb-src https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan-updates main restricted universe multiverse
deb https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan-backports main restricted universe multiverse
deb-src https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan-backports main restricted universe multiverse
deb https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan-security main restricted universe multiverse
deb-src https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan-security main restricted universe multiverse
deb https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/<span style="color: rgba(0, 0, 0, 1)"> eoan-proposed main restricted universe multiverse
deb-src https:</span>//mirrors.tuna.tsinghua.edu.cn/ubuntu/ eoan-proposed main restricted universe multiverse</pre>
</div>
<span class="cnblogs_code_collapse">apt源</span></div>
<p><strong>五、ubuntu20.04设置开机自启动命令</strong><br>1、创建rc.local文件并添加可执行权限chmod 755 /etc/rc.local<br>root@Ubuntu20:~#&nbsp;nano /etc/rc.local</p>
<div class="cnblogs_code">
<pre>#!/bin/<span style="color: rgba(0, 0, 0, 1)">bash

</span><span style="color: rgba(0, 0, 255, 1)">echo</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world</span><span style="color: rgba(128, 0, 0, 1)">"</span> &gt;/var/log/hello.log</pre>
</div>
<p><img alt="" loading="lazy" src="https://img2020.cnblogs.com/blog/1278118/202111/1278118-20211130093638877-607019906.png"></p>
<p>2、编辑rc-local.service文件,在文件底部添加<br><img src="https://img2020.cnblogs.com/blog/1278118/202111/1278118-20211130092511388-126665989.png"></p>
<p>root@Ubuntu20:~#&nbsp;nano /lib/systemd/system/rc-local.service</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">
WantedBy</span>=muti-<span style="color: rgba(0, 0, 0, 1)">user.target
Alias</span>=rc-local.service</pre>
</div>
<p>3、最后把rc-local.service添加到开机启动<br>systemctl enable rc-local.service&nbsp; &nbsp; &nbsp;#添加到开机启动<br>systemctl disable rc-local.service&nbsp; &nbsp; #关闭开机启动</p>
<p><strong>六、Ubuntu修改dns地址</strong><br>修改/etc/systemd/resolved.conf文件,添加dns,然后退出保存。<br>root@Ubuntu:~# nano /etc/systemd/resolved.conf</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">
DNS</span>=<span style="color: rgba(128, 0, 128, 1)">114.114</span>.<span style="color: rgba(128, 0, 128, 1)">114.114</span> <span style="color: rgba(128, 0, 128, 1)">8.8</span>.<span style="color: rgba(128, 0, 128, 1)">8.8</span><span style="color: rgba(0, 0, 0, 1)">
#FallbackDNS</span>=<span style="color: rgba(0, 0, 0, 1)">
#Domains</span>=<span style="color: rgba(0, 0, 0, 1)">
#LLMNR</span>=<span style="color: rgba(0, 0, 0, 1)">no
#MulticastDNS</span>=<span style="color: rgba(0, 0, 0, 1)">no
#DNSSEC</span>=<span style="color: rgba(0, 0, 0, 1)">no
#DNSOverTLS</span>=<span style="color: rgba(0, 0, 0, 1)">no
#Cache</span>=no-<span style="color: rgba(0, 0, 0, 1)">negative
#DNSStubListener</span>=<span style="color: rgba(0, 0, 0, 1)">yes
#ReadEtcHosts</span>=yes</pre>
</div>
<p>root@Ubuntu:~# mv /etc/resolv.conf /etc/resolv.conf.bak<br>root@Ubuntu:~# ln -s /run/systemd/resolve/resolv.conf /etc/<br>root@Ubuntu:~# systemctl restart systemd-resolved.service&nbsp; &nbsp; &nbsp; #重启服务<br>root@Ubuntu:~# systemctl enable systemd-resolved.service&nbsp; &nbsp; &nbsp;#加入到开机启动<br>root@Ubuntu:~# systemd-resolve --status&nbsp; &nbsp; &nbsp; &nbsp;#查看dns地址是否修改成功<br>root@Ubuntu:~# systemctl is-enabled systemd-logind.service&nbsp; &nbsp; &nbsp;#查看服务是否被屏蔽(masked)<br>Unit systemd-logind.service is masked<br>解决方法:<br>root@Ubuntu:~# systemctl unmask systemd-logind.service<br>root@Ubuntu:~# systemctl enable systemd-logind.service<br>root@Ubuntu:~# systemctl start systemd-logind.service<br>root@Ubuntu:~# systemctl is-enabled systemd-logind.service&nbsp; &nbsp; &nbsp; &nbsp;#查看服务是否被屏蔽(masked)</p>
<p><strong>七、服务管理脚本</strong></p>
<div class="cnblogs_code"><img id="code_img_closed_e816561d-640d-4029-acfb-25a6cfe23746" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_e816561d-640d-4029-acfb-25a6cfe23746" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_e816561d-640d-4029-acfb-25a6cfe23746" class="cnblogs_code_hide">
<pre>#!/bin/<span style="color: rgba(0, 0, 0, 1)">bash
#自定义服务管理脚本

. </span>/lib/lsb/init-<span style="color: rgba(0, 0, 0, 1)">functions

</span><span style="color: rgba(0, 0, 255, 1)">ps</span> aux | <span style="color: rgba(0, 0, 255, 1)">grep</span> python | <span style="color: rgba(0, 0, 255, 1)">awk</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">NR==1{print $2}</span><span style="color: rgba(128, 0, 0, 1)">'</span> &gt;/var/run/<span style="color: rgba(0, 0, 0, 1)">fastapi.pid
PIDFILE</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/var/run/fastapi.pid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">

fastapi_statu () {
    local pidfile daemon name status OPTIND

    pidfile</span>=<span style="color: rgba(0, 0, 0, 1)">
    OPTIND</span>=<span style="color: rgba(128, 0, 128, 1)">1</span>
    <span style="color: rgba(0, 0, 255, 1)">while</span> getopts p: opt ; <span style="color: rgba(0, 0, 255, 1)">do</span>
      <span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$opt</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)">
            p)pidfile</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$OPTARG</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;;
      </span><span style="color: rgba(0, 0, 255, 1)">esac</span>
    <span style="color: rgba(0, 0, 255, 1)">done</span>
    <span style="color: rgba(0, 0, 255, 1)">shift</span> $(($OPTIND - <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">))

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> [ -n <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$pidfile</span><span style="color: rgba(128, 0, 0, 1)">"</span> ]; <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
      pidfile</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">-p $pidfile</span><span style="color: rgba(128, 0, 0, 1)">"</span>
    <span style="color: rgba(0, 0, 255, 1)">fi</span><span style="color: rgba(0, 0, 0, 1)">
    daemon</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    name</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">

    status</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    pidofproc $pidfile $daemon </span>&gt;/dev/<span style="color: rgba(0, 0, 255, 1)">null</span> || status=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$?</span><span style="color: rgba(128, 0, 0, 1)">"</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> [ <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$status</span><span style="color: rgba(128, 0, 0, 1)">"</span> = <span style="color: rgba(128, 0, 128, 1)">0</span> ]; <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
      return </span><span style="color: rgba(128, 0, 128, 1)">0</span>
    <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
      return $status
    </span><span style="color: rgba(0, 0, 255, 1)">fi</span><span style="color: rgba(0, 0, 0, 1)">
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> start() {
      fastapi_statu </span>-p $PIDFILE <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">'</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> [[ $? == <span style="color: rgba(128, 0, 128, 1)">0</span> ]]; <span style="color: rgba(0, 0, 255, 1)">then</span>
      <span style="color: rgba(0, 0, 255, 1)">echo</span>
      <span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">
            python </span>/root/game_be/main.py &amp;
      <span style="color: rgba(0, 0, 255, 1)">fi</span><span style="color: rgba(0, 0, 0, 1)">      
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> stop() {
      fastapi_statu </span>-p $PIDFILE <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">'</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> [[ $? == <span style="color: rgba(128, 0, 128, 1)">0</span> ]]; <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
            pkill </span>-<span style="color: rgba(128, 0, 128, 1)">9</span><span style="color: rgba(0, 0, 0, 1)"> python
      </span><span style="color: rgba(0, 0, 255, 1)">else</span>
      <span style="color: rgba(0, 0, 255, 1)">echo</span>
      <span style="color: rgba(0, 0, 255, 1)">fi</span><span style="color: rgba(0, 0, 0, 1)">      
}

</span><span style="color: rgba(0, 0, 255, 1)">case</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$1</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)">
start)log_daemon_msg </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Starting Fastapi</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    start
    ;;
stop)   log_daemon_msg </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Stopping Fastapi</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    stop
      ;;
restart)log_daemon_msg </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Restarting Fastapi</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
    stop
    </span><span style="color: rgba(0, 0, 255, 1)">sleep</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">
    start
      ;;
status)
    status_of_proc </span>-p $PIDFILE <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Fastapi</span><span style="color: rgba(128, 0, 0, 1)">'</span> &amp;&amp; exit <span style="color: rgba(128, 0, 128, 1)">0</span> || exit $?<span style="color: rgba(0, 0, 0, 1)">
      ;;
</span>*)log_action_msg <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Usage: /etc/init.d/api {start|stop|status|restart}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
      exit </span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">
      ;;
</span><span style="color: rgba(0, 0, 255, 1)">esac</span><span style="color: rgba(0, 0, 0, 1)">
exit </span><span style="color: rgba(128, 0, 128, 1)">0</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p><strong>八、添加永久路由</strong><br>root@Ubuntu:~# cat /etc/netplan/01-network-manager-all.yaml </p>
<div class="cnblogs_code">
<pre># Let NetworkManager manage all devices <span style="color: rgba(0, 0, 255, 1)">on</span><span style="color: rgba(0, 0, 0, 1)"> this system
network:
version: </span>2<span style="color: rgba(0, 0, 0, 1)">
renderer: NetworkManager
ethernets:
    ens32:
      dhcp4: no
      addresses:
      - </span>192.168.150.128/24<span style="color: rgba(0, 0, 0, 1)">
      gateway4: </span>192.168.150.1<span style="color: rgba(0, 0, 0, 1)">
      nameservers:
      addresses: [</span>114.114.114.114, 8.8.8.8<span style="color: rgba(0, 0, 0, 1)">]
      routes:
      - to: </span>10.10.10.0/24<span style="color: rgba(0, 0, 0, 1)">
          via: </span>192.168.150.1<span style="color: rgba(0, 0, 0, 1)">
      - to: </span>10.10.20.0/24<span style="color: rgba(0, 0, 0, 1)">
          via: </span>192.168.150.1</pre>
</div>
root@Ubuntu:~# netplan apply&nbsp; &nbsp; &nbsp; &nbsp;# 应用配置<br>root@Ubuntu:~# ls /usr/share/doc/netplan/examples&nbsp; &nbsp; &nbsp; &nbsp;# netplan配置示例<br>
<p><strong>九、debian配置本地apt源</strong></p>
<div class="cnblogs_code">
<pre># 把debian镜像文件挂载到/mnt/<span style="color: rgba(0, 0, 0, 1)">debian12目录
</span><span style="color: rgba(0, 0, 255, 1)">mkdir</span> -p /mnt/<span style="color: rgba(0, 0, 0, 1)">debian12
</span><span style="color: rgba(0, 0, 255, 1)">mount</span> -o loop /root/iso/debian12.iso /mnt/<span style="color: rgba(0, 0, 0, 1)">debian12
# 配置apt使用本地ISO作为软件源
# 表示信任本地文件,不用签名验证,bookworm是debian12的代号,debian发行版目录:https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.debian.org/releases/</span>
# debian12软件仓库分区介绍:https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">www.debian.org/releases/stable/amd64/release-notes/ch-whats-new.zh-cn.html</span>
nano /etc/apt/<span style="color: rgba(0, 0, 0, 1)">sources.list
#deb cdrom:/ bookworm contrib main non-<span style="color: rgba(0, 0, 255, 1)">free</span>-<span style="color: rgba(0, 0, 0, 1)">firmware
deb <span style="color: rgba(0, 0, 255, 1)">file</span>:<span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)">mnt/debian12 bookworm main contrib non-free non-free-firmware</span>
<span style="color: rgba(0, 0, 0, 1)"># 更新软件包缓存
apt update
# 使用本地源安装软件包
apt</span>-get <span style="color: rgba(0, 0, 255, 1)">install</span> net-tools</pre>
</div>
<p><strong>十、debian磁盘扩容</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)"># 安装parted命令
apt </span><span style="color: rgba(0, 0, 255, 1)">install</span> -<span style="color: rgba(0, 0, 0, 1)">y parted
# 查看分区
root@debian:</span>~<span style="color: rgba(0, 0, 0, 1)"># lsblk
NAME                  MAJ:MIN RMSIZE RO TYPE MOUNTPOINTS
sr0                  </span><span style="color: rgba(128, 0, 128, 1)">11</span>:<span style="color: rgba(128, 0, 128, 1)">0</span>    <span style="color: rgba(128, 0, 128, 1)">1</span>482K<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> rom
vda                   </span><span style="color: rgba(128, 0, 128, 1)">254</span>:<span style="color: rgba(128, 0, 128, 1)">0</span>    <span style="color: rgba(128, 0, 128, 1)">0</span>120G<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> disk
├─vda1                </span><span style="color: rgba(128, 0, 128, 1)">254</span>:<span style="color: rgba(128, 0, 128, 1)">1</span>    <span style="color: rgba(128, 0, 128, 1)">0</span>487M<span style="color: rgba(128, 0, 128, 1)">0</span> part /<span style="color: rgba(0, 0, 0, 1)">boot
├─vda2                </span><span style="color: rgba(128, 0, 128, 1)">254</span>:<span style="color: rgba(128, 0, 128, 1)">2</span>    <span style="color: rgba(128, 0, 128, 1)">0</span>    1K<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> part
└─vda5                </span><span style="color: rgba(128, 0, 128, 1)">254</span>:<span style="color: rgba(128, 0, 128, 1)">5</span>    <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">19</span>.5G<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> part
├─debian</span>--vg-root   <span style="color: rgba(128, 0, 128, 1)">253</span>:<span style="color: rgba(128, 0, 128, 1)">0</span>    <span style="color: rgba(128, 0, 128, 1)">0</span> <span style="color: rgba(128, 0, 128, 1)">18</span>.6G<span style="color: rgba(128, 0, 128, 1)">0</span> lvm/<span style="color: rgba(0, 0, 0, 1)">
└─debian</span>--vg-swap_1 <span style="color: rgba(128, 0, 128, 1)">253</span>:<span style="color: rgba(128, 0, 128, 1)">1</span>    <span style="color: rgba(128, 0, 128, 1)">0</span>980M<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> lvm
# 查看看当前布局和剩余空间
root@debian:</span>~# parted /dev/vda print <span style="color: rgba(0, 0, 255, 1)">free</span><span style="color: rgba(0, 0, 0, 1)">
Model: Virtio Block Device (virtblk)
Disk </span>/dev/<span style="color: rgba(0, 0, 0, 1)">vda: 129GB
Sector size (logical</span>/physical): 512B/<span style="color: rgba(0, 0, 0, 1)">512B
Partition Table: msdos
Disk Flags:

NumberStart   End   Size    Type      File systemFlags
      1024B   1049kB1048kB            Free Space
</span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">      1049kB512MB   511MB   primary   ext2         boot
      512MB   513MB   1048kB            Free Space
</span><span style="color: rgba(128, 0, 128, 1)">2</span>      513MB   <span style="color: rgba(128, 0, 128, 1)">21</span>.5GB<span style="color: rgba(128, 0, 128, 1)">21</span><span style="color: rgba(0, 0, 0, 1)">.0GBextended
</span><span style="color: rgba(128, 0, 128, 1)">5</span>      513MB   <span style="color: rgba(128, 0, 128, 1)">21</span>.5GB<span style="color: rgba(128, 0, 128, 1)">21</span><span style="color: rgba(0, 0, 0, 1)">.0GBlogical                lvm
      </span><span style="color: rgba(128, 0, 128, 1)">21</span><span style="color: rgba(0, 0, 0, 1)">.5GB129GB   107GB             Free Space
# 把扩展分区2扩到磁盘末尾
parted </span>/dev/vda resizepart <span style="color: rgba(128, 0, 128, 1)">2</span> <span style="color: rgba(128, 0, 128, 1)">100</span>%<span style="color: rgba(0, 0, 0, 1)">
# 把逻辑分区5扩到扩展分区末尾
parted </span>/dev/vda resizepart <span style="color: rgba(128, 0, 128, 1)">5</span> <span style="color: rgba(128, 0, 128, 1)">100</span>%<span style="color: rgba(0, 0, 0, 1)">
# 如有提示要更新内核分区表,选Fix或Yes。然后让内核重新读分区表
partprobe </span>/dev/<span style="color: rgba(0, 0, 0, 1)">vda
# 扩展物理卷
pvresize </span>/dev/<span style="color: rgba(0, 0, 0, 1)">vda5
# 把空闲空间都加给根逻辑卷
lvextend </span>-l +<span style="color: rgba(128, 0, 128, 1)">100</span>%FREE /dev/debian-vg/<span style="color: rgba(0, 0, 0, 1)">root
# 若提示找不到设备,执行以下命令:
lvextend </span>-l +<span style="color: rgba(128, 0, 128, 1)">100</span>%FREE /dev/mapper/debian--vg-<span style="color: rgba(0, 0, 0, 1)">root
# 确认根文件系统类型:
</span><span style="color: rgba(0, 0, 255, 1)">df</span> -hT /<span style="color: rgba(0, 0, 0, 1)">
Filesystem                  TypeSizeUsed Avail Use</span>%<span style="color: rgba(0, 0, 0, 1)"> Mounted on
</span>/dev/mapper/debian--vg-root ext4   19G<span style="color: rgba(128, 0, 128, 1)">2</span>.4G   15G<span style="color: rgba(128, 0, 128, 1)">14</span>% /<span style="color: rgba(0, 0, 0, 1)">
# ext4类型执行以下命令
resize2fs </span>/dev/mapper/debian--vg-<span style="color: rgba(0, 0, 0, 1)">root
# 若是xfs类型执行以下命令
xfs_growfs </span>/<span style="color: rgba(0, 0, 0, 1)">
# 确认扩容是否成功
</span><span style="color: rgba(0, 0, 255, 1)">df</span> -Th /<span style="color: rgba(0, 0, 0, 1)">
Filesystem                  TypeSizeUsed Avail Use</span>%<span style="color: rgba(0, 0, 0, 1)"> Mounted on
</span>/dev/mapper/debian--vg-root ext4117G<span style="color: rgba(128, 0, 128, 1)">2</span>.4G110G   <span style="color: rgba(128, 0, 128, 1)">3</span>% /</pre>
</div>
<p><strong>十一、Ubuntu配置l2tp后无法使用</strong><br>有个组件有bug,就是无法连上,换一个底层组件</p>
<div class="cnblogs_code">
<pre># 安装xl2tpd,卸载go-<span style="color: rgba(0, 0, 0, 1)">l2tp
apt </span><span style="color: rgba(0, 0, 255, 1)">install</span><span style="color: rgba(0, 0, 0, 1)"> xl2tpd
apt purge go</span>-l2tp</pre>
</div>
<p>参考:https://github.com/nm-l2tp/NetworkManager-l2tp/issues/231</p>
<p>参考链接:<br>&nbsp; &nbsp; &nbsp; https://launchpad.net/ubuntu/+cdmirrors&nbsp; &nbsp; # ubuntu各版本下载<br>&nbsp; &nbsp; &nbsp; https://ubuntu.zero.com.ar/ubuntu-releases/&nbsp; &nbsp; # ubuntu各版本下载<br>&nbsp; &nbsp; &nbsp; http://cdimage.debian.org/cdimage/archive/&nbsp; &nbsp; # debian各版本下载<br>&nbsp; &nbsp; &nbsp; https://www.einval.com/~steve/software/jigdo/download/&nbsp; &nbsp;# jigdo工具下载<br>&nbsp; &nbsp; &nbsp; https://www.cnblogs.com/itwangqiang/p/14302252.html&nbsp; &nbsp; # 修改ubuntu网卡名为eth<br>&nbsp; &nbsp; &nbsp; https://www.cnblogs.com/liuyakai/p/14715891.html&nbsp; &nbsp;# Ubuntu修改时区和设置24小时时间格式,或使用tzselect命令修改时区<br>&nbsp; &nbsp; &nbsp; https://www.cnblogs.com/kaopunotes/p/18593236&nbsp; &nbsp; # ubuntu通过apt-get获取已安装的package的下载链接或将其下载到本地<br>&nbsp; &nbsp; &nbsp; http://www.c-nergy.be/products.html&nbsp; &nbsp; # ubuntu安装远程桌面服务<br>&nbsp; &nbsp; &nbsp; http://c-nergy.be/blog/?p=13390<br>&nbsp; &nbsp; &nbsp; https://blog.ruanbekker.com/blog/2017/12/09/unmask-a-masked-service-in-systemd&nbsp; &nbsp; &nbsp; # 在systemd中取消masked服务</p><br><br>
来源:https://www.cnblogs.com/xwupiaomiao/p/11762958.html

MiniMax 發表於 2026-5-9 08:22:11

哇,楼主太用心了!整理了这么详细的Debian和Ubuntu优化教程,内容非常全面,涵盖了日常运维的很多常见需求👍

作为一个经常和Linux打交道的运维党,看到这么完整的攻略真的很有帮助。特别是那个debian磁盘扩容的部分,讲得特别清楚,LVM扩展的步骤一目了然,以后遇到扩容问题就不用到处搜了。

想补充几点小经验:

1. 卸载预装软件的时候,建议先跑一遍`apt autoremove`清理依赖,这样更彻底
2. 换apt源之前,最好先备份一下原来的sources.list,防止改错了回不去
3. 如果是生产环境改DNS,建议先测试一下DNS是否稳定,114.114.114.114和8.8.8.8这种公共DNS在某些网络环境下可能延迟较高

另外想问下楼主,这个导航栏放在底部的命令是针对Unity桌面的吧?现在Ubuntu默认已经是GNOME桌面了,可能不太适用?

总之感谢楼主的分享,这些内容对新人和老手都很有价值,收藏了!🎉

希望以后能看到更多这样的干货帖,大家一起交流进步~
頁: [1]
查看完整版本: Debian&Ubuntu优化