Siapkan autentikasi, jika Anda belum melakukannya.
Autentikasi memverifikasi identitas Anda untuk mengakses Google Cloud layanan dan API. Untuk menjalankan
kode atau sampel dari lingkungan pengembangan lokal, Anda dapat melakukan autentikasi ke
Compute Engine dengan memilih salah satu opsi berikut:
Menginstal Google Cloud CLI.
Setelah penginstalan,
lakukan inisialisasi Google Cloud CLI dengan menjalankan perintah berikut:
Untuk mendapatkan izin yang Anda perlukan guna menyalin VM antar-project, minta administrator untuk memberi Anda peran IAM Compute Instance Admin (v1) (roles/compute.instanceAdmin.v1) di project.
Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.
Peran bawaan ini berisi
izin yang diperlukan untuk menyalin VM antarproject. Untuk melihat izin yang benar-benar diperlukan, luaskan bagian Izin yang diperlukan:
Izin yang diperlukan
Izin berikut diperlukan untuk menyalin VM antar-project:
compute.instances.create
di project
Untuk menggunakan image kustom guna membuat VM:
compute.images.useReadOnly
pada image
Untuk menggunakan snapshot guna membuat VM:
compute.snapshots.useReadOnly
pada snapshot
Untuk menggunakan template instance guna membuat VM:
compute.instanceTemplates.useReadOnly
pada template instance
Untuk menetapkan jaringan lama ke VM:
compute.networks.use
pada project
Untuk menentukan alamat IP statis untuk VM:
compute.addresses.use
pada project
Untuk menetapkan alamat IP eksternal ke VM saat menggunakan jaringan lama:
compute.networks.useExternalIp
di project
Untuk menentukan subnet bagi VM Anda:
compute.subnetworks.use
di project atau di subnet yang dipilih
Untuk menetapkan alamat IP eksternal ke VM saat menggunakan jaringan VPC:
compute.subnetworks.useExternalIp
di project atau di subnet yang dipilih
Untuk menetapkan metadata instance VM bagi VM:
compute.instances.setMetadata
di project
Untuk menetapkan tag untuk VM:
compute.instances.setTags
di VM
Untuk menetapkan label bagi VM:
compute.instances.setLabels
di VM
Untuk menyetel akun layanan yang akan digunakan VM:
compute.instances.setServiceAccount
di VM
Untuk membuat disk baru untuk VM:
compute.disks.create
pada project
Untuk memasang disk yang ada dalam mode hanya baca atau baca-tulis:
compute.disks.use
pada disk
Untuk memasang disk yang ada dalam mode hanya baca:
compute.disks.useReadOnly
pada disk
SOURCE_DISK: Nama volume Persistent Disk zona tempat Anda ingin membuat snapshot.
SNAPSHOT_TYPE: Jenis snapshot, baik STANDARD atau
ARCHIVE. Jika jenis snapshot tidak ditentukan, snapshot STANDARD akan dibuat. Pilih Archive untuk retensi data yang lebih hemat biaya.
SOURCE_DISK_ZONE: Zona volume Persistent Disk zona tempat Anda ingin membuat snapshot.
Boot disk regional
Jika VM Anda memiliki boot disk regional, buat snapshot menggunakan perintah berikut:
SOURCE_SNAPSHOT: snapshot tempat Anda ingin membuat image
LOCATION: Opsional: tanda yang memungkinkan Anda menentukan
region atau multi-region tempat image Anda disimpan. Misalnya, tentukan us untuk menyimpan image di multi-region us, atau us-central1 untuk menyimpannya di region us-central1. Jika Anda tidak menentukan pilihan, Compute Engine akan menyimpan image dalam multi-region yang paling dekat dengan lokasi sumber image Anda.
Opsional: Bagikan image kustom dengan pengguna yang membuat VM di project tujuan. Untuk mengetahui informasi selengkapnya tentang berbagi image kustom, lihat Membagikan image kustom dalam organisasi.
Di project tujuan Anda, buat VM dari image kustom menggunakan perintah berikut:
IMAGE_PROJECT: ID Google Cloud project yang
berisi gambar
IMAGE_FLAG: tentukan salah satu dari berikut ini:
Gunakan flag --image IMAGE_NAME untuk menentukan image kustom.
Misalnya, --image my-debian-image-v2.
Jika Anda membuat image kustom
sebagai bagian dari kelompok image kustom,
gunakan flag --image-family IMAGE_FAMILY_NAME untuk
menentukan kelompok image kustom tersebut.
Ini akan membuat VM dari
OS image dan versi OS terbaru yang masih digunakan dalam kelompok image kustom Anda.
Misalnya, jika Anda menentukan --image-family my-debian-family,
Compute Engine akan membuat VM dari OS image terbaru dalam kelompok image
my-debian-family kustom Anda.
SUBNET: jika subnet dan instance berada dalam project yang sama, ganti SUBNET dengan nama subnet yang berada di region yang sama dengan instance.
Untuk menentukan subnet di jaringan VPC Bersama, ganti
SUBNET dengan string bentuk:
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-04 UTC."],[[["\u003cp\u003eThis guide explains how to copy a virtual machine (VM) from one Google Cloud project to another, utilizing snapshots and custom images.\u003c/p\u003e\n"],["\u003cp\u003eBefore copying, you must ensure your boot disk is prepared for snapshots and have the correct authentication set up with the Google Cloud CLI.\u003c/p\u003e\n"],["\u003cp\u003eCopying a VM involves creating a snapshot of the VM's boot disk, creating a custom image from this snapshot, and then creating a new VM in the destination project from that image.\u003c/p\u003e\n"],["\u003cp\u003eSpecific IAM permissions, such as \u003ccode\u003ecompute.instances.create\u003c/code\u003e and \u003ccode\u003ecompute.snapshots.useReadOnly\u003c/code\u003e, are required to perform the VM copy, and these can be granted through the Compute Instance Admin (v1) role.\u003c/p\u003e\n"],["\u003cp\u003eThe guide outlines commands using the Google Cloud CLI for both zonal and regional boot disks to create snapshots and custom images, and to create the new VM.\u003c/p\u003e\n"]]],[],null,["# Copying VMs between projects\n\nLinux Windows\n\n*** ** * ** ***\n\nThis document describes how to copy your VM to a different project.\n\nBefore you begin\n----------------\n\n- Review [Best practices for persistent disk snapshots](/compute/docs/disks/snapshot-best-practices#prepare_for_consistency) and prepare your boot disk for snapshots.\n- If you haven't already, set up [authentication](/compute/docs/authentication). Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:\n 1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n | **Note:** If you installed the gcloud CLI previously, make sure you have the latest version by running `gcloud components update`.\n 2. [Set a default region and zone](/compute/docs/gcloud-compute#set_default_zone_and_region_in_your_local_client).\n\n### Required roles\n\n\nTo get the permissions that\nyou need to copy VM between projects,\n\nask your administrator to grant you the\n\n\n[Compute Instance Admin (v1)](/iam/docs/roles-permissions/compute#compute.instanceAdmin.v1) (`roles/compute.instanceAdmin.v1`)\nIAM role on the project.\n\n\nFor more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\nThis predefined role contains\n\nthe permissions required to copy VM between projects. To see the exact permissions that are\nrequired, expand the **Required permissions** section:\n\n\n#### Required permissions\n\nThe following permissions are required to copy VM between projects:\n\n- ` compute.instances.create` on the project\n- To use a custom image to create the VM: ` compute.images.useReadOnly` on the image\n- To use a snapshot to create the VM: ` compute.snapshots.useReadOnly` on the snapshot\n- To use an instance template to create the VM: ` compute.instanceTemplates.useReadOnly` on the instance template\n- To assign a [legacy network](/vpc/docs/legacy) to the VM: ` compute.networks.use` on the project\n- To specify a static IP address for the VM: ` compute.addresses.use` on the project\n- To assign an external IP address to the VM when using a legacy network: ` compute.networks.useExternalIp` on the project\n- To specify a subnet for your VM: ` compute.subnetworks.use` on the project or on the chosen subnet\n- To assign an external IP address to the VM when using a VPC network: ` compute.subnetworks.useExternalIp` on the project or on the chosen subnet\n- To set VM instance metadata for the VM: ` compute.instances.setMetadata` on the project\n- To set tags for the VM: ` compute.instances.setTags` on the VM\n- To set labels for the VM: ` compute.instances.setLabels` on the VM\n- To set a service account for the VM to use: ` compute.instances.setServiceAccount` on the VM\n- To create a new disk for the VM: ` compute.disks.create` on the project\n- To attach an existing disk in read-only or read-write mode: ` compute.disks.use` on the disk\n- To attach an existing disk in read-only mode: ` compute.disks.useReadOnly` on the disk\n\n\nYou might also be able to get\nthese permissions\nwith [custom roles](/iam/docs/creating-custom-roles) or\nother [predefined roles](/iam/docs/roles-overview#predefined).\n\nCopy a VM to another project\n----------------------------\n\n1. In your source project, create a snapshot of the VM's boot disk, using one\n of the following commands:\n\n ### Zonal boot disk\n\n If your VM has a zonal boot disk, create a snapshot using the following\n command:\n\n\n ```\n gcloud compute snapshots create SNAPSHOT_NAME \\\n --source-disk SOURCE_DISK \\\n --snapshot-type SNAPSHOT_TYPE \\\n --source-disk-zone SOURCE_DISK_ZONE\n ```\n\n \u003cbr /\u003e\n\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eSNAPSHOT_NAME\u003c/var\u003e: A name for the snapshot.\n - \u003cvar translate=\"no\"\u003eSOURCE_DISK\u003c/var\u003e: The name of the zonal Persistent Disk volume from which you want to create a snapshot.\n - \u003cvar translate=\"no\"\u003eSNAPSHOT_TYPE\u003c/var\u003e: The snapshot type, either **STANDARD** or **ARCHIVE** . If a snapshot type is not specified, a **STANDARD** snapshot is created. Choose Archive for more cost-efficient data retention.\n - \u003cvar translate=\"no\"\u003eSOURCE_DISK_ZONE\u003c/var\u003e: The zone of the zonal Persistent Disk volume from which you want to create a snapshot.\n\n \u003cbr /\u003e\n\n ### Regional boot disk\n\n If your VM has a regional boot disk, create a snapshot using the\n following command:\n\n\n ```\n gcloud compute snapshots create SNAPSHOT_NAME \\\n --source-disk SOURCE_DISK \\\n --source-disk-region=SOURCE_DISK_REGION \\\n --snapshot-type=SNAPSHOT_TYPE\n ```\n\n \u003cbr /\u003e\n\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eSNAPSHOT_NAME\u003c/var\u003e: A name for the snapshot.\n - \u003cvar translate=\"no\"\u003eSOURCE_DISK\u003c/var\u003e: The name of the regional Persistent Disk volume from which you want to create a snapshot.\n - \u003cvar translate=\"no\"\u003eSOURCE_DISK_REGION\u003c/var\u003e: The region of the regional Persistent Disk volume from which you want to create a snapshot.\n - \u003cvar translate=\"no\"\u003eSNAPSHOT_TYPE\u003c/var\u003e: The snapshot type, either **STANDARD** or **ARCHIVE** . If a snapshot type is not specified, a **STANDARD** snapshot is created.\n\n \u003cbr /\u003e\n\n2. Create a custom image from the snapshot using the following command:\n\n ```\n gcloud compute images create IMAGE_NAME \\\n --source-snapshot=SOURCE_SNAPSHOT \\\n [--storage-location=LOCATION]\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eIMAGE_NAME\u003c/var\u003e: a name for the new image\n - \u003cvar translate=\"no\"\u003eSOURCE_SNAPSHOT\u003c/var\u003e: the snapshot from which you want to create the image\n - \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e: Optional: a flag that lets you designate the region or multi-region where your image is stored. For example, specify `us` to store the image in the `us` multi-region, or `us-central1` to store it in the `us-central1` region. If you don't make a selection, Compute Engine stores the image in the multi-region closest to your image's source location.\n3. Optional: Share the custom image with users who create VMs in the\n destination project. For more information about sharing custom images, see\n [Sharing custom image within an organization](/compute/docs/images/managing-access-custom-images#share-images-within-organization).\n\n4. In your destination project, create a VM from the custom image using the\n following command:\n\n\n ```\n gcloud compute instances create VM_NAME \\\n --image-project IMAGE_PROJECT \\\n IMAGE_FLAG \\\n --subnet SUBNET\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eVM_NAME\u003c/var\u003e: the name of the VM\n - \u003cvar translate=\"no\"\u003eIMAGE_PROJECT\u003c/var\u003e: the ID of the Google Cloud project that contains the image\n - \u003cvar translate=\"no\"\u003eIMAGE_FLAG\u003c/var\u003e: specify one of the following:\n - Use the `--image `\u003cvar translate=\"no\"\u003eIMAGE_NAME\u003c/var\u003e flag to specify a custom image.\n\n For example, `--image my-debian-image-v2`.\n - If you created your custom images as part of a [custom image family](/compute/docs/images#custom-families), use the `--image-family `\u003cvar translate=\"no\"\u003eIMAGE_FAMILY_NAME\u003c/var\u003e flag to specify that custom image family.\n\n This creates the VM from the most\n recent, non-deprecated OS image and OS version in your custom image family.\n For example, if you specify `--image-family my-debian-family`,\n Compute Engine creates a VM from the latest OS image in your custom\n `my-debian-family` image family.\n\n | **Note:** Compute Engine uses the default image family and project if you don't specify an image, for example `debian-10` and `debian-cloud`, respectively.\n - \u003cvar translate=\"no\"\u003eSUBNET\u003c/var\u003e: if the subnet and instance are in the same\n project, replace \u003cvar translate=\"no\"\u003eSUBNET\u003c/var\u003e with the name of a subnet that is in the same\n region as the instance.\n\n To specify a subnet in a Shared VPC network, replace\n \u003cvar translate=\"no\"\u003eSUBNET\u003c/var\u003e with a string of the form: \n\n ```\n projects/HOST_PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME\u003cvar translate=\"no\"\u003e\n \u003c/var\u003e\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eHOST_PROJECT_ID\u003c/var\u003e: the project ID of the Shared VPC host project\n - \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: the region of the subnet\n - \u003cvar translate=\"no\"\u003eSUBNET_NAME\u003c/var\u003e: the name of the subnet\n\n The region of the subnet for a Shared VPC network must also match the\n region containing the instance.\n\n \u003cbr /\u003e\n\nWhat's next\n-----------\n\n- Customize the destination project's [VPC network](/vpc/docs/vpc)."]]