使用PowerShell操作Ubuntu

Posted by 刘巨 on 2020-08-19

安装PowerShell

安装PowerShell帮助文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb

# Update the list of products
sudo apt-get update

# Enable the "universe" repositories
sudo add-apt-repository universe

# Install PowerShell
sudo apt-get install -y powershell

# Start PowerShell
pwsh

通过 SSH 进行 PowerShell 远程处理

通过 SSH 进行 PowerShell 远程处理

1
2
3
4
5
6
7
8
9
10
11
12
# 安装SSH
sudo apt install openssh-client
sudo apt install openssh-server

# 编辑 /etc/ssh 位置中的 sshd_config 文件。
# 确保已启用密码身份验证:
PasswordAuthentication yes
# 添加 PowerShell 子系统条目:
Subsystem powershell /usr/bin/pwsh -sshs -NoLogo -NoProfile

# 启用密钥身份验证(可选)
PubkeyAuthentication yes

重启 sshd 服务

1
sudo service sshd restart

通过PowerShell 连接 Ubuntu

通过PowerShell 连接 Ubuntu

1
New-PSSession -HostName UserA@LinuxServer01

PowerShell连接Ubuntu

PowerShell使用密钥文件连接Ubuntu

1.先在Windows打开PowerShell输入ssh-keygen.exe生成公/私密钥
PowerShell使用密钥文件连接Ubuntu

1
2
3
4
#生成公/私密钥
#公钥id_rsa.pub
#私钥id_rsa
ssh-keygen.exe

2.把公钥复制到Ubuntu
PowerShell把公钥复制到Ubuntu

1
2
3
4
5
6
7
8
#使用密码登录Ubuntu
$session=New-PSSession -HostName 10.76.20.51 -UserName giant
#复制Windows公钥到Ubunto
Copy-Item "C:\Users\Administrator\id_rsa.pub" -Destination "/home/giant/.ssh/hncsie-liuju.pub" -ToSession $session
#进入Ubuntu Shell
Enter-PSSession -Session $session
cd ./.ssh
ls

3.修改Ubuntu SSH配置
#打开RSA认证
RSAAuthentication yes
#公钥认证
pubkeyAuthentication yes
#Root权限登录
permitROOTlogin yes
#添加公钥文件
AuthorizedKeysFile .ssh/hncsie-liuju.pub

Ubuntu SSH配置

4.重启sshd服务sudo service sshd restart

1
2
3
4
#使用VI编辑sshd配置文件
sudo vi /etc/ssh/sshd_config
#重启sshd
sudo service sshd restart

PowerShell连接Ubuntu

1
2
3
4
#使用私钥文件免密登录Ubuntu
$session=New-PSSession -HostName 10.76.20.51 -UserName giant -KeyFilePath .\id_rsa
#进入Ubuntu shell
Enter-PSSession -Session $session

参考文档

sshd.exe,它是远程所管理的系统上必须运行的 SSH 服务器组件
ssh.exe,它是在用户的本地系统上运行的 SSH 客户端组件
ssh-keygen.exe,为 SSH 生成、管理和转换身份验证密钥
ssh-agent.exe,存储用于公钥身份验证的私钥
ssh-add.exe,将私钥添加到服务器允许的列表中
ssh-keyscan.exe,帮助从许多主机收集公用 SSH 主机密钥
sftp.exe,这是提供安全文件传输协议的服务,通过 SSH 运行
scp.exe 是在 SSH 上运行的文件复制实用工具