Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Nov 12 '11 at 7:07

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '11 at 22:09

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 ]$
share|improve this answer

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.

share|improve this answer
    
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 '11 at 20:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.