Ubuntu 24.04安装后常用软件配置与SSH连接设置
•软件实施
Ubuntu 24.04安装后常用软件配置与SSH连接设置
Ubuntu 24.04 LTS是最新的长期支持版本。本文将详细介绍系统安装后的初始化配置、常用软件安装和SSH安全设置。
一、系统初始化配置
1. 更新系统软件包
# 更新软件包列表
sudo apt update
# 升级已安装的软件包
sudo apt upgrade -y
# 升级系统(可选,谨慎操作)
sudo apt full-upgrade -y
# 清理不需要的软件包
sudo apt autoremove -y
sudo apt autoclean
2. 配置系统时区和语言
# 查看当前时区
timedatectl
# 设置为上海时区
sudo timedatectl set-timezone Asia/Shanghai
# 查看语言设置
locale
# 安装中文语言支持
sudo apt install -y language-pack-zh-hans language-pack-zh-hans-base
# 生成中文locale
sudo locale-gen zh_CN.UTF-8
sudo update-locale LANG=zh_CN.UTF-8
3. 配置主机名
# 查看当前主机名
hostnamectl
# 修改主机名
sudo hostnamectl set-hostname myserver
# 编辑hosts文件
sudo vim /etc/hosts
添加或修改:
127.0.0.1 localhost
127.0.1.1 myserver
4. 配置时间同步
# 检查systemd-timesyncd状态
systemctl status systemd-timesyncd
# 配置NTP服务器
sudo vim /etc/systemd/timesyncd.conf
修改为:
[Time]
NTP=ntp.aliyun.com ntp.tencent.com ntp.ntsc.ac.cn
FallbackNTP=ntp.ubuntu.com
# 重启时间同步服务
sudo systemctl restart systemd-timesyncd
# 验证时间同步
timedatectl timesync-status
二、基础工具安装
1. 常用系统工具
# 安装常用工具
sudo apt install -y \
vim \
git \
curl \
wget \
htop \
net-tools \
iotop \
iftop \
tcpdump \
telnet \
dnsutils \
unzip \
zip \
rar \
tree \
tmux \
screen \
rsync \
lsof \
strace \
build-essential \
cmake \
pkg-config \
ca-certificates \
gnupg \
lsb-release \
software-properties-common
2. 文本编辑器配置
配置Vim:
# 编辑Vim配置文件
vim ~/.vimrc
添加基本配置:
" 显示行号
set number
" 高亮当前行
set cursorline
" 自动缩进
set autoindent
set smartindent
" 设置Tab为4个空格
set tabstop=4
set shiftwidth=4
set expandtab
" 搜索时忽略大小写
set ignorecase
set smartcase
" 显示匹配的括号
set showmatch
" 开启语法高亮
syntax on
3. 配置Git
# 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 配置默认分支名
git config --global init.defaultBranch main
# 配置编辑器
git config --global core.editor vim
# 查看配置
git config --list
# 生成SSH密钥(用于Git认证)
ssh-keygen -t ed25519 -C "your.email@example.com" -f ~/.ssh/id_ed25519 -N ""
三、SSH服务器配置
1. 安装SSH服务器
# 安装OpenSSH Server
sudo apt install -y openssh-server
# 检查SSH服务状态
sudo systemctl status ssh
# 启动并设置开机自启
sudo systemctl start ssh
sudo systemctl enable ssh
2. SSH安全配置
# 备份原始配置
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
# 编辑SSH配置
sudo vim /etc/ssh/sshd_config
修改或添加以下配置:
# SSH端口(建议修改默认端口)
Port 2222
# 禁止root远程登录
PermitRootLogin no
# 禁止密码登录(使用密钥登录)
PasswordAuthentication no
# 禁止空密码
PermitEmptyPasswords no
# 限制最大认证尝试次数
MaxAuthTries 5
# 超时断开
ClientAliveInterval 300
ClientAliveCountMax 3
# 禁用不安全的认证方式
GSSAPIAuthentication no
# 使用最新协议
Protocol 2
# 只允许特定用户登录
AllowUsers admin deploy
# 允许特定用户组
AllowGroups sshusers
3. 配置SSH密钥认证
在客户端生成密钥:
# 在本地机器上生成密钥
ssh-keygen -t ed25519 -C "your@email.com" -f ~/.ssh/id_ed25519 -N ""
# 复制公钥到服务器
ssh-copy-id -p 2222 admin@your_server_ip
# 或者手动添加
cat ~/.ssh/id_ed25519.pub | ssh -p 2222 admin@your_server_ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
在服务器上配置:
# 创建用户(如果不存在)
sudo useradd -m -s /bin/bash admin
sudo passwd admin
# 将用户添加到sudo组
sudo usermod -aG sudo admin
# 创建.ssh目录
sudo mkdir -p /home/admin/.ssh
sudo chmod 700 /home/admin/.ssh
# 复制客户端的公钥到authorized_keys
sudo vim /home/admin/.ssh/authorized_keys
# 设置正确的权限
sudo chmod 600 /home/admin/.ssh/authorized_keys
sudo chown -R admin:admin /home/admin/.ssh
4. 重启SSH服务并验证
# 测试配置文件语法
sudo sshd -t
# 重启SSH服务
sudo systemctl restart ssh
# 从客户端测试连接(新开一个终端)
ssh -p 2222 admin@your_server_ip
# 如果连接成功,再禁用密码登录
sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo systemctl restart ssh
5. 配置SSH客户端(可选)
在本地机器上配置SSH客户端,方便连接:
# 编辑SSH客户端配置
vim ~/.ssh/config
添加:
Host myserver
HostName your_server_ip
Port 2222
User admin
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 300
ServerAliveCountMax 3
StrictHostKeyChecking no
使用配置连接:
# 直接使用别名连接
ssh myserver
6. 防火墙配置
# 检查防火墙状态
sudo ufw status
# 允许新的SSH端口
sudo ufw allow 2222/tcp
# 启用防火墙(如果未启用)
sudo ufw enable
# 查看防火墙规则
sudo ufw status numbered
# 如果需要删除规则
sudo ufw delete 1
四、常用软件安装
1. 开发环境
Node.js:
# 安装Node.js 20.x LTS
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
# 验证安装
node -v
npm -v
# 配置npm源为国内镜像
npm config set registry https://registry.npmmirror.com
# 安装常用npm包
sudo npm install -g yarn pm2
Python:
# Ubuntu 24.04默认自带Python 3.12
python3 --version
# 安装Python开发工具
sudo apt install -y python3-dev python3-pip python3-venv
# 配置pip镜像
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF
# 验证
pip3 config list
Java:
# 安装OpenJDK 17
sudo apt install -y openjdk-17-jdk
# 或安装OpenJDK 11
sudo apt install -y openjdk-11-jdk
# 配置环境变量
cat >> ~/.bashrc << 'EOF'
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$PATH:$JAVA_HOME/bin
EOF
# 重载配置
source ~/.bashrc
# 验证
java -version
javac -version
Go:
# 下载Go(请访问官网获取最新版本)
cd /tmp
wget https://go.dev/dl/go1.23.0.linux-amd64.tar.gz
# 解压到/usr/local
sudo tar -C /usr/local -xzf go1.23.0.linux-amd64.tar.gz
# 配置环境变量
cat >> ~/.bashrc << 'EOF'
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOF
# 重载配置
source ~/.bashrc
# 验证
go version
# 配置Go模块代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
2. 数据库服务
MySQL 8.0:
# 安装MySQL
sudo apt install -y mysql-server
# 启动并设置开机自启
sudo systemctl start mysql
sudo systemctl enable mysql
# 安全配置
sudo mysql_secure_installation
# 登录MySQL
sudo mysql -u root -p
# 创建用户和数据库
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'your_password';
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON mydb.* TO 'admin'@'localhost';
FLUSH PRIVILEGES;
EXIT;
# 允许远程连接(生产环境不建议)
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
修改:
bind-address = 0.0.0.0
# 重启MySQL
sudo systemctl restart mysql
# 防火墙开放MySQL端口
sudo ufw allow 3306/tcp
PostgreSQL:
# 安装PostgreSQL
sudo apt install -y postgresql postgresql-contrib
# 启动并设置开机自启
sudo systemctl start postgresql
sudo systemctl enable postgresql
# 切换到postgres用户
sudo -u postgres psql
# 创建用户和数据库
CREATE USER admin WITH PASSWORD 'your_password';
CREATE DATABASE mydb OWNER admin;
GRANT ALL PRIVILEGES ON DATABASE mydb TO admin;
\q
# 修改认证方式
sudo vim /etc/postgresql/16/main/pg_hba.conf
修改:
# 本地连接使用密码认证
local all all scram-sha-256
# 重启PostgreSQL
sudo systemctl restart postgresql
# 测试连接
psql -h localhost -U admin -d mydb
Redis:
# 安装Redis
sudo apt install -y redis-server
# 启动并设置开机自启
sudo systemctl start redis-server
sudo systemctl enable redis-server
# 配置Redis
sudo vim /etc/redis/redis.conf
修改配置:
# 设置密码
requirepass your_redis_password
# 允许远程连接(可选)
bind 0.0.0.0
# 持久化配置
appendonly yes
# 重启Redis
sudo systemctl restart redis-server
# 测试连接
redis-cli -a your_redis_password ping
3. Web服务器
Nginx:
# 安装Nginx
sudo apt install -y nginx
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 检查状态
sudo systemctl status nginx
# 防火墙开放HTTP/HTTPS端口
sudo ufw allow 'Nginx Full'
# 测试配置
sudo nginx -t
# 重新加载配置
sudo systemctl reload nginx
# 默认网站目录
cd /var/www/html
Apache:
# 安装Apache
sudo apt install -y apache2
# 启动并设置开机自启
sudo systemctl start apache2
sudo systemctl enable apache2
# 防火墙开放HTTP/HTTPS端口
sudo ufw allow 'Apache Full'
4. 容器化工具
Docker:
# 安装依赖
sudo apt install -y \
ca-certificates \
curl \
gnupg \
lsb-release
# 添加Docker官方GPG密钥
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
# 设置Docker仓库
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装Docker
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 启动并设置开机自启
sudo systemctl start docker
sudo systemctl enable docker
# 将用户添加到docker组(免sudo)
sudo usermod -aG docker $USER
newgrp docker
# 验证安装
docker --version
docker run hello-world
# 配置Docker镜像加速
sudo mkdir -p /etc/docker
sudo vim /etc/docker/daemon.json
添加:
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn",
"https://hub-mirror.c.163.com"
],
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
# 重启Docker
sudo systemctl daemon-reload
sudo systemctl restart docker
5. 监控与维护工具
Netdata:
# 安装Netdata
wget -O /tmp/netdata-kickstart.sh https://my-netdata.io/kickstart.sh
chmod +x /tmp/netdata-kickstart.sh
sudo /tmp/netdata-kickstart.sh --stable-channel
# 访问Netdata
# http://your_server_ip:19999
Fail2Ban:
# 安装Fail2Ban
sudo apt install -y fail2ban
# 配置
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo vim /etc/fail2ban/jail.local
配置SSH防护:
[sshd]
enabled = true
port = 2222
maxretry = 3
bantime = 3600
findtime = 600
# 启动并设置开机自启
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
# 查看状态
sudo fail2ban-client status
sudo fail2ban-client status sshd
五、安全加固
1. 系统安全更新
# 安装 unattended-upgrades
sudo apt install -y unattended-upgrades
# 启用自动安全更新
sudo dpkg-reconfigure -plow unattended-upgrades
# 配置自动更新
sudo vim /etc/apt/apt.conf.d/50unattended-upgrades
修改配置:
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
};
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "02:00";
2. 内核安全参数
# 编辑sysctl配置
sudo vim /etc/sysctl.conf
添加以下安全参数:
# 防止IP欺骗
net.ipv4.conf.all.rp_filter = 1
# 防止SYN洪水攻击
net.ipv4.tcp_syncookies = 1
# 禁用ICMP重定向
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# 禁用源路由
net.ipv4.conf.all.accept_source_route = 0
# 增加端口范围
net.ipv4.ip_local_port_range = 1024 65000
# 应用配置
sudo sysctl -p
六、快速配置脚本
创建一键初始化脚本:
vim ~/setup.sh
#!/bin/bash
# Ubuntu 24.04 初始化脚本
echo "=== 开始系统初始化 ==="
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装基础工具
echo "=== 安装基础工具 ==="
sudo apt install -y vim git curl wget htop net-tools tree tmux
# 配置时区
echo "=== 配置时区 ==="
sudo timedatectl set-timezone Asia/Shanghai
# 配置Vim
echo "=== 配置Vim ==="
cat > ~/.vimrc << 'EOF'
set number
set cursorline
set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
syntax on
EOF
echo "=== 初始化完成,请手动配置SSH和安装其他软件 ==="
# 添加执行权限
chmod +x ~/setup.sh
# 执行脚本
~/setup.sh
总结
本文介绍了Ubuntu 24.04安装后的完整配置流程,包括系统初始化、常用软件安装、SSH安全配置等。按照这些步骤操作,你可以快速部署一个安全、高效的服务器环境。
建议根据实际需求选择合适的软件和配置进行安装。