标签 debian 下的文章

安装 resolvconf

apt install resolvconf

vi /etc/resolvconf/resolv.conf.d/head

在文件中增加
nameserver 8.8.8.8
nameserver 8.8.4.4

重启网络服务,使新 DNS 配置生效

systemctl restart networking

查看 dns 服务器

cat /etc/resolv.conf

Debian 8 带有开机自启动的 rc.local 文件。

Debian 9 和 10 则不带 rc.local 文件。

一、添加 rc.local.service

增加 systemd 服务

cd /etc/systemd/system
vi rc.local.service

键入以下内容,保存

[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target

二、新建 rc.local 文件

cd /etc
vi rc.local

键入以下内容,保存

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.



exit 0

三、添加权限并设置开机自启

重载新增加的服务

systemctl daemon-reload

设置 rc.local 的权限,并设置开机自启。

chmod +x /etc/rc.local
systemctl enable rc.local
systemctl start rc.local

通过设置正确的时区,可以保证cron、日志等服务的正确性;

更改时区

# Debian 10 / CentOS 7 更改时区为上海
timedatectl set-timezone Asia/Shanghai

检查当前时区

timedatectl是一个命令行实用程序,可以查看和更改系统的时间和日期。

它在所有基于systemd的现代Linux系统上都可用:

timedatectl

以下输出显示系统的时区设置为“ UTC”:

               Local time: Tue 2021-04-27 23:09:01 EDT
           Universal time: Wed 2021-04-28 03:09:01 UTC
                 RTC time: Wed 2021-04-28 03:09:02
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

通过符号链接/etc/localtime/usr/share/zoneinfo目录中的二进制时区标识符来配置系统时区。您还可以使用以下ls命令通过检查符号链接指向的路径来找到时区:

ls -l /etc/localtime

# 屏幕输出:
lrwxrwxrwx 1 root root 36 Apr 27 12:59 /etc/localtime -> /usr/share/zoneinfo/America/New_York

Debian 10 、 CentOS 7 更改时区

在更改时区之前,您需要找出要使用的时区的长名称。时区使用“地区/城市”格式。

要列出所有可用时区:

timedatectl list-timezones
...
America/Monterrey
America/Montevideo
America/Montserrat
America/Nassau
America/New_York
...

确定哪个时区适合您的位置后,以root或具有sudo特权的用户身份运行以下命令:

timedatectl set-timezone your_time_zone

例如,要将系统的时区更改为Asia/Shanghai,可以运行:

timedatectl set-timezone Asia/Shanghai

通过使用以下timedatectl命令检查当前系统的时区来验证更改:

timedatectl

# 屏幕输出:
               Local time: Wed 2021-04-28 11:12:26 CST
           Universal time: Wed 2021-04-28 03:12:26 UTC
                 RTC time: Wed 2021-04-28 03:12:27
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

通过创建符号链接来更改时区

如果您运行的是Debian的旧版本,并且timedatectl系统上没有该命令,则可以通过将/etc/localtime文件符号链接到目录中的时区二进制文件来更改时区/usr/share/zoneinfo

确定要配置的时区,并创建一个符号链接:

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

通过列出/etc/localtime文件或发出timedatectlor date命令来验证更改:

date

# 屏幕输出:
Wed 28 Apr 2021 11:13:23 AM CST

查看 Debian 源

cat /etc/apt/sources.list

修改 Debian 源

vi /etc/apt/sources.list

Debian 11 官方源

deb http://deb.debian.org/debian bullseye main contrib non-free
deb-src http://deb.debian.org/debian bullseye main contrib non-free

deb http://deb.debian.org/debian-security/ bullseye-security main contrib non-free
deb-src http://deb.debian.org/debian-security/ bullseye-security main contrib non-free

deb http://deb.debian.org/debian bullseye-updates main contrib non-free
deb-src http://deb.debian.org/debian bullseye-updates main contrib non-free

Debian 10 官方源

# 官方源
deb http://deb.debian.org/debian/ buster main
deb-src http://deb.debian.org/debian/ buster main

deb http://security.debian.org/debian-security buster/updates main
deb-src http://security.debian.org/debian-security buster/updates main

deb http://deb.debian.org/debian/ buster-updates main
deb-src http://deb.debian.org/debian/ buster-updates main
# 163源
deb http://mirrors.163.com/debian/ buster main non-free contrib
deb http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib

deb-src http://mirrors.163.com/debian/ buster main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.163.com/debian-security/ buster/updates main non-free contrib
# 清华源
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security/ buster/updates main contrib non-free

deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-updates main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ buster-backports main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security/ buster/updates main contrib non-free

Debian 官方全球镜像站列表地址:https://www.debian.org/mirror/list

更新apt索引:

apt update --allow-releaseinfo-change

# 升级包
apt upgrade

Debian 10 安装 docker

通过 HTTPS 添加新存储库所需的软件包

# 更新软件包
apt update -y
# 通过 HTTPS 添加新存储库所需的软件包
apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common

使用以下 curl 命令导入存储库的 GPG 密钥

curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -

将稳定的 Docker APT 存储库添加到系统的软件存储库列表中

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
# 再次更新软件包
apt update -y

# 安装 docker
apt install docker-ce

安装完成后, Docker 服务会自动启动,并默认开机自启。

# 查看docker服务的状态
systemctl status docker

# 查看docker开机自启状态
systemctl is-enabled docker

解决 docker 与 ufw 的冲突

  • ufw 防火墙默认本地路由转发默认为关,需要修改为开;

    vi /etc/default/ufw
    
    # 将DEFAULT_FORWARD_POLICY修改为
    DEFAULT_FORWARD_POLICY="ACCEPT"
    
    # ufw 防火墙规则重载
    ufw reload
  • docker默认直接修改iptables规则,需要关闭,从而使 UFW 防火墙规则生效;

    vi /etc/default/docker
    
    # 修改文件配置项DOCKER_OPTS,该项默认为注释状态
    DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 -iptables=false"
    
    # 重启 docker
    systemctl restart docker

    部署一个容器后,它将不再改变iptables并且严格遵守UFW防火墙的设置规则。