Direct SSH connection is the recommended way to connect to a DSW instance from a local terminal or VS Code. Direct SSH connections are faster and more stable than ProxyClient connections.
Limitations
-
Unsupported instance types
In public resource groups, instance types whose names start with
ecs.ebmdon't support direct SSH connections. -
Public network connection limits
When you connect to a DSW instance over the public network, PAI automatically configures DNAT rules on each Internet NAT gateway. No manual configuration is required.
Each Internet NAT gateway supports a maximum of 100 DNAT rules. Therefore, a maximum of 100 DSW instances can connect at the same time. For more information about quotas, see Quotas.
Billing
Public network access to a DSW instance requires a NAT gateway, an Elastic IP address (EIP), or an Network Load Balancer (NLB). NAT gateways, EIPs, and NLBs are billed independently. Charges continue to accrue even after the DSW instance is stopped. Delete these resources promptly when you no longer need them.
Implementation steps
Step 1: Enable private DNS resolution
DSW instances are deployed in virtual private clouds (VPCs), and SSH connections rely on private DNS to resolve instance IP addresses. Enable private DNS resolution before you connect to a DSW instance. For more information, see Activate Private DNS.
Step 2: Generate an SSH key pair
Run the following command on your client to generate an SSH key pair. When prompted, set a passphrase for the key pair. If you plan to use remote_ikernel to connect to the instance (see the scenario example in this topic), leave the passphrase empty and press Enter.
Generate the key pair in the default path (recommended)
Run the following command:
ssh-keygen
After the command runs, a public key (id_rsa.pub) and a private key (id_rsa) are generated in the following directories. If an SSH key pair already exists, the existing files are overwritten. The default paths vary by operating system:
Windows: the
C:\Users\<username>\.sshdirectory.Linux: the
/root/.sshdirectory for the root user, or the/home/<username>/.sshdirectory for standard users.macOS: the
/Users/<username>/.sshdirectory.
Generate the key pair in a custom directory
Run the following command:
ssh-keygen -f /your/path/example_rsa # Generates example_rsa.pub and example_rsa in /your/path
Step 3: Configure SSH settings for the DSW instance
When you Create a DSW instance, configure the following network parameters.
For an existing instance, click Change Settings to modify the parameters, and then click OK to apply the changes.
|
Parameter |
Description |
|
VPC Settings, vSwitch, Security Group |
A VPC Settings, a vSwitch, and a Security Group are required to create a DSW instance in a public resource group. You can create these resources by following the steps below. For more information, see Create a VPC and vSwitches and Manage security groups. The VPC and the security group must be in the same region as the DSW instance. Important
Make sure the inbound rules of the security group allow traffic on TCP port 22 from your client IP address. |
|
Enable SSH |
Turn on the switch, and then copy the content of the public key file |
|
Service Access and Port Configuration |
After you turn on the Enable SSH switch, a service named SSH appears.
|
|
Public Network Access |
Configure this parameter if public network access is required. Select DNAT + EIP.
To create a NAT gateway and an EIP, follow these steps: You can also use an NLB instead. When you create the NLB instance, set Instance Network Type to Internet and select the same VPC as the current DSW instance. For more information, see Get started with NLB for IPv4 load balancing. Note
If multiple DSW instances share the same DNAT rule and EIP (or NLB), use different public access ports. |
Step 4: Install the SSH server
Official base images and custom images derived from them have the SSH server installed and started by default.
In the terminal of the DSW instance, run service ssh status to check the service status, or run pgrep sshd to check whether the sshd process exists. If the SSH service isn't running, run the following commands to install the SSH server.
The Ready status of SSH in the console indicates only that the configuration is complete. It doesn't mean that the sshd process is running. Run pgrep sshd to confirm.
sudo apt-get update
sudo apt-get install openssh-server
sudo service ssh start
If the sudo command isn't found, run apt-get install sudo first, and then retry the preceding commands.
On CentOS or Red Hat Enterprise Linux (RHEL) systems, run yum install openssh-server or dnf install openssh-server.
Step 5: Connect to the DSW instance
-
View the access methods.
On the DSW page, click the instance name to go to the instance details page. In the Instance Settings tab, find the Network Settings section and view the public access method and the VPC access method.
NoteIf public access isn't selected in the SSH configuration, only the VPC access method is displayed here.
-
Connect to the DSW instance.
Connect by using SSH
-
Public network access
From your local command line or another terminal, connect to the DSW instance by using the public access method from Step 1.
ssh root@xx.xx.xx.xx -p 1024 # If the private key is not in the default path, specify the key location ssh -i /your/path/example_rsa root@xx.xx.xx.xx -p 1024ImportantIn the preceding commands, the
-pparameter specifies the public access port configured for SSH. In this example, the port is 1024. -
VPC access
From another terminal in the VPC, such as an ECS instance, connect to the DSW instance by using the VPC access method from Step 1.
ssh root@dsw-notebook-xxxx.dsw-xxxx.dsw.pai.alibaba.com -p 22 # If the private key is not in the default path, specify the key location ssh -i /your/path/example_rsa root@dsw-notebook-xxxx.dsw-xxxx.dsw.pai.alibaba.com -p 22
Connect from local VS Code
-
Open VS Code, and install the Remote - SSH extension from the Extensions view.
After the installation is complete, open the Command Palette, enter
remote-ssh, and choose the Remote-SSH: Connect to Host... command to start the remote connection. Click
in the lower-left corner to open the Remote-SSH dialog box, and then select Connect to Host... > Add New SSH Host....-
Enter the SSH connection command.
ssh root@xx.xx.xx.xx -p 1024 # If the private key is not in the default path, specify the key location ssh -i /your/path/example_rsa root@xx.xx.xx.xx -p 1024 Click Open in the lower-right corner. The connection status appears in the lower-left corner.
-
Open a directory in the DSW instance to start remote development and debugging.
Click the Explorer icon in the activity bar on the left, click the Open Folder button, enter
/mnt/workspacein the Open File or Folder dialog box, and then click OK to open the directory. -
To run code, connect to a kernel in the DSW instance.
In the Select a Jupyter Kernel drop-down list at the top of VS Code, select the recommended Python 3 (ipykernel) kernel.
-
Scenario: Run a local notebook on a DSW instance
Prerequisites
Complete Steps 1 through 4 in this topic to configure the direct SSH connection. When you generate the SSH key pair, leave the passphrase empty.
Procedure
-
From your local terminal, install remote_ikernel and add a remote kernel.
pip install remote_ikernelremote_ikernel manage --add \ --kernel_cmd="ipython kernel -f {connection_file}" \ # No changes required --name="Remote Python" \ --interface=ssh \ --host=root@*.*.*.*:1024 # Replace this with your host address. If the private key is not in the default path, use --host="-i /your/path/example_rsa root@*.*.*.*:1024"If the following message appears, the remote kernel is added.
Added kernel ['rik_ssh_root_121_40_*_*_1024_remotepython']: SSH root@121.40.*.*:1024 Remote Python.To view the remote kernel configuration, run the following command:
remote_ikernel manage --show In VS Code on your local machine, open a notebook file and switch the kernel in the upper-right corner. Search for Remote Python and select it.
FAQ
Q: What are the differences between direct SSH connection and ProxyClient proxy?
Q: How do I troubleshoot SSH connection failures, timeouts, or key rejection when I connect to a DSW instance?
Troubleshoot the issue by category:
-
Public network connection failures
Make sure the inbound rules of the security group allow traffic on TCP port 22 from your client IP address.
If multiple Internet NAT gateways exist in the VPC, see Deploy single-AZ NAT gateways for high availability to check the routing relationships among the NAT gateways, vSwitches, and route tables.
If only one NAT gateway exists, use self-service troubleshooting to analyze network connectivity.
-
VPC connection failures
Make sure the client and the DSW instance are in the same VPC. Run the
digcommand to test DNS resolution of the VPC domain name of the DSW instance. IfNXDOMAINis returned, DNS resolution isn't working. Check the private DNS settings of the VPC.If you use a custom image, make sure the SSH service is installed and started. For more information, see Step 4: Install the SSH server.
Make sure the inbound rules of the security group allow traffic on TCP port 22.
Run
ssh -v root@IP -p portto get detailed connection information.Connection timed outindicates a security group or network connectivity issue.Connection refusedindicates that the port isn't listening (sshd isn't running).Connection resetindicates an authentication issue.
-
Key rejection (Permission denied)
Make sure the public key (the entire content of
id_rsa.pub) is added to the SSH configuration of the DSW instance.Make sure the private key used in the SSH command matches the public key. If the private key isn't in the default path (
~/.ssh/id_rsa), specify it by using the-iparameter.Check whether the permissions of the
~/.ssh/authorized_keysfile are 600. Runls -l ~/.ssh/authorized_keysto verify. Incorrect permissions cause SSH to reject the public key.
-
VS Code connection failures
Make sure the
Remote - SSHextension is installed and that you entered a complete SSH command, such asssh root@xx.xx.xx.xx -p 1024.Test the connection by running the SSH command in a local terminal. After the basic connection works, check the VS Code configuration.
If an error about a nonexistent pipe appears, restart VS Code or reinstall the Remote - SSH extension.
Q: Can I use a local IDE such as PyCharm to remotely connect to a DSW instance?
All features based on the SSH protocol are supported. When you connect by using PyCharm, PyCharm automatically downloads and installs the server-side components on the DSW instance. Therefore, the DSW instance must have public network access.
Q: How do I view the public IP address of an instance?
If an Internet NAT gateway is configured, view the public IP address by following these steps:
Q: Does a DSW instance support multiple users with separate SSH logins?
By default, a DSW instance provides only the root user for SSH logins. DSW doesn't support creating multiple independent users with isolated SSH logins. You can manually create additional users within the instance, but this isn't an officially supported practice.
Q: Why can't I see workspace files when I connect to a DSW instance from a local IDE such as VS Code or PyCharm?
After an SSH login, the default directory may be the home directory of the root user (/root). Workspace files are usually stored in a directory on the mounted data disk, such as /mnt/workspace. In VS Code, choose File > Open Folder to open the correct working directory.
References
If this method doesn't fit your scenario or you don't want to configure additional network components, use ProxyClient to connect remotely.
Configure the VPC name, IPv4 CIDR block, and vSwitch information. Keep the other parameters at their default values, and then click OK to create the VPC and the vSwitch.



