Force ssh to prompt for user

Solution 1:

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.

Solution 2:

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 ]$

Tags:

Ssh