This document describes how to connect to Linux virtual machine (VM) instances as the root user, which enables superuser privileges on the VM. By default, Compute Engine VMs built from public images and most common operating systems don't allow root login with a password over SSH.
An alternative to connecting to VMs as the root user is to run commands through
sudo
, which we recommend instead of enabling root login.
Supported operating systems
These connection methods are supported for all public Linux images that are available on Compute Engine. For Fedora CoreOS images, you must set up SSH access before you can use these methods.
Enable root login
By default, Compute Engine VMs, set the PermitRootLogin
parameter to
prohibit-password
or no
in the /etc/ssh/sshd_config
SSH configuration
file. Enable root login by following the instructions for your VM:
Enable root login by doing the following:
Connect to the VM as you usually do.
Change
PermitRootLogin no
toPermitRootLogin prohibit-password
in the/etc/ssh/sshd_config
file, by running the following command:sudo sed -i 's/PermitRootLogin no/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config
Restart the
sshd
daemon by restarting the VM, or running the restart command for your VM's operating system. Wait for the VM to reboot, and then connect as the root user.
Connect as the root user
After you enable root login, connect to the VM as the root user.
gcloud
Connect to VMs as the root user by using the
gcloud compute ssh
command with
root@
specified before the VM name:
-
In the Trusted Cloud console, activate Cloud Shell.
At the bottom of the Trusted Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Connect to the VM by running the following command:
gcloud compute ssh \ --project=PROJECT_ID \ --zone=ZONE \ root@VM_NAME
Replace the following:
PROJECT_ID
: the ID of the project that contains the VMZONE
: the name of the zone in which the VM is locatedVM_NAME
: the name of the VM
Third-party tools
Create an SSH key for the root user. The username for the key must be
root
.Connect using third-party tools and specify the following:
Private key: the private key for the root user
Username: the username must be
root
Troubleshooting
To find methods for diagnosing and resolving failed SSH connections, see Troubleshooting SSH.
What's next
- Learn how to transfer files to VMs.
- Learn how SSH connections to Linux VMs work on Compute Engine.