Force ssh to prompt for user
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?
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
.
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 ]$
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
$