您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Linux下怎么安装PowerShell
## 前言
PowerShell 作为微软开发的跨平台任务自动化和配置管理框架,已从Windows专属工具演变为支持Linux/macOS的多平台解决方案。本文将详细介绍在Linux系统中安装PowerShell的多种方法,包括直接包管理安装、二进制包安装以及容器化部署方案,并提供完整的配置指南和常见问题解决方案。
---
## 一、系统要求与准备工作
### 1.1 支持的Linux发行版
官方支持的主流发行版包括:
- Ubuntu 16.04+
- Debian 9+
- CentOS/RHEL 7+
- Fedora 30+
- Alpine Linux 3.12+
- Arch Linux
### 1.2 硬件要求
- 最低配置:
- 1GHz处理器
- 512MB内存
- 200MB磁盘空间
- 推荐配置:
- 双核处理器
- 1GB以上内存
- SSD存储
### 1.3 环境检查
```bash
# 检查系统版本
lsb_release -a || cat /etc/*release
# 检查架构
uname -m # x86_64或arm64
# 导入微软GPG密钥
wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
# 更新源并安装
sudo apt update
sudo apt install -y powershell
# 验证安装
pwsh --version
# 注册微软仓库
curl -o /etc/yum.repos.d/microsoft.repo https://packages.microsoft.com/config/rhel/7/prod.repo
# 安装PowerShell
sudo yum install -y powershell
# 或者使用dnf(Fedora/RHEL8+)
sudo dnf install -y powershell
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo zypper addrepo https://packages.microsoft.com/opensuse/15/prod.repo
sudo zypper install -y powershell
从GitHub Releases获取:
# 查看最新版本号
LATEST_VER=$(curl -s https://api.github.com/repos/PowerShell/PowerShell/releases/latest | grep tag_name | cut -d '"' -f 4)
# 下载对应架构的tar包
wget https://github.com/PowerShell/PowerShell/releases/download/$LATEST_VER/powershell-${LATEST_VER:1}-linux-x64.tar.gz
# 创建安装目录
sudo mkdir -p /opt/microsoft/powershell/7
sudo tar -xzf powershell-*.tar.gz -C /opt/microsoft/powershell/7
sudo chmod +x /opt/microsoft/powershell/7/pwsh
# 创建符号链接
sudo ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
pwsh -c "Write-Host 'PowerShell成功安装!' -ForegroundColor Green"
sudo snap install powershell --classic
# 运行最新稳定版
docker run -it mcr.microsoft.com/powershell
# 运行特定版本
docker run -it mcr.microsoft.com/powershell:7.2.5
git clone https://github.com/PowerShell/PowerShell.git
cd PowerShell
./build.sh
# 创建profile文件
if (!(Test-Path -Path $PROFILE)) {
New-Item -ItemType File -Path $PROFILE -Force
}
# 编辑配置文件
code $PROFILE
Install-Module -Name PSReadLine -Force -AllowClobber
Install-Module -Name Az -AllowClobber -Scope CurrentUser
# 添加到合法shell列表
sudo sh -c "echo '/usr/bin/pwsh' >> /etc/shells"
# 修改用户默认shell
chsh -s /usr/bin/pwsh
# Ubuntu下修复依赖
sudo apt install -f
# CentOS下补充依赖
sudo yum install libunwind libicu
# 临时忽略证书验证
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
# 永久解决方案
sudo apt install ca-certificates
sudo sed -i 's/^LogLevel=.*/LogLevel=Critical/' /etc/systemd/journald.conf
Set-PSReadLineOption -PredictionSource History
# Debian/Ubuntu
sudo apt remove powershell
# RHEL/CentOS
sudo yum remove powershell
sudo rm -rf /opt/microsoft/powershell
sudo rm /usr/bin/pwsh
pwsh
和pwsh-preview
区分稳定版和预览版
Update-Module -Force
通过本文介绍的多种安装方式,您可以在任何主流Linux发行版上顺利运行PowerShell。建议生产环境优先选择官方仓库安装方式,开发环境可尝试容器化方案。PowerShell 7+版本在Linux上已实现90%以上的功能兼容性,结合WSL使用效果更佳。
最后更新:2023年10月
参考文档:
- 微软官方安装指南
- GitHub项目主页 “`
这篇文章包含约2900字,采用Markdown格式编写,包含: 1. 8个主要章节 2. 20+个可执行的代码块 3. 结构化排版和层级标题 4. 实际操作的命令序列 5. 故障排除建议 6. 多种安装方式对比 7. 安全性和性能优化建议
可根据具体发行版需求调整命令细节,建议读者根据实际环境选择最适合的安装方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。