12

When you run ssh without a login_name argument, it automatically uses your local username as the login_name. for example:

cogan@localhost$ ssh myserver
cogan@myserver's password: 

Is there a way to force ssh to prompt for login_name?

0

6 Answers 6

12

If you're talking about OpenSSH, the answer is no (you can check the source). As pointed out by @cnicutar, you can use ssh user@host or configure aliases in .ssh/config along the lines of

Host meh
  HostName foo
  User bar

and then ssh meh.

1
  • This is the answer I was looking for. I'm using ssh in a script, so I can just prompt the user for their name myself before making the call, I just wanted to see if there was a way I could have ssh save me the work.
    – Cogan
    Nov 11, 2011 at 22:09
4

Since ssh does not have an option for forcing a username prompt; you can create the following script in your ~/bin directory and name it ssh:

#!/usr/bin/perl

my $user_at_address = $ARGV[0];
my @u_a = split(/@/, $user_at_address);

if (defined $u_a[1])
{
    if ( $^O == 'linux' )
    {
        exec ("/usr/bin/ssh $u_a[0]\@$u_a[1]");
    }
    if ( $^O == 'solaris' )
    {
        exec ("/usr/local/bin/ssh $u_a[0]\@$u_a[1]");
    }
}
else
{
    print "Enter your username: ";
    my $username = <STDIN>;
    chomp ( $username );
    if ( $^O == 'linux' )
    {
        exec ("/usr/bin/ssh $username\@$u_a[0]");
    }
    if ( $^O == 'solaris' )
    {
        exec ("/usr/local/bin/ssh $username\@$u_a[0]");
    }
}

Then, make the script executable:

chmod 755 ~/bin/ssh

Make sure you have $HOME/bin in your PATH (put export PATH=$HOME/bin:$PATH in your ~/.bashrc or /etc/bashrc and source ~/.bashrc or source /etc/bashrc).

Then, run it as you would ssh:

[ 12:49 jon@hozbox ~ ]$ ssh localhost
Enter your username: bob
bob@localhost's password:
[ 12:50 bob@hozbox /home/bob ]$
1

This is an adaptation of @meh's answer.

In a shell script, we prompt the username.

We pass the username in a subsequent call to ssh.

The following runs uptime on contosso.com:

sshhost=contosso.com
read -p "$sshhost user [$USER]: " sshuser
[ "$sshuser" == "" ] && sshuser=$USER
ssh $sshuser@$sshhost uptime

Here's a sample output:

$ ./run-script.sh 
contosso.com user [stephen]: billgates
[email protected] password:
 10:57:12 up 71 days,  1:01,  2 users,  load average: 0.00, 0.04, 0.05
$
0

I don't think so -- if it were, it would be through .sshconfig

http://linux.die.net/man/5/ssh_config

You could try setting User to something illegal to see what it does.

1
  • I just tried this for fun: [ 12:56 Jonathan@MacBookPro ~ ]$ ssh /dev/null@somehost actually works..: /dev/null@somehost's password: -> Permission denied, please try again.. Of course, in the hosts /var/log/secure it shows sshd[21936]: pam_sepermit(sshd:auth): Cannot determine the user's name and sshd[21936]: pam_succeed_if(sshd:auth): error retrieving information about user.
    – chown
    Nov 11, 2011 at 20:57
0

In Windows Batch:

echo off
echo Please type your SSH username.
set /p usern
ssh -t %usern%@serveripaddress
-1

So I was having the same issue. I was using cmd

ssh "IP address"

ssh 000.000.000.0

And it was asking immediately for the password for a user that didn't exist (My computers Name).

really simple fix just use this command instead:

ssh root@"IP address"

ssh [email protected]

You can replace root with any username you want. This will cause the server to prompt you for the password to the username you desire.

1
  • This does not really answer the question. Besides the method you suggest has already been suggested in another answer four years ago.
    – kasperd
    Jan 18, 2016 at 17:11

You must log in to answer this question.