Skip to content

Ubuntu配置

一、特殊版本

Ubuntu22的系统,如果需要设置静态ip,需要新增一个00-network-config.yaml的文件,修改这个文件的网络配置,50-cloud-init.yaml (可能不同的机器前面的序号不同, 比如我的是50)重启后会恢复默认值

cp /etc/netplan/50-cloud-init.yaml /etc/netplan/00-network-config.yaml

二、修改ssh安全登陆

  1. 打开配置文件

    nano /etc/ssh/sshd_config
  2. 修改配置文件

打开下面四个参数的注释,或者直接复制这四个参数进去

LoginGraceTime 2m          #登陆时间
PermitRootLogin yes        #允许root登录
StrictModes yes            #严格模式
PasswordAuthentication yes
UseDNS no
  1. 重启服务

    systemctl restart ssh && systemctl restart sshd

三、设置源

设置清华源,如果需要设置别的源自行查找

#备份原始文件
cp /etc/apt/sources.list /etc/apt/sources.list.bak
tee /etc/apt/sources.list << EOF
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
EOF

更新包

sudo apt-get update
sudo apt-get upgrade -y

十二、修改静态ip

修改/etc/netplan下的配置

#如ubuntu22
vi /etc/netplan/00-network-config.yaml

修改下面的配置

network:
    ethernets:
        enp6s18:
            addresses:
            - 192.168.1.11/24
            nameservers:
                addresses:
                - 192.168.1.1
                search: []
            routes:
            -   to: default
                via: 192.168.1.1
    version: 2

执行生效

netplan apply

如果在执行变更的时候提示如下,则需安装openvswitch-switch:root:cannot call open vswitch:

#安装openvswitch-switch
apt install openvswitch-switch

十三、网络优化配置

  1. 修改网络配置

    tee /etc/sysctl.conf << EOF
    net.core.somaxconn= 10240
    vm.swappiness = 0
    kernel.sysrq = 1
    
    net.ipv4.neigh.default.gc_stale_time = 120
    
    # see details in https://help.aliyun.com/knowledge_detail/39428.html
    net.ipv4.conf.all.src_valid_mark = 1
    net.ipv4.ip_forward = 1
    net.ipv4.conf.all.rp_filter = 0
    net.ipv4.conf.default.rp_filter = 0
    net.ipv4.conf.default.arp_announce = 2
    net.ipv4.conf.lo.arp_announce = 2
    net.ipv4.conf.all.arp_announce = 2
    
    # see details in https://help.aliyun.com/knowledge_detail/41334.html
    net.ipv4.tcp_max_tw_buckets = 5000
    net.ipv4.tcp_syncookies = 1
    net.ipv4.tcp_max_syn_backlog = 8192
    net.ipv4.tcp_synack_retries = 2
    net.ipv4.tcp_slow_start_after_idle = 0
    EOF
  2. 执行生效动作

    sysctl -p

十四、修改中文包

#下载中文包
apt-get install language-pack-zh-hans

#修改中文设置
tee /etc/default/locale << EOF
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:zh:en_US:en
EOF
#更新
update-locale LANG=zh_CN.UTF-8
#追加到环境变量
echo export LANG=zh_CN.utf8 >> /etc/profile
echo export LC_CTYPE=zh_CN.utf8 >> /etc/profile

#重启生效
reboot

十五、设置时区

查看当前时区

timedatectl

查看可用时区

timedatectl list-timezones

设置上海时区

timedatectl set-timezone Asia/Shanghai