After you manually import a virtual disk to Compute Engine, you need to optimize those images so they can use features specific to the Compute Engine environment.
Install the Compute Engine guest environment
You must install the guest environment before you can use key features of Compute Engine. To find out when you need to manually install the guest environment, see when to manually install or update the guest environment.
Install the guest environment on the running VM instance you created after manually importing your existing image. To perform the installation, access the VM instance via SSH with a user account you created before importing it or by interacting with the Serial Console.
Configure your imported image for Compute Engine
You can run your boot disk image in Compute Engine without additional changes, but you can also further optimize the image so that it runs optimally within Compute Engine and has access to all Compute Engine features.
ntp.conffile to include only the
server metadata.google.internal iburstGoogle NTP server entry.
Set the timezone to UTC:
sudo ln -sf /usr/share/zoneinfo/UTC /etc/localtime
To ensure high performance network capability, use the following recommended network configurations:
- Use the ISC DHCP client.
- Set the DHCP MTU to the network MTU. The Compute Engine DHCP
server serves this parameter as the
interface-mtuoption, which most clients respect. For more information about network MTUs, see Maximum transmission unit.
- If you don't plan to configure IPv6 addresses, disable IPv6.
Remove persistent network rules to prevent the instance from remembering MAC addresses. For example:
rm -f /etc/udev/rules.d/70-persistent-net.rules
Disable the operating system firewall unless you have specific requirements not supported by Compute Engine Firewall Rules. Compute Engine provides a firewall for inbound and outbound traffic. For more information about firewalls, see Firewall rules overview.
To ensure high performance network and disk capability, disable or remove the
irqbalancedaemon. This daemon does not correctly balance IRQ requests for the guest operating systems on virtual machine (VM) instances. Instead, use the scripts that are part of the guest environment to correctly balance IRQ settings for virtual CPUs.
Configure SSH access to the base image:
- Disable root ssh login.
- Disable password authentication.
- Disable host-based authentication.
- Enable strict, host-key checking.
ServerAliveIntervalto keep connections open.
Remove SSH keys from your image so that others can't access the public or private keys in your image. Instead, use Compute Engine to manage access to instances.
/etc/ssh/ssh_configfile to use the following configuration:
Host * Protocol 2 ForwardAgent no ForwardX11 no HostbasedAuthentication no StrictHostKeyChecking no Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc Tunnel no # Compute Engine times out connections after 10 minutes of inactivity. # Keep alive ssh connections by sending a packet every 7 minutes. ServerAliveInterval 420
/etc/ssh/sshd_configfile to use the following configuration:
# Disable PasswordAuthentication because ssh keys are more secure. PasswordAuthentication no # Disable root login. Using sudo provides better auditing. PermitRootLogin no PermitTunnel no AllowTcpForwarding yes X11Forwarding no # Compute Engine times out connections after 10 minutes of inactivity. # Keep alive ssh connections by sending a packet every 7 minutes. ClientAliveInterval 420
After you configure and optimize your boot disk on Compute Engine, create an image from that boot disk so that you can create instances from a fully-optimized version of the image rather than having to configure each instance every time you create it.
Configure security best practices
You should always provide a secure operating system environment, but it can be difficult to strike a balance between a secure and accessible environment. Virtual machines that are vulnerable to attack can consume expensive resources. Google strongly recommends that your images comply with the following security best practices:
- Minimize the amount of software installed by default (for example, perform a minimal install of the OS).
- Enable automatic updates.
- By default, disable all network services except for SSH, DHCP, and NTPD. You can allow a mail server, such as Postfix, to run if it is only accepting connections from localhost.
- Do not allow externally listening ports except for sshd.
- Install the denyhosts package to help prevent SSH brute-force login attempts.
- Remove all unnecessary non-user accounts from the default install.
/etc/passwd, set the shell of all non-user accounts to
/usr/sbin/nologin(depending on where your OS installed nologin).
- Configure your OS to use salted SHA512 for passwords in
- Set up and configure pam_cracklib for strong passwords.
- Set up and configure pam_tally to lock out accounts for 5 minutes after 3 failures.
/etc/shadow, configure the root account to be locked by default. Run the following command to lock the root account:
usermod -L root
Deny root in
/etc/ssh/sshd_configby adding the following line:
Use file system capabilities where possible to remove the need for the S*ID bit and to provide more granular control.
Enable compiler and runtime exploit mitigations when compiling network-facing software. For example, here are some of the mitigations that the GNU Compiler Collection (GCC) offers and how to enable them:
- Stack smash protection: Enable this with
-fstack-protector. By default, this option protects functions with a stack-allocated buffer longer than eight bytes. To increase protection by covering functions with buffers of at least four bytes, add
- Address space layout randomization (ASLR):
Enable this by building a position-independent executable with
- Glibc protections: Enable these protections with
- Global Offset Table (GOT) protection: Enable this runtime loader
- Compile-time errors for missing format strings:
-Wformat -Wformat-security -Werror=format-security
- Stack smash protection: Enable this with
CAP_SYS_MODULE, which allows for loading and unloading of kernel modules. This feature is deprecated in the Linux kernel. To disable this feature:
echo 1 > /proc/sys/kernel/modules_disabled
Remove the kernel symbol table:
sudo rm /boot/System.map
- After your image is ready for production, create a final version of that custom image and include the image in an image family so that you can easily manage updated versions of the custom image.
- Learn how to start an instance from an image.