目录
1.镜像下载
2.安装虚拟机
3.配置虚拟机模板
3.1配置IP地址及主机名
3.2配置系统用户
3.3修改网卡名称
3.4调整文件描述符大小
3.5优化history
3.6配置防火墙
3.7配置软件源
3.8配置时钟同步
3.9优化内核参数
3.10垃圾盒历史记录清理
3.11添加镜头IP地址模板
1.镜像下载
https://cn.ubuntu.com/download/server/step1

https://releases.ubuntu.com/24.04/ubuntu-24.04.1-live-server-amd64.iso
2.安装虚拟机
















3.配置虚拟机模板
3.1配置IP地址及主机名
查看网卡名称
|
sbdz@poc:~$ sudo su [sudo] password for sbdz: root@poc:/home/sbdz# ip link show |
|
1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens160: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:50:56:a8:ef:62 brd ff:ff:ff:ff:ff:ff altname enp3s0 |
|
root@poc:/home/sbdz# cd /etc/netplan root@poc:/etc/netplan# ls -l |
|
-rw——- 1 root root 658 Oct 30 07:27 50-cloud-init.yaml |
|
root@poc:/etc/netplan# vim 50-cloud-init.yaml |
|
network: renderer: networkd ethernets: ens160: dhcp4: false dhcp6: false addresses: [10.9.254.24/24] routes: – to: default via: 10.9.254.1 nameservers: addresses: [114.114.114.114] search: [] version: 2 ##「renderer」配置为networkd,使用 systemd-networkd 作为网络配置的后端。桌面环境使用 NetworkManager,Ubuntu Server 和无头环境使用networkd。 |
|
root@poc:/etc/netplan# netplan apply root@poc:/etc/netplan# ip addr show |
|
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host noprefixroute valid_lft forever preferred_lft forever 2: ens160: mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:50:56:a8:ef:62 brd ff:ff:ff:ff:ff:ff altname enp3s0 inet 10.9.254.24/24 brd 10.9.254.255 scope global ens160 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fea8:ef62/64 scope link valid_lft forever preferred_lft forever |
|
root@poc:/etc/netplan# ip route show |
|
default via 10.9.254.1 dev ens160 proto static 10.9.254.0/24 dev ens160 proto kernel scope link src 10.9.254.24 |
|
root@poc:/etc/netplan# ping www.baidu.com |
|
PING www.a.shifen.com (220.181.38.150) 56(84) bytes of data. 64 bytes from 220.181.38.150: icmp_seq=1 ttl=51 time=18.6 ms 64 bytes from 220.181.38.150: icmp_seq=2 ttl=51 time=19.5 ms 64 bytes from 220.181.38.150: icmp_seq=3 ttl=51 time=18.5 ms |
设置主机名
|
root@poc:~# hostnamectl set-hostname ubuntu root@poc:~# bash root@ubuntu:~# |
3.2配置系统用户
|
root@ubuntu:~# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak root@ubuntu:~# vim /etc/ssh/sshd_config |
|
PermitRootLogin yes PasswordAuthentication yes GSSAPIAuthentication no UseDNS no |
|
root@ubuntu:~# systemctl enable ssh.service |
|
Synchronizing state of ssh.service with SysV service script with /usr/lib/systemd/systemd-sysv-install. Executing: /usr/lib/systemd/systemd-sysv-install enable ssh Created symlink /etc/systemd/system/sshd.service → /usr/lib/systemd/system/ssh.service. Created symlink /etc/systemd/system/multi-user.target.wants/ssh.service → /usr/lib/systemd/system/ssh.service. |
|
root@ubuntu:~# systemctl restart sshd root@ubuntu:~# passwd root |
|
New password: Retype new password: passwd: password updated successfully |
|
root@ubuntu:~# vim /etc/hosts |
|
127.0.1.1 ubuntu |
3.3修改网卡名称
|
root@ubuntu:~# cp /etc/default/grub /etc/default/grub.bak root@ubuntu:~# vim /etc/default/grub |
|
GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0" |
|
root@ubuntu:~# grub-mkconfig -o /boot/grub/grub.cfg |
|
Sourcing file `/etc/default/grub' Generating grub configuration file … Found linux image: /boot/vmlinuz-6.8.0-41-generic Found initrd image: /boot/initrd.img-6.8.0-41-generic Warning: os-prober will not be executed to detect other bootable partitions. Systems on them will not be added to the GRUB boot configuration. Check GRUB_DISABLE_OS_PROBER documentation entry. Adding boot menu entry for UEFI Firmware Settings … done |
|
root@ubuntu:~# update-grub |
|
Sourcing file `/etc/default/grub' Generating grub configuration file … Found linux image: /boot/vmlinuz-6.8.0-41-generic Found initrd image: /boot/initrd.img-6.8.0-41-generic Warning: os-prober will not be executed to detect other bootable partitions. Systems on them will not be added to the GRUB boot configuration. Check GRUB_DISABLE_OS_PROBER documentation entry. Adding boot menu entry for UEFI Firmware Settings … done |
|
root@ubuntu:~# vim /etc/netplan/50-cloud-init.yaml |
|
network: renderer: networkd ethernets: eth0: dhcp4: false dhcp6: false addresses: [10.9.254.24/24] routes: – to: default via: 10.9.254.1 nameservers: addresses: [114.114.114.114] search: [] version: 2 |
|
root@ubuntu:~# reboot root@ubuntu:~# ip link show |
|
1: lo: mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000 link/ether 00:50:56:a8:ef:62 brd ff:ff:ff:ff:ff:ff altname enp3s0 altname ens160 |
3.4调整文件描述符大小
|
root@ubuntu:~# vim /etc/profile |
|
ulimit -SHn 655350 |
|
root@ubuntu:~# vim /etc/sysctl.conf |
|
fs.file-max = 655350 |
|
root@ubuntu:~# vim /etc/security/limits.conf |
|
root soft nofile 655350 root hard nofile 655350 * soft nofile 655350 * hard nofile 655350 |
|
root@ubuntu:~# vim /etc/pam.d/common-session |
|
session required pam_limits.so |
|
root@ubuntu:~# source /etc/profile |
3.5优化history
|
root@ubuntu:~# vim /etc/profile |
|
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` " export HISTFILESIZE=1000 export HISTSIZE=1000 |
|
root@ubuntu:~# source /etc/profile |
3.6配置防火墙
|
root@ubuntu:~# apt list –installed | grep ufw |
|
WARNING: apt does not have a stable CLI interface. Use with caution in scripts. ufw/noble,now 0.36.2-6 all [installed,automatic] |
|
root@ubuntu:~# ufw disable |
|
Firewall stopped and disabled on system startup |
|
journalctl 命令自动维护文件大小 |
|
只保留近一周的日志 root@ubuntu:~# journalctl –vacuum-time=1w |
|
Vacuuming done, freed 0B of archived journals from /var/log/journal/a012fa6cac384fefa4ee34c9da66fc85. Vacuuming done, freed 0B of archived journals from /run/log/journal. Vacuuming done, freed 0B of archived journals from /var/log/journal. |
|
只保留500MB的日志 root@ubuntu:~# journalctl –vacuum-size=500M |
|
Vacuuming done, freed 0B of archived journals from /var/log/journal. Vacuuming done, freed 0B of archived journals from /var/log/journal/a012fa6cac384fefa4ee34c9da66fc85. Vacuuming done, freed 0B of archived journals from /run/log/journal. |
3.7配置软件源
|
root@ubuntu:~# cp /etc/apt/sources.list /etc/apt/sources.list.bak root@ubuntu:~# vim /etc/apt/sources.list |
|
deb https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse # deb https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse # deb-src https://mirrors.aliyun.com/ubuntu/ noble-proposed main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse deb-src https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse |
|
root@ubuntu:~# apt-get update |
|
Fetched 61.0 MB in 18s (3,450 kB/s) Reading package lists… Done |
|
root@ubuntu:~# apt-get -y install vim wget git unzip zip ntp ntpdate lrzsz lftp tree bash-completion elinks dos2unix tmux jq pdsh root@ubuntu:~# apt-get -y install nmap net-tools mtr traceroute tcptraceroute aptitude htop iftop hping3 fping nethogs sshuttle tcpdump figlet stress iperf iperf3 dnsutils |
3.8配置时钟同步
|
root@ubuntu:~# apt install systemd-timesyncd root@ubuntu:~# timedatectl set-timezone Asia/Shanghai root@ubuntu:~# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime root@ubuntu:~# timedatectl set-local-rtc 1 root@ubuntu:~# timedatectl status |
|
Local time: Wed 2024-10-30 17:08:23 CST Universal time: Wed 2024-10-30 09:08:23 UTC RTC time: Wed 2024-10-30 09:08:23 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: n/a RTC in local TZ: no |
|
root@ubuntu:~# timedatectl set-ntp true root@ubuntu:~# vim /etc/systemd/timesyncd.conf |
|
NTP=ntp.tencent.com FallbackNTP=ntp1.tencent.com,ntp2.tencent.com,ntp3.tencent.com RootDistanceMaxSec=5 PollIntervalMinSec=32 PollIntervalMaxSec=2048 |
|
root@ubuntu:~# systemctl restart systemd-timesyncd root@ubuntu:~# timedatectl show-timesync |
|
SystemNTPServers=ntp.tencent.com FallbackNTPServers=ntp1.tencent.com,ntp2.tencent.com,ntp3.tencent.com ServerName=ntp.tencent.com ServerAddress=106.55.184.199 RootDistanceMaxUSec=5s PollIntervalMinUSec=32s PollIntervalMaxUSec=34min 8s PollIntervalUSec=32s Frequency=0 |
|
root@ubuntu:~# vim /etc/default/locale |
|
LANG=en_US.UTF-8 LC_TIME=en_DK.UTF-8 |
|
root@ubuntu:~# reboot |
3.9优化内核参数
|
root@ubuntu:~# cp /etc/sysctl.conf /etc/sysctl.conf.bak root@ubuntu:~# vim /etc/sysctl.conf |
|
net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_abort_on_overflow = 1 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 66384 4194304 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.core.optmem_max = 81920 net.core.wmem_default = 8388608 net.core.wmem_max = 16777216 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.ipv4.tcp_max_syn_backlog = 1020000 net.core.netdev_max_backlog = 862144 net.core.somaxconn = 262144 net.ipv4.tcp_max_orphans = 327680 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 15 net.ipv4.tcp_keepalive_time = 30 net.ipv4.ip_local_port_range = 1024 65535 net.netfilter.nf_conntrack_tcp_timeout_established = 180 net.netfilter.nf_conntrack_max = 1048576 net.nf_conntrack_max = 1048576 fs.file-max = 655350 |
|
root@ubuntu:~# modprobe nf_conntrack root@ubuntu:~# sysctl -p /etc/sysctl.conf |
|
net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_abort_on_overflow = 1 net.ipv4.tcp_max_tw_buckets = 6000 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_rmem = 4096 87380 4194304 net.ipv4.tcp_wmem = 4096 66384 4194304 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.core.optmem_max = 81920 net.core.wmem_default = 8388608 net.core.wmem_max = 16777216 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.ipv4.tcp_max_syn_backlog = 1020000 net.core.netdev_max_backlog = 862144 net.core.somaxconn = 262144 net.ipv4.tcp_max_orphans = 327680 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 15 net.ipv4.tcp_keepalive_time = 30 net.ipv4.ip_local_port_range = 1024 65535 net.netfilter.nf_conntrack_tcp_timeout_established = 180 net.netfilter.nf_conntrack_max = 1048576 net.nf_conntrack_max = 1048576 fs.file-max = 655350 |
|
root@ubuntu:~# sysctl -w net.ipv4.route.flush=1 |
|
net.ipv4.route.flush = 1 |
3.10垃圾盒历史记录清理
|
root@ubuntu:~# apt-get autoclean |
|
Reading package lists… Done Building dependency tree… Done Reading state information… Done |
|
root@ubuntu:~# apt-get clean root@ubuntu:~# apt-get autoremove |
|
Reading package lists… Done Building dependency tree… Done Reading state information… Done 0 upgraded, 0 newly installed, 0 to remove and 72 not upgraded. |
|
root@ubuntu:~# truncate -s 0 /var/log/btmp root@ubuntu:~# cat /dev/null >/var/log/syslog root@ubuntu:~# cat /dev/null >/var/log/lastlog root@ubuntu:~# cat /dev/null >/var/log/auth.log root@ubuntu:~# cat /dev/null >/var/log/alternatives.log root@ubuntu:~# cat /dev/null >/var/log/bootstrap.log root@ubuntu:~# cat /dev/null >/var/log/apt/history.log root@ubuntu:~# cat /dev/null >/var/log/apt/term.log root@ubuntu:~# cat /dev/null >~/.ssh/known_hosts root@ubuntu:~# cat /dev/null >~/.bash_history root@ubuntu:~# history -c |
|
root@ubuntu:~# cat /dev/null > /etc/machine-id root@ubuntu:~# vim /etc/profile |
|
systemd-machine-id-setup |
3.11添加镜头IP地址模板
|
root@ubuntu:~# vim /etc/netplan/50-cloud-init.yaml |
|
network: renderer: networkd ethernets: eth0: dhcp4: false dhcp6: false addresses: [10.9.254.0/24] routes: – to: default via: 10.9.254.1 nameservers: addresses: [114.114.114.114] search: [] version: 2 |
|
root@ubuntu:~# shutdown -h now |
关机后将虚机转成模板。





