在 Azure 应用服务中打开 Linux 容器的 SSH 会话Open an SSH session to a Linux container in Azure App Service

安全外壳 (SSH) 通常用于通过命令行终端以远程方式执行管理命令。Secure Shell (SSH) is commonly used to execute administrative commands remotely from a command-line terminal. Linux 上的应用服务在应用容器中提供 SSH 支持。App Service on Linux provides SSH support into the app container.

Linux 应用服务 SSH

还可以使用 SSH 和 SFTP 直接从本地开发计算机连接到容器。You can also connect to the container directly from your local development machine using SSH and SFTP.

在浏览器中打开 SSH 会话Open SSH session in browser

若要通过容器打开直接的 SSH 会话,应用应该处于正在运行状态。To make open a direct SSH session with your container, your app should be running.

将以下 URL 粘贴到浏览器中,将 <app-name> 替换为应用名称:Paste the following URL into your browser and replace <app-name> with your app name:

https://<app-name>.scm.chinacloudsites.cn/webssh/host

如果尚未进行身份验证,则需通过要连接的 Azure 订阅进行身份验证。If you're not yet authenticated, you're required to authenticate with your Azure subscription to connect. 完成身份验证以后,可以看到一个浏览器内 shell,可以在其中的容器中运行命令。Once authenticated, you see an in-browser shell, where you can run commands inside your container.

SSH 连接

从远程 shell 打开 SSH 会话Open SSH session from remote shell

备注

此功能目前为预览版。This feature is currently in Preview.

使用 TCP 隧道,可以通过经身份验证的 WebSocket 连接在开发计算机与适用于容器的 Web 应用之间创建网络连接。Using TCP tunneling you can create a network connection between your development machine and Web App for Containers over an authenticated WebSocket connection. 这样就可以通过所选客户端使用在应用服务中运行的容器打开一个 SSH 会话。It enables you to open an SSH session with your container running in App Service from the client of your choice.

若要开始,需安装 Azure CLITo get started, you need to install Azure CLI. 了解它在不安装 Azure CLI 的情况下的工作方式。To see how it works without installing Azure CLI.

使用 az webapp remote-connection create 命令打开到应用的远程连接。Open a remote connection to your app using the az webapp remote-connection create command. 为应用指定 <subscription-id>、<group-name> 和 _<app-name>Specify <subscription-id>, <group-name> and _<app-name> for your app.

az webapp create-remote-connection --subscription <subscription-id> --resource-group <resource-group-name> -n <app-name> &

提示

命令末尾的 & 只是为了方便起见(如果你是使用 Cloud Shell)。& at the end of the command is just for convenience if you are using Cloud Shell. 它在后台运行此进程,因此你可以在同一 shell 中运行下一命令。It runs the process in the background so that you can run the next command in the same shell.

命令输出提供的信息是打开 SSH 会话所需的。The command output gives you the information you need to open an SSH session.

Port 21382 is open
SSH is available { username: root, password: Docker! }
Start your favorite client and connect to port 21382

请使用本地端口通过所选客户端打开容器的 SSH 会话。Open an SSH session with your container with the client of your choice, using the local port. 以下示例使用默认的 ssh 命令:The following example uses the default ssh command:

ssh root@127.0.0.1 -p <port>

出现提示时,请键入 yes 继续进行连接。When being prompted, type yes to continue connecting. 然后,系统会提示输入密码。You are then prompted for the password. 请使用 Docker!(此前已向你显示过)。Use Docker!, which was shown to you earlier.

Warning: Permanently added '[127.0.0.1]:21382' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:

进行身份验证以后,会看到会话欢迎屏幕。Once you're authenticated, you should see the session welcome screen.

  _____
  /  _  \ __________ _________   ____
 /  /_\  \___   /  |  \_  __ \_/ __ \
/    |    \/    /|  |  /|  | \/\  ___/
\____|__  /_____ \____/ |__|    \___  >
        \/      \/                  \/
A P P   S E R V I C E   O N   L I N U X

0e690efa93e2:~#

现在已连接到连接器。You are now connected to your connector.

请尝试运行 top 命令。Try running the top command. 应该可以在进程列表中看到应用的进程。You should be able to see your app's process in the process list. 在下面的示例输出中,它带有 PID 263In the example output below, it's the one with PID 263.

Mem: 1578756K used, 127032K free, 8744K shrd, 201592K buff, 341348K cached
CPU:   3% usr   3% sys   0% nic  92% idle   0% io   0% irq   0% sirq
Load average: 0.07 0.04 0.08 4/765 45738
  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
    1     0 root     S     1528   0%   0   0% /sbin/init
  235     1 root     S     632m  38%   0   0% PM2 v2.10.3: God Daemon (/root/.pm2)
  263   235 root     S     630m  38%   0   0% node /home/site/wwwroot/app.js
  482   291 root     S     7368   0%   0   0% sshd: root@pts/0
45513   291 root     S     7356   0%   0   0% sshd: root@pts/1
  291     1 root     S     7324   0%   0   0% /usr/sbin/sshd
  490   482 root     S     1540   0%   0   0% -ash
45539 45513 root     S     1540   0%   0   0% -ash
45678 45539 root     R     1536   0%   0   0% top
45733     1 root     Z        0   0%   0   0% [init]
45734     1 root     Z        0   0%   0   0% [init]
45735     1 root     Z        0   0%   0   0% [init]
45736     1 root     Z        0   0%   0   0% [init]
45737     1 root     Z        0   0%   0   0% [init]
45738     1 root     Z        0   0%   0   0% [init]

后续步骤Next steps

如有问题和疑问,请在 Azure 论坛上发帖。You can post questions and concerns on the Azure forum.

有关用于容器的 Web 应用的详细信息,请参阅:For more information on Web App for Containers, see: