Powershell: chsh to pwsh for root on ubuntu 18.04.1 results in "Key in dictionary: 'SHELL'" error for all commands

Created on 29 Nov 2018  路  13Comments  路  Source: PowerShell/PowerShell

I wanted to set my default shell for users and root to pwsh. This may just be a rookie ubuntu mistake, is there a different way I should be changing the shell? I imagine there's some reasonable argument about not changing the shell for root, but I'm more comfortable in powershell than bash so I figured I'd try.

Steps to reproduce

  1. Install Ubuntu Server 18.04.1 (in a hyper-v VM for me, but I doubt that matters)
  2. apt update && apt upgrade
  3. Enable universe source: sudo add-apt-repository universe
  4. Install powershell per this link
  5. sudo chsh -s /usr/bin/pwsh for root
  6. chsh -s /usr/bin/pwsh for user
  7. log out
  8. log back in
  9. sudo -i

Expected behavior

For commands to work

Actual behavior

Get this error when running any command as root (ls, ps, etc...)

PS /root> ls
Program 'ls' failed to run: Item has already been added. Key in dictionary: 'SHELL'  Key being added: 'SHELL'At line:1 char:1
+ ls
+ ~~.
At line:1 char:1
+ ls
+ ~~
+ CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed

PS /root> shutdown
Program 'shutdown' failed to run: Item has already been added. Key in dictionary: 'SHELL'  Key being added: 'SHELL'At line:1 char:1
+ shutdown
+ ~~~~~~~~~~~~~~~.
At line:1 char:1
+ shutdown
+ ~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed

Environment data

Name                           Value
----                           -----
PSVersion                      6.1.1
PSEdition                      Core
GitCommitId                    6.1.1
OS                             Linux 4.15.0-39-generic #42-Ubuntu SMP Tue Oct 23 15:48:01 UTC 2018
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question Resolution-Answered

Most helpful comment

I don't have 18.04 around anymore, but just tested powershell-preview on 19.10 and everything worked as expected, changed shell for my user account and root:

echo /snap/powershell-preview/current/opt/powershell/pwsh >> /etc/shells
chsh -s /snap/powershell-preview/current/opt/powershell/pwsh
sudo -i
chsh -s /snap/powershell-preview/current/opt/powershell/pwsh

Logged out, logged back in, sudo -i. Powershell looks good before, during and after sudo.

re: .bashrc and .profile - Yeah, I have a very basic understanding of profiles and .bashrc, .bash_login... I can't remember the specifics now, but when I tried to move some variables from .bashrc to .profile, it was causing issues... maybe around tmux and nesting, I can't remember exactly.

Thanks for the work on this and for the profile explanation, I think I'll revisit setting pwsh as my shell when I get some free time to test it out.

All 13 comments

@mklement0 Could you please share your experience about login shell?

@iSazonov: I'm afraid I have no explanation for these symptoms.

As an aside, @jonstelly: If you run your interactive sudo shell with sudo -s rather than sudo -i, your own shell is used, not the root user's - that way, you needn't change the root user's default shell.

@rjmholt Perhaps it is interesting for you.

I just stood up an environment to investigate this:

> uname -a 
Linux ubuntuUI 4.15.0-72-generic #81-Ubuntu SMP Tue Nov 26 12:20:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
/home/ubuntu
> cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.3 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.3 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

Unfortunately I wasn't able to reproduce the issue with the current master build as either ordinary user or root. It may be that .NET 3.1 has fixed the issues here, so maybe give it another try now?

I guess there was "SHELL" environment variable in both user and root context and Core throwed on this.

Well the SHELL env var is special because it gets set by the login util.

One thing we do is set environment variables at this early point with setenv() rather than the .NET API, because of https://github.com/dotnet/corefx/issues/40130. So it may be that there's a bug in how we're handling that.

@jonstelly if you're seeing this issue still, the way to go is probably to build a debug version of PowerShell like this:

  • git clone https://github.com/PowerShell/PowerShell
  • cd PowerShell
  • ipmo build.psm1 && start-psbootstrap && start-psbuild -clean
  • sudo rm /usr/bin/pwsh (it's just a symlink, if you have non-preview PowerShell installed and want to put it back, you can restore it with sudo ln -s /opt/microsoft/6/pwsh /usr/bin/pwsh)
  • sudo ln -s $PWD/src/powershell-unix/bin/Debug/netcoreapp3.1/linux-x64/publish/pwsh /usr/bin/pwsh
  • Run pwsh and try out native commands

Hopefully in that case you'll at least get a nice stack trace

Oh wait, @jonstelly if you're trying to use PS 6 as a login shell that won't work. Only PS 7 has the required changes to be used as a login shell.

@rjmholt is correct, PS7 (thanks to @rjmholt) was updated to be a more proper login shell. PS6 as a login shell won't work as expected.

Thanks. It's good to hear it's working in PS7. I ended up with something a bit different anyway, there are so many applications that assume bash as a shell, simple additions to the .bashrc that weren't working when using PS as a shell. I also started using tmux so I've changed to using the following at the end of my .bashrc to spin up tmux or pwsh.

I opened another issue here for a problem related to pwsh as a shell and SSH: https://github.com/PowerShell/PowerShell/issues/9842#issuecomment-517736879

It's a bit odd to fire up bash just to set up environment and then launch tmux or pwsh, but it works pretty well in practice, even across SSH to another host configured the same way, etc...

I'm fine if you'd like to close either or both of these issues.

If you're able to test pwsh-preview in your environment it might help to make sure that this is not an ongoing issue.

Also I should note we don't execute .bashrc. On both Linux and macOS we use /bin/sh or another shell in POSIX shell mode, meaning ~/.profile (or /etc/profile) is executed (rather than .bash_login or .bash_profile -- .bashrc is not a login profile so is not executed on login but rather shell process start). This is because the POSIX standard makes this more reliable and consistent across implementations (a number of Ubuntu installations, etc. don't have bash but do have a POSIX shell, like dash, since they must for POSIX compliance).

If you need more configurability there, it's worth opening a feature request. I will say that the login behaviour lies on the critical path for startup, so ideally the implementation can be kept as minimal as possible.

I don't have 18.04 around anymore, but just tested powershell-preview on 19.10 and everything worked as expected, changed shell for my user account and root:

echo /snap/powershell-preview/current/opt/powershell/pwsh >> /etc/shells
chsh -s /snap/powershell-preview/current/opt/powershell/pwsh
sudo -i
chsh -s /snap/powershell-preview/current/opt/powershell/pwsh

Logged out, logged back in, sudo -i. Powershell looks good before, during and after sudo.

re: .bashrc and .profile - Yeah, I have a very basic understanding of profiles and .bashrc, .bash_login... I can't remember the specifics now, but when I tried to move some variables from .bashrc to .profile, it was causing issues... maybe around tmux and nesting, I can't remember exactly.

Thanks for the work on this and for the profile explanation, I think I'll revisit setting pwsh as my shell when I get some free time to test it out.

Was this page helpful?
0 / 5 - 0 ratings