Se connecter à des VM Linux en tant qu'utilisateur racine
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Linux
Ce document explique comment se connecter à des instances de machines virtuelles (VM) Linux en tant qu'utilisateur racine, ce qui active les droits de super-utilisateur sur la VM. Par défaut, les VM Compute Engine créées à partir d'images publiques et les systèmes d'exploitation les plus courants n'autorisent pas la connexion en tant qu'utilisateur racine avec un mot de passe via SSH.
Plutôt que de vous connecter à des VM en tant qu'utilisateur racine, vous pouvez aussi exécuter les commandes via sudo. Nous vous recommandons cette méthode au lieu d'activer la connexion racine.
Systèmes d'exploitation compatibles
Ces méthodes de connexion sont compatibles avec toutes les images Linux publiques disponibles sur Compute Engine. Pour les images Fedora CoreOS, vous devez configurer l'accès SSH afin de pouvoir utiliser ces méthodes.
Activer la connexion racine
Par défaut, sur les VM Compute Engine, définissez le paramètre PermitRootLogin sur prohibit-password ou no dans le fichier de configuration SSH /etc/ssh/sshd_config. Activez la connexion racine en suivant les instructions de votre VM :
VM OS Login
Pour activer la connexion racine, procédez comme suit :
Remplacez PermitRootLogin no par PermitRootLogin prohibit-password dans le fichier /etc/ssh/sshd_config, en exécutant la commande suivante :
sudo sed -i 's/PermitRootLogin no/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config
Créez le répertoire /root/.ssh à l'aide de la commande suivante :
sudo mkdir /root/.ssh
Définissez des autorisations sur le répertoire .ssh en exécutant la commande suivante :
sudo chmod 700 /root/.ssh
Créez le fichier authorized_keys en exécutant la commande suivante :
sudo touch /root/.ssh/authorized_keys
Définissez des autorisations sur le fichier authorized_keys en exécutant la commande suivante :
sudo chmod 600 /root/.ssh/authorized_keys
Collez la clé SSH publique dans le fichier /root/.ssh/authorized_keys.
Redémarrez le daemon sshd en relançant la VM ou en exécutant la commande de redémarrage pour le système d'exploitation de votre VM. Attendez que la VM redémarre, puis connectez-vous en tant qu'utilisateur racine.
VM autres qu'OS Login
Pour activer la connexion racine, procédez comme suit :
Remplacez PermitRootLogin no par PermitRootLogin prohibit-password dans le fichier /etc/ssh/sshd_config, en exécutant la commande suivante :
sudo sed -i 's/PermitRootLogin no/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config
Redémarrez le daemon sshd en relançant la VM ou en exécutant la commande de redémarrage pour le système d'exploitation de votre VM. Attendez que la VM redémarre, puis connectez-vous en tant qu'utilisateur racine.
Se connecter en tant qu'utilisateur racine
Après avoir activé la connexion racine, connectez-vous à la VM en tant qu'utilisateur racine. Si vous vous connectez à une VM sur laquelle OS Login est activé, vous devez utiliser des outils tiers plutôt que la gcloud CLI.
gcloud
Remarque : Vous devez utiliser des outils tiers pour vous connecter en tant qu'utilisateur racine si OS Login est activé sur la VM à laquelle vous vous connectez.
Connectez-vous à des VM en tant qu'utilisateur racine à l'aide de la commande gcloud compute ssh en spécifiant root@ avant le nom de la VM :
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google 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.
Connectez-vous à la VM en exécutant la commande suivante :
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/05/07 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/05/07 (UTC)."],[[["\u003cp\u003eThis document details how to enable and use root login for Linux virtual machines (VMs) on Compute Engine, although using \u003ccode\u003esudo\u003c/code\u003e is recommended as a more secure alternative.\u003c/p\u003e\n"],["\u003cp\u003eBy default, Compute Engine VMs prevent root login via password over SSH, requiring modifications to the \u003ccode\u003e/etc/ssh/sshd_config\u003c/code\u003e file, setting \u003ccode\u003ePermitRootLogin\u003c/code\u003e to \u003ccode\u003eprohibit-password\u003c/code\u003e, and creating and adding keys to the \u003ccode\u003eauthorized_keys\u003c/code\u003e file for root.\u003c/p\u003e\n"],["\u003cp\u003eThe process for enabling root login differs slightly between OS Login and Non-OS Login VMs, with OS Login VMs requiring the creation of an SSH key, while Non-OS Login VMs need to change SSH configuration settings.\u003c/p\u003e\n"],["\u003cp\u003eConnecting as the root user can be done via the \u003ccode\u003egcloud compute ssh\u003c/code\u003e command for VMs that do not have OS Login enabled, or through third-party tools for both OS Login and Non-OS Login VMs by using the root user key.\u003c/p\u003e\n"],["\u003cp\u003eTroubleshooting SSH connections and managing access and file transfers to VMs are also linked, and are relevant to the overall context of connecting to Linux VMs.\u003c/p\u003e\n"]]],[],null,["# Connect to Linux VMs as the root user\n\nLinux\n\n*** ** * ** ***\n\nThis document describes how to connect to Linux virtual machine (VM) instances\nas the root user, which enables superuser privileges on the VM. By default,\nCompute Engine VMs built from [public images](/compute/docs/images#os-details)\nand most common operating systems don't allow root login with a password over\nSSH.\n\nAn alternative to connecting to VMs as the root user is to run commands through\n`sudo`, which we recommend instead of enabling root login.\n\nSupported operating systems\n---------------------------\n\nThese connection methods are supported for all\n[public Linux images](/compute/docs/images/os-details) that are available on\nCompute Engine. For Fedora CoreOS images, you must\n[set up SSH access](https://docs.fedoraproject.org/en-US/fedora-coreos/tutorial-containers/)\nbefore you can use these methods.\n\nEnable root login\n-----------------\n\n| **Note:** We recommend running commands through `sudo` instead of enabling root login.\n\nBy default, Compute Engine VMs, set the `PermitRootLogin` parameter to\n`prohibit-password` or `no` in the `/etc/ssh/sshd_config` SSH configuration\nfile. Enable root login by following the instructions for your VM: \n\n### OS Login VMs\n\nEnable root login by doing the following:\n\n1. [Create an SSH key](/compute/docs/connect/create-ssh-keys). Copy the\n public SSH key for later.\n\n2. [Connect to the VM](/compute/docs/instances/connecting-to-instance) as\n you usually do.\n\n3. Change `PermitRootLogin no` to `PermitRootLogin prohibit-password` in the\n `/etc/ssh/sshd_config` file, by running the following command:\n\n ```\n sudo sed -i 's/PermitRootLogin no/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config\n ```\n4. Create the `/root/.ssh` directory using the following command:\n\n ```\n sudo mkdir /root/.ssh\n ```\n5. Set permissions on the `.ssh` directory, by running the following command:\n\n ```\n sudo chmod 700 /root/.ssh\n ```\n6. Create the `authorized_keys` file by running the following\n command:\n\n ```\n sudo touch /root/.ssh/authorized_keys\n ```\n7. Set permissions on the `authorized_keys` file, by running the following\n command:\n\n ```\n sudo chmod 600 /root/.ssh/authorized_keys\n ```\n8. Paste the public SSH key into the `/root/.ssh/authorized_keys` file.\n\n9. Restart the `sshd` daemon by restarting the VM, or running the restart\n command for your VM's operating system. Wait for the VM to reboot, then\n connect as the root user.\n\n### Non-OS Login VMs\n\nEnable root login by doing the following:\n\n1. [Connect to the VM](/compute/docs/instances/connecting-to-instance) as\n you usually do.\n\n2. Change `PermitRootLogin no` to `PermitRootLogin prohibit-password` in the\n `/etc/ssh/sshd_config` file, by running the following command:\n\n ```\n sudo sed -i 's/PermitRootLogin no/PermitRootLogin prohibit-password/g' /etc/ssh/sshd_config\n ```\n3. Restart the `sshd` daemon by restarting the VM, or running the restart\n command for your VM's operating system. Wait for the VM to reboot, then\n connect as the root user.\n\nConnect as the root user\n------------------------\n\nAfter you enable root login, connect to the VM as the root user. If\nyou're connecting to a VM that has OS Login enabled, you must use third-party\ntools instead of the gcloud CLI. \n\n### gcloud\n\n\nNote: You must use third-party tools to connect as the root user if the VM\nyou're connecting to has OS Login enabled.\n\nConnect to VMs as the root user by using the\n[`gcloud compute ssh` command](/sdk/gcloud/reference/compute/ssh) with\n`root@` specified before the VM name:\n\n1. In the Google Cloud console, activate Cloud Shell.\n\n [Activate Cloud Shell](https://console.cloud.google.com/?cloudshell=true)\n\n\n At the bottom of the Google Cloud console, a\n [Cloud Shell](/shell/docs/how-cloud-shell-works)\n session starts and displays a command-line prompt. Cloud Shell is a shell environment\n with the Google Cloud CLI\n already installed and with values already set for\n your current project. It can take a few seconds for the session to initialize.\n2. Connect to the VM by running the following command:\n\n ```\n gcloud compute ssh \\\n --project=PROJECT_ID \\\n --zone=ZONE \\\n root@VM_NAME\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the ID of the project that contains the VM\n - \u003cvar translate=\"no\"\u003eZONE\u003c/var\u003e: the name of the zone in which the VM is located\n - \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e: the name of the VM\n\n### Third-party tools\n\n\nConnect to VMs as the root user by following the instructions for your VM:\n\n- **OS Login VMs**\n\n [Connect using third-party tools](/compute/docs/connect/ssh-using-third-party-tools)\n and specify the following:\n - **Private key** : the private key that corresponds with the public key that you added to the `authorized_keys` file\n - **Username** : the username must be `root`\n- **Non-OS Login VMs**\n\n 1. [Create an SSH key](/compute/docs/connect/create-ssh-keys) for the\n root user. The username for the key must be `root`.\n\n 2. [Add the key for the root user to metadata](/compute/docs/connect/add-ssh-keys#metadata).\n\n 3. [Connect using third-party tools](/compute/docs/connect/ssh-using-third-party-tools)\n and specify the following:\n\n - **Private key**: the private key for the root user\n\n - **Username** : the username must be `root`\n\nTroubleshooting\n---------------\n\nTo find methods for diagnosing and resolving failed SSH connections, see\n[Troubleshooting SSH](/compute/docs/troubleshooting/troubleshooting-ssh).\n\nWhat's next\n-----------\n\n- Learn how to [manage access to VMs](/compute/docs/instances/access-overview).\n- Learn how to [transfer files to VMs](/compute/docs/instances/transfer-files).\n- Learn how [SSH connections to Linux VMs](/compute/docs/instances/ssh) work on Compute Engine."]]