本文最后更新于 2024-10-30,文章内容可能已经过时。

一、在Ubuntu Server中,使用普通账号登录后给root 账号设置密码

ubuntu@ubuntu:~$ sudo passwd root
[sudo] password for ubuntu: 
New password: 
Retype new password: 
passwd: password updated successfully
ubuntu@ubuntu:~$ sudo su -
[sudo] password for ubuntu: 
root@ubuntu:~# 

二、在Ubuntu Server中,默认情况下,出于安全考虑,root用户是不允许通过SSH进行远程登录的

三、在Ubuntu Server上修改默认locale为中文(简体中文,即zh_CN.UTF-8)涉及几个步骤

1、安装语言包
root@ubuntu:~# locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
root@ubuntu:~# sudo apt install language-pack-zh-hans
The following packages were automatically installed and are no longer required:
  linux-headers-6.11.0-8          linux-modules-6.11.0-8-generic        linux-tools-6.11.0-8
  linux-headers-6.11.0-8-generic  linux-modules-extra-6.11.0-8-generic  linux-tools-6.11.0-8-generic
Use 'sudo apt autoremove' to remove them.

Installing:
  language-pack-zh-hans

Installing dependencies:
  language-pack-zh-hans-base

Summary:
  Upgrading: 0, Installing: 2, Removing: 0, Not Upgrading: 2
  Download size: 1,820 kB
  Space needed: 7,285 kB / 21.9 GB available

Continue? [Y/n] Y
Get:1 http://cn.archive.ubuntu.com/ubuntu oracular/main amd64 language-pack-zh-hans-base all 1:24.10+20241003 [1,818 kB]
Get:2 http://cn.archive.ubuntu.com/ubuntu oracular/main amd64 language-pack-zh-hans all 1:24.10+20241003 [1,944 B]                                                  
Fetched 1,820 kB in 6s (287 kB/s)                                                                                                                                   
Selecting previously unselected package language-pack-zh-hans-base.
(Reading database ... 126495 files and directories currently installed.)
Preparing to unpack .../language-pack-zh-hans-base_1%3a24.10+20241003_all.deb ...
Unpacking language-pack-zh-hans-base (1:24.10+20241003) ...
Selecting previously unselected package language-pack-zh-hans.
Preparing to unpack .../language-pack-zh-hans_1%3a24.10+20241003_all.deb ...
Unpacking language-pack-zh-hans (1:24.10+20241003) ...
Setting up language-pack-zh-hans (1:24.10+20241003) ...
Setting up language-pack-zh-hans-base (1:24.10+20241003) ...
Generating locales (this might take a while)...
  zh_CN.UTF-8... done
  zh_SG.UTF-8... done
Generation complete.
Scanning processes...                                                                                                                                                
Scanning linux images...                                                                                                                                             

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
root@ubuntu:~# 
2、生成新的locale

使用locale-gen命令来生成或更新系统的locale设置。如果zh_CN.UTF-8还没有被生成,可以运行:

root@ubuntu:~# locale-gen zh_CN.UTF-8
Generating locales (this might take a while)...
  zh_CN.UTF-8... done
Generation complete.
root@ubuntu:~# update-locale
root@ubuntu:~# 
3、编辑配置文件

编辑/etc/default/locale文件来设置系统级别的locale

root@ubuntu:~# vim /etc/default/locale 
LANG=zh_CN.UTF-8
#LC_ALL=zh_CN.UTF-8
4、更新环境变量

为了使更改立即生效,需要更新当前终端会话的环境变

root@ubuntu:~# source /etc/default/locale
root@ubuntu:~# 
5、验证设置

通过运行locale命令来检查当前的locale设置是否正确

root@ubuntu:~# locale
LANG=zh_CN.UTF-8
LANGUAGE=
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER="zh_CN.UTF-8"
LC_NAME="zh_CN.UTF-8"
LC_ADDRESS="zh_CN.UTF-8"
LC_TELEPHONE="zh_CN.UTF-8"
LC_MEASUREMENT="zh_CN.UTF-8"
LC_IDENTIFICATION="zh_CN.UTF-8"
LC_ALL=
root@ubuntu:~#
6、重启服务

希望所有新打开的终端会话都使用新的locale设置,可能需要重新登录或者重启相关服务。对于一些服务如SSH,你可能还需要重启SSH服务以应用新的locale设置

root@ubuntu:~# systemctl restart ssh
root@ubuntu:~# 

备注说明:请注意,改变系统locale可能会影响某些程序的行为,尤其是那些依赖特定locale格式的应用程序。如果你遇到问题,可以考虑只对用户级别进行locale设置,而不是整个系统。要针对某个用户设置locale,可以在用户的家目录下的.profile.bashrc文件中设置相应的环境变量。

四、Ubuntu Server上将系统时间从UTC更改为北京时间(中国标准时间,CST)

root@ubuntu:~# date
2024年 10月 30日 星期三 09:30:46 UTC
root@ubuntu:~#
1、使用timedatectl命令

如果你的系统支持systemd,你可以使用timedatectl来设置时区。

sudo timedatectl set-timezone Asia/Shanghai
2、使用dpkg-reconfigure tzdata命令

这是一个交互式的命令,会提示你选择时区。

sudo dpkg-reconfigure tzdata

按照提示进行操作,选择Asia,然后选择Shanghai

root@ubuntu:~# sudo dpkg-reconfigure tzdata

Current default time zone: 'Asia/Shanghai'
Local time is now:      Wed Oct 30 17:35:34 CST 2024.
Universal Time is now:  Wed Oct 30 09:35:34 UTC 2024.

root@ubuntu:~#
3、手动更新时区文件

可以直接复制正确的时区文件到/etc/localtime

sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
4、编辑/etc/timezone文件
echo "Asia/Shanghai" | sudo tee /etc/timezone
5、同步硬件时钟

更新完系统时区后,建议同步硬件时钟以确保系统重启后时间仍然正确。

sudo hwclock --systohc
root@ubuntu:~# sudo hwclock --systohc
sudo: hwclock:找不到命令
root@ubuntu:~# sudo apt-get install util-linux
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
正在读取状态信息... 完成                 
util-linux 已经是最新版 (2.40.2-1ubuntu1)。
下列软件包是自动安装的并且现在不需要了:
  linux-headers-6.11.0-8 linux-headers-6.11.0-8-generic linux-modules-6.11.0-8-generic linux-modules-extra-6.11.0-8-generic linux-tools-6.11.0-8
  linux-tools-6.11.0-8-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 2 个软件包未被升级。
root@ubuntu:~# sudo hwclock --systohc
sudo: hwclock:找不到命令
root@ubuntu:~# hwclock --version
找不到命令 “hwclock”,但可以通过以下软件包安装它:
apt install util-linux-extra
root@ubuntu:~# sudo apt install util-linux-extra
下列软件包是自动安装的并且现在不需要了:       
  linux-headers-6.11.0-8          linux-modules-6.11.0-8-generic        linux-tools-6.11.0-8
  linux-headers-6.11.0-8-generic  linux-modules-extra-6.11.0-8-generic  linux-tools-6.11.0-8-generic
使用'sudo apt autoremove'来卸载它(它们)。

将要安装:
  util-linux-extra

摘要:
  升级:0,安装:1,卸载:0,不升级:2
  下载大小:218 kB
  所需的空间:724 kB / 21.9 GB 可用

获取:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu oracular/universe amd64 util-linux-extra amd64 2.40.2-1ubuntu1 [218 kB]
已下载 218 kB,耗时 3秒 (80.9 kB/s)        
正在选中未选择的软件包 util-linux-extra。
(正在读取数据库 ... 系统当前共安装有 126725 个文件和目录。)
准备解压 .../util-linux-extra_2.40.2-1ubuntu1_amd64.deb  ...
正在添加 通过 util-linux-extra 从 /sbin/ctrlaltdel 到 /sbin/ctrlaltdel.usr-is-merged 的转移
正在添加 通过 util-linux-extra 从 /sbin/fsck.cramfs 到 /sbin/fsck.cramfs.usr-is-merged 的转移
正在添加 通过 util-linux-extra 从 /sbin/fsck.minix 到 /sbin/fsck.minix.usr-is-merged 的转移
正在添加 通过 util-linux-extra 从 /sbin/mkfs.bfs 到 /sbin/mkfs.bfs.usr-is-merged 的转移
正在添加 通过 util-linux-extra 从 /sbin/mkfs.cramfs 到 /sbin/mkfs.cramfs.usr-is-merged 的转移
正在添加 通过 util-linux-extra 从 /sbin/mkfs.minix 到 /sbin/mkfs.minix.usr-is-merged 的转移
正在解压 util-linux-extra (2.40.2-1ubuntu1) ...
正在设置 util-linux-extra (2.40.2-1ubuntu1) ...
正在处理用于 man-db (2.12.1-3) 的触发器 ...
Scanning processes...                                                                                                                                                
Scanning linux images...                                                                                                                                             

Running kernel seems to be up-to-date.

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
root@ubuntu:~# hwclock --version
hwclock,来自 util-linux 2.40.2
root@ubuntu:~# sudo hwclock --systohc
root@ubuntu:~#
6、验证设置

使用date命令来检查当前的日期和时间是否已经变更为北京时间。

root@ubuntu:~# date
2024年 10月 30日 星期三 17:48:36 CST

完成上述步骤后,Ubuntu Server应该已经使用了北京时间。请注意,如果你的服务器是云服务提供商托管的,并且他们提供了自己的NTP服务,可能需要额外配置NTP客户端来确保时间与提供商的时间保持一致。通常,这可以通过安装和配置ntpchrony服务来实现。