https://mirrors.tuna.tsinghua.edu.cn/ubuntu),后续可通过命令行修改(见“系统配置”部分)。sudo apt update && sudo apt upgrade -y
/etc/netplan/00-installer-config.yaml),修改为以下内容(以桥接模式为例):network:
version: 2
ethernets:
ens33: # 网卡名称(通过`ip a`命令查看)
dhcp4: no
addresses: [192.168.1.100/24] # 静态IP地址(与主机同一网段)
gateway4: 192.168.1.1 # 网关(主机网关)
nameservers:
addresses: [8.8.8.8, 8.8.4.4] # DNS服务器
保存后应用配置:sudo netplan apply
ufw(Uncomplicated Firewall),仅允许必要端口(如SSH的22端口):sudo apt install ufw -y
sudo ufw allow OpenSSH # 允许SSH
sudo ufw enable # 启用防火墙
sudo ufw status # 查看状态(确认规则生效)
/etc/ssh/sshd_config),修改以下参数提升安全性:sudo vi /etc/ssh/sshd_config
修改内容:Port 2222 # 修改SSH端口(避免默认22被扫描)
PermitRootLogin no # 禁止root远程登录
PasswordAuthentication yes # 允许密码登录(若用密钥登录可设为no)
保存后重启SSH服务:sudo systemctl restart sshd
ssh ubuntu@192.168.1.100 -p 2222),输入密码即可登录。若使用密钥登录,需提前生成密钥对并将公钥复制到虚拟机(ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@192.168.1.100 -p 2222)。sudo apt install apache2 -y # 安装Apache
sudo systemctl start apache2 # 启动服务
sudo systemctl enable apache2 # 开机自启
测试:浏览器访问http://192.168.1.100,应看到Apache默认页面。sudo apt install mysql-server -y # 安装MySQL
sudo mysql_secure_installation # 运行安全脚本(设置root密码、删除匿名用户等)
sudo systemctl start mysql # 启动服务
sudo systemctl enable mysql # 开机自启
sudo apt install vsftpd -y # 安装vsftpd
sudo systemctl start vsftpd # 启动服务
sudo systemctl enable vsftpd # 开机自启
通过以上步骤,即可在Ubuntu虚拟机中搭建一个基础的服务器,满足远程访问、Web服务、数据库存储等需求。后续可根据业务需求安装更多应用(如Postfix邮件服务器、Redis缓存服务器等)。