跳转至

Ubuntu 优化

系统:Ubuntu 24.04.4 LTS

sudo免密

普通账户执行sudo需要输入密码,可以配置免密

创建文件添加下列内容:

Bash
sudo vim /etc/sudoers.d/nopass
chaic  ALL=(ALL)NOPASSWD:ALL

rc.local

Bash
# 创建文件
sudo vim /etc/rc.local
#---start
#!/bin/bash
# 这是一个 rc.local 示例文件
# 在这里添加启动命令
#xxx.sh

# 设置主机名
hostip=$(/sbin/ip a | grep 192.168|grep "/2" | awk '{print $2}'|awk -F "/" '{print $1}' |sort -t '.' -k 3 -n|sed -n 1p)
#set hostname
hostnamectl set-hostname Ubuntu-${hostip}

# 确保以 exit 0 结束
exit 0
#---end

# 添加执行权限
sudo chmod +x /etc/rc.local
# 启用 rc-local 服务
sudo systemctl enable rc-local.service
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service

PS1友好显示

Bash
1
2
3
4
5
6
# 在模版文件最后添加
sudo vim /etc/skel/.bashrc
...
PS1='\[\033[1;33m\]\H \[\033[1;32m\][\w] \[\033[1;35m\]\D{%Y-%m-%d %T}\n\[\033[1;36m\]\u@pts/\l \[\033[00m\]\$ '
...
# 注意:此文件是新建bash用户的默认模版,修改后新建用户可以继承此配置,但是原先已有账户不会重新读取,需要手动修改
Bash
1
2
3
vim /root/.bashrc
...
PS1='\[\033[1;33m\]\H \[\033[1;32m\][\w] \[\033[1;35m\]\D{%Y-%m-%d %T}\n\[\033[1;36m\]\u@pts/\l \[\033[00m\]\$ '

source /root/.bashrc

ssh配置

Bash
# 安装ssh服务
sudo apt install openssh-server openssh-client

# 编辑配置文件
sudo vim /etc/ssh/sshd_config

# sshd_config add 允许root登录
PermitRootLogin yes

# 重启ssh服务
sudo systemctl restart ssh
sudo systemctl enable ssh

# 配置密码
passwd root
新密码:

配置时区

Bash
# 安装 chrony
sudo apt update && sudo apt install -y chrony

# 禁用 systemd-timesyncd
sudo timedatectl set-ntp false
sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd

# vim /etc/chrony/chrony.conf
# 支持多 NTP 源
pool ntp.aliyun.com iburst
pool ntp.tencent.com iburst

# 启用 chrony
sudo systemctl enable chrony
sudo systemctl start chrony

# 查看时间配置
timedatectl status
# 请将 Asia/Shanghai 替换为你的目标时区
sudo timedatectl set-timezone Asia/Shanghai

# 再次查看
timedatectl status
               Local time:  2026-03-17 13:12:43 CST   # 本地时间(时间正确)
           Universal time:  2026-03-17 05:12:43 UTC   # UTC时间
                 RTC time:  2026-03-17 05:12:43       # 硬件时钟
                Time zone: Asia/Shanghai (CST, +0800)   # 时区正确(Asia/Shanghai)
System clock synchronized: yes                          # 时间已同步(NTP同步正确)
              NTP service: active                       # NTP 服务运行正常
          RTC in local TZ: no                           # 双系统才建议开启