Setting PowerShell as your default Linux shell

In this post we’re going set PowerShell as your default Linux shell.

What is a shell?

In Linux systems you’re given options, tons of options, you can set, reconfigure, add/remove almost anything that you want. And one of those options is your shell. The shell is the thing that you interact with when you’re typing commands at the command line. Different shells have different behaviors and characteristics. It’s a very personal choice. For ages I’ve been a fan of bash.

This week Microsoft announced that they’ve open sourced PowerShell and made it available on Linux and MacOS. Awesome! Want some tips on getting started check out my post here.

PowerShell is…a shell!

One thing that people seem to forget about PowerShell is that it’s…a shell not just a scripting language! So we can use it as our primary command line interface to interact with our system if we choose. This is the key point as to why this is so significant, one shell…many platforms. Don’t forget Microsoft brought bash to Windows too ;)

Changing Your Default Shell

In this post I’m going to show you how to change your default shell on your Linux system. It’s a pretty simple change, Linux systems of course give you several options, chsh is one of them.

If you want to use cash you’re going to need to add /usr/bin/powershell to the list of shells in /etc/shells…so let’s crack open that file and add that line. /etc/shells is a protected file, so you’ll need to elevate your privileges with vi…

sudo vi /etc/shells

Here’s the content of my /etc/shells on a RHEL 7.2after making the edit.

/bin/sh

/bin/bash

/sbin/nologin

/usr/bin/sh

/usr/bin/bash

/usr/sbin/nologin

/usr/bin/powershell


Now with that out of the way we can use chsh to change our shell.

chsh -s /usr/bin/powershell


chch will ask for your password, enter that then log out and back in, and you’ll go right into PowerShell.

mbp:~ aen$ ssh psdemo@172.16.94.150

psdemo@172.16.94.150’s password: 

Last login: Sat Aug 20 11:42:23 2016 from 172.16.94.1

Windows PowerShell 

Copyright (C) 2016 Microsoft Corporation. All rights reserved.

 

PS /home/psdemo> 

What Just Happened?

So what did cash to under the hood? Well, it changed the settings in /etc/passwd for you. A single line of your /etc/passwd file represents one user’s information. In here from left to right, separated by colons, you’ll find username, shadow password, user id, group id, home directory then finally shell. 

Here’s a snippet from mine:

PS /home/aen> Get-Content /etc/passwd                                                                                                                                       

root:x:0:0:root:/root:/bin/bash

demo:x:1000:1000:demo:/home/demo:/bin/bash

psdemo:x:1001:1001:psdemo:/home/psdemo:/usr/bin/powershell


You can see for the user psdemo the shell is now /usr/bin/powershell. Our demo user’s shell is set to /bin/bash. 

If you’re up for it you can edit /etc/passwd directly, to do that you’ll need super user rights. Which you may or may not have. Using chsh you won’t need super user rights. It uses a concept called setuid which allows a user to execute a command with escalated privileges.

If you want to learn more about shells, setuid and command execution on Linux systems check out my Pluralsight course on Understanding and Using Enterprise Linux 7

1 thought on “Setting PowerShell as your default Linux shell

  1. Andrei

    Hello, If you set powershell as default shell you can’t use sudo with cmdlts.
    For exmaple, you can’t create folder in /etc without root privilege:
    New-Item -type Directory -Path /etc/ -Name Test
    sudo doesn’t solve the problem, because sudo doesn’t know new-item command
    sudo New-Item -type Directory -Path /etc/ -Name Test
    changing shell variable doesn’t solve the problem
    sudo SHELL=/usr/bin/pwsh-preview -s New-Item -type Directory -Path /etc/ -Name Test
    there are a lots of way to give additional privilege to new-item, is there way to use sudo instead?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.