Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to the prompt are reverted when running start-ssh-agent.cmd #640

Closed
brainplot opened this issue Nov 2, 2018 · 10 comments
Closed

Changes to the prompt are reverted when running start-ssh-agent.cmd #640

brainplot opened this issue Nov 2, 2018 · 10 comments

Comments

@brainplot
Copy link

@brainplot brainplot commented Nov 2, 2018

System Details

  • posh-git version/path: 1.0.0 beta2 ~\Documents\WindowsPowerShell\Modules\posh-git\1.0.0
  • PowerShell version: 5.1.17134.228
  • git version 2.19.1.windows.1
  • OS: Microsoft Windows NT 10.0.17134.0

Issue Description

Hi, I'm experiencing an issue with posh-git. I'm coming from Linux and this is the first time I try to use Git on Windows (specifically in a Powershell session) so I'm not familiar with how Powershell works, how to configure it and whatnot.
I installed posh-git and managed to make it permanent so that it gets loaded automatically whenever I open a Powershell session. I tried it for a while, making a few commits and it works fantastically, much faster and more responsive than MinTTY. The problems came when I had to push my commits.
I authenticate myself on my remote repository using an SSH key. I saw that Git on Windows comes with a handy Batch script that runs an ssh-agent in the background. Whenever I run it however, the git prompt is gone! The Powershell prompt seems like it's reverted to its default settings. I'll show some screenshots.

This is as soon as I start my Powershell (I've not hit Enter yet):

before

and then as I hit Enter:

after

Interestingly enough though, if I type exit I'll get the Git prompt back (screenshot below), so it looks like start-ssh-agent.cmd runs in a sub-shell, which doesn't get the Git prompt.

after2

@rkeithhill

This comment has been minimized.

Copy link
Collaborator

@rkeithhill rkeithhill commented Nov 2, 2018

Rather than use Git's start-ssh-agent.cmd, I would use the built-in OpenSSH you have on Windows 10 1803. To use this follow these steps:

From a PowerShell window run get-command ssh.exe. You should see that ssh.exe is located at C:\Windows\System\OpenSSH\ssh.exe. If you don't get any result, open Apps & Features section of the Windows 10 Settings app and press the "Manage optional features" link. Once that is open select OpenSSH Client to add that feature. After that has finished, open PowerShell again and see if get-command ssh.exe returns C:\Windows\System\OpenSSH\ssh.exe

Now let's start the ssh-agent service and configure it to automatically start upon reboot. Open PowerShell as administrator and execute:

Get-Service ssh-agent | Set-Service -StartupType Automatic -PassThru | Start-Service

Cd to your $home\.ssh dir and execute ssh-add .\<private-key-file> to add your private key file (and passphrase) to ssh-agent. Last step is to get Git for Windows to use the WIndows native ssh.exe. To do that, execute: git config --global core.sshCommand C:/WINDOWS/System32/OpenSSH/ssh.exe.

That's it! Now ssh-agent is running and serving your passphrase. It will continue to do so after a reboot (without having to re-enter your passphrase). And even better, ssh-agent will work for apps (like Visual Studio) that you start from the Start menu instead of the PowerShell console - which you would have to do it you used start-ssh-agent.cmd.

@brainplot

This comment has been minimized.

Copy link
Author

@brainplot brainplot commented Nov 2, 2018

@rkeithhill thanks for the useful info! Can I start the agent only when I need to? Will this work?

Get-Service ssh-agent | Start-Service

I don't want it to start on every boot, I rarely use Git on Windows.

@rkeithhill

This comment has been minimized.

Copy link
Collaborator

@rkeithhill rkeithhill commented Nov 2, 2018

Yes but you need to do that from PowerShell running elevated. And you can simplify that to Start-Service ssh-agent.

@brainplot

This comment has been minimized.

Copy link
Author

@brainplot brainplot commented Nov 2, 2018

@rkeithhill on Linux though, it works that you need to add your private key to the agent whenever you're going to use it. Your key only gets cached into the agent, you're not adding anything permanently.
Is this different on Windows? You said "without having to re-enter your passphrase".

@rkeithhill

This comment has been minimized.

Copy link
Collaborator

@rkeithhill rkeithhill commented Nov 2, 2018

Yes, it is different in Windows. Windows stores your passphrase (encrypted) using DPAPI (data protection API) - I think. So it is remembered across reboots without you having to re-enter it. That store is local to your machine and is only accessible via your user account on that machine.

@brainplot

This comment has been minimized.

Copy link
Author

@brainplot brainplot commented Nov 3, 2018

The Start-Service thing doesn't work. What am I doing wrong?

C:\Windows\system32> Start-Service ssh-agent
Start-Service : Service 'OpenSSH Authentication Agent (ssh-agent)' cannot be started due to the
following error: Cannot start service ssh-agent on computer '.'.
At line:1 char:1
+ Start-Service ssh-agent
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [S
   tart-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

It's an elevated Powershell session, and ssh should be available:

C:\Windows\system32> Get-Command ssh | Format-List


Name            : ssh.exe
CommandType     : Application
Definition      : C:\Windows\System32\OpenSSH\ssh.exe
Extension       : .exe
Path            : C:\Windows\System32\OpenSSH\ssh.exe
FileVersionInfo : File:             C:\Windows\System32\OpenSSH\ssh.exe
                  InternalName:
                  OriginalFilename:
                  FileVersion:      7.6.0.0
                  FileDescription:
                  Product:          OpenSSH for Windows
                  ProductVersion:   OpenSSH_7.6p1 for Windows
                  Debug:            False
                  Patched:          False
                  PreRelease:       False
                  PrivateBuild:     False
                  SpecialBuild:     False
                  Language:         English (United States)
@rkeithhill

This comment has been minimized.

Copy link
Collaborator

@rkeithhill rkeithhill commented Nov 3, 2018

Make sure you run PowerShell as Adminstrator. On Linux, you'd have to use sudo to start/stop services. Similar on Windows but without sudo.

@brainplot

This comment has been minimized.

Copy link
Author

@brainplot brainplot commented Nov 3, 2018

I solved it! I typed "Services" in the Start Menu and I got a window listing all the services currently installed on the machine. I noticed that, next to "OpenSSH Authentication Agent", it said "Stopped".

I ran Get-Service ssh-agent | Set-Service -StartupType Manual -PassThru and then Start-Service ssh-agent and it worked. It seems like the service was disabled or something. I've never touched any of these things on Windows, maybe it comes as "Stopped" by default.

@rkeithhill

This comment has been minimized.

Copy link
Collaborator

@rkeithhill rkeithhill commented Nov 3, 2018

Ah yeah, by default that service is disabled. So you can't start it until you changed the startup type away from disabled. So manual works as does automatic (and anything else other than disabled).

@rkeithhill

This comment has been minimized.

Copy link
Collaborator

@rkeithhill rkeithhill commented Nov 3, 2018

Closing this issue. If you think it shouldn't be, you can re-open it.

@rkeithhill rkeithhill closed this Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.