Ubuntu Server 22.04 服务器配置。
一、Ubuntu 基本配置
首先,掌握 apt 命令的基本使用:https://itsfoss.com/apt-command-guide/
1. 开启 root 用户远程登录
对于 服务器版本 来说,默认的 root 用户是禁止使用密码登录的(prohibit-password),这里修改允许使用密码进行远程登录。
# 设置 root 用户的密码
sudo passwd root
编辑文件 sudo vim /etc/ssh/sshd_config,找到 #PermitRootLogin prohibit-password 所在的行,去掉注释并修改 prohibit-password 为 yes,保存退出并重启 sshd 服务。
systemctl restart sshd
2. apt-get
# 查看所有的安装软件
dpkg --list
# 安装已经下载好的 deb 安装包
dpkg -i 安装包
# 更新所有可更新的软件包
sudo apt update && sudo apt upgrade
# 卸载指定软件并删除配置文件
sudo apt --purge remove 包名
# 清理不再使用的依赖和库文件
sudo apt autoremove
3. 禁止使用 snap
snap 可以获取软件的最新版,Ubuntu 逐渐迁移很多软件包到 snap,这样会导致软件不稳定。当然优点也是显而易见的,不过只对开发人员友好。对于个人日常使用、生产环境等场景还是不需要的。
卸载 snap 请参考:https://ubuntuhandbook.org/index.php/2022/04/remove-snap-block-ubuntu-2204/amp/
# 查看列表
snap list
# 卸载所有列表中的软件,有些软件依赖其它组件的,注意先后顺序
sudo snap remove --purge [soft]
# 最后卸载 snapd
sudo apt remove --autoremove snapd
然后编辑文件 sudo vim /etc/apt/preferences.d/nosnap.pref 添加下面的内容禁止 apt 使用 sanp 包。
# To prevent repository packages from triggering the installation of snap,
# this file forbids snapd from being installed by APT.
Package: snapd
Pin: release a=*
Pin-Priority: -10
之后执行 sudo apt update,就不会使用 snap 下载了。
4. 安装基础的开发、编译工具
由于最小化的服务器版安装有一些编译库默认是没有安装的,需要用户手动安装,所以这里记一下常用的基础库:
# gcc/g++/gdb/make 编译工具
apt-get install build-essential
# ssl-lib
apt-get install openssl libssl-dev
apt-get install zlib1g-dev
5. 时区设置
Ubuntu 可以使用内置的 timedatectl 设置系统的时区。
# 查看当前的时区
timedatectl
# 查看支持的时区列表
timedatectl list-timezones
# 是否含有中国时区
timedatectl list-timezones | grep Asia/Shanghai
# 设置中国时区
timedatectl set-timezone Asia/Shanghai
# 无需重启,再次查看
timedatectl
6. 语言设置
① Ubuntu 22.04 以上
# 查看当前系统的语言配置
locale
# 如果使用的 Ubuntu 22.04 以上使用这个命令将更加简单容易,选择完成之后注销重启
sudo dpkg-reconfigure locales
# 再次查看配置
locale
② Ubuntu 22.04 以下
# 下面的适用旧版本 Ubuntu 22.04 以下
# 列出所有启用的区域设置
locale -a
# 编辑 locale.gen 文件
# 取消 zh_CN.UTF-8 UTF-8 en_US.UTF-8 UTF-8 两行的注释
vim /etc/locale.gen
# 编辑保存后,执行以下命令
locale-gen
# 显示正在使用的 Locale 和相关的环境变量
locale
# 设置整个系统使用的区域设置
localectl set-locale LANG=zh_CN.UTF-8
# 立即生效
unset LANG
如果使用上面的立即生效命令仍然有问题,没有生效,请重启计算机。
7. Vim 配置
Linux 中有很多编辑工具,像 Vim、Nano 等,都是非常不错的,可以使用自己喜欢的。Vim 的全局配置文件在 /etc/vim/vimrc 或者 /etc/vimrc,对所有用户生效;用户的配置文件一般在 ~/.vimrc 中。
这里修改全局的配置,编辑 {% mark /etc/vim/vimrc color:green %} 文件,没有的话可以手动创建一个。下面给了一些简单实用的配置。
" 文件编码
set encoding=utf-8
set fileencoding=utf-8
" 自动缩进
set autoindent
" 新行智能自动缩进
set smartindent
" 状态栏标尺
set ruler
" 语法高亮 vim5 之后默认开启
set syntax=on
" Tab键的宽度
set tabstop=4
" 搜索忽略大小写
set ignorecase
" 高亮显示匹配的括号
set showmatch
" 总是显示行号
set nu
" 侦测文件类型
filetype on
" 状态行显示的内容(包括文件类型和解码)
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
" 总是显示状态行
set laststatus=2
" 高亮搜索的文本
set hlsearch
二、常用软件
1. Redis
wget https://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz
cd redis-stable
make && make install
redis-server &
2. MySQL
apt-get安装:https://blog.lanluo.cn/8662
3. Docker
# 卸载旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
# Add Docker’s official GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 设置稳定版仓库
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 更新包索引
sudo apt-get update
####################### 下面二选一 #############################
# 1. 安装最新版本的 Docker Engine-Community 和 containerd ,或者转到下一步安装特定版本:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 2.
# 查看版本
apt-cache madison docker-ce
# 选择指定的版本安装
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker-compose-plugin
4. btop++
# 下载解压安装
wget https://github.com/aristocratos/btop/releases/download/v1.2.8/btop-x86_64-linux-musl.tbz
mkdir btop && tar xvf btop-x86_64-linux-musl.tbz -C btop
cd btop
./install.sh
# 运行
btop
5. Nodejs
# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
6. open-jdk
sudo apt-get install openjdk-8-jdk
三、其它
1. 查找apt安装的软件的路径
这里以jdk为例
which java
# 多次执行,直到最后打印出一些系统信息
file 上一条命令打印的路径
2. 开放指定端口
随着 Ubuntu 的更新,它的各种软件也在更新,以前用 firewall,新版本(22.04)已经默认使用 ufw,相比较而言,UFW command more easily,不过具体使用这里就不详细写了,可以参考官网的说明,很简单。
# 启动防火墙
systemctl start firewalld
# --zone 作用域 --add-port 格式 端口/通讯协议 --permanent 永久生效
firewall-cmd --zone=public --add-port=1935/tcp --permanent
# 重启
firewall-cmd --reload
# 查看当前所有tcp端口
netstat -ntlp
# 查看所有1935端口使用情况
netstat -ntulp | grep 1935
# 启用 ufw
ufw enable
# 禁用 ufw
ufw disable
# 查看状态
ufw status verbose
# 放行端口
ufw allow port/协议(TCP|UDP)
# 关闭端口
ufw deny port/协议
# 允许特定 IP 访问
ufw allow from IP
3. ftp服务搭建
下载 完成后上传至服务器,执行以下命令安装
dpkg -i xxx.deb
安装完成后编辑 /etc/vsftpd.conf 文件:
listen=NO
listen_ipv6=YES
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
chroot_local_user=YES
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
# 新增
pasv_enable=Yes
pasv_min_port=10000
pasv_max_port=10100
allow_writeable_chroot=YES
更新防火墙:sudo ufw allow from any to any port 20,21,10000:10100 proto tcp
重启服务:sudo systemctl restart vsftpd
创建新用户并设置密码:
sudo useradd -m ftptest
sudo passwd ftptest
New password:
Retype new password:
passwd: password updated successfully
# 出于安全考虑,限定用户ftpuser不能登录,只能ftp
usermod -s /sbin/nologin ftpuser
# 出于安全考虑,我们将禁用此用户的 ssh 权限。输入
sudo vim /etc/ssh/sshd_config
# 在此文件中添加以下行,然后重启 SSH sudo systemctl restart sshd
DenyUsers ftpuser
添加文件(连接 ftp 时展示的目录为登录的用户的主目录,所以为了安全我们使用新建的用户):
sudo echo "this is a simple file" > /home/ftptest/test.txt
然后就可以连接测试了:ftp://ip
四、一些问题
1. Docker
Ubuntu version 22.04 Docker version 23.0.1 Docker Compose version v2.16.0 安装 Docker 工具:snap 错误原因:
Cannot Connect to the Docker Daemon at ‘unix:///var/run/docker.sock’
解决参考:https://appuals.com/cannot-connect-to-the-docker-daemon-at-unix-var-run-docker-sock/
如果不能解决,尝试下面的方法:
# 停止 docker
systemctl stop docker.socket
systemctl stop docker
# 查看 snap 管理的 docker 状态
snap services
# 停止服务
snap stop docker
# 然后重启
systemctl start docker

说些什么吧!