Regarding your actual question:
However, how does ssh handle the possibility of two machines on the local network having the same username? Is there a flag to differentiate between user bob on machine A vs a different user bob on machine B, or does ssh throw an error?
Every Linux system, by default, manages its own users, groups & passwords locally in 3 files.
/etc/password
$ head -4 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
user1:x:1001:1001::/home/user1:/bin/bash
/etc/shadow
$ head -4 /etc/shadow
root:$1$jmJYUghS$AxysSW9MzG6AmmweysZsi1::0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
user1:!!:17717:0:99999:7:::
/etc/group
$ head -4 /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
user1:x:1001:
Within these files are actual numbers. You can see them on this line from the /etc/password
file:
user1:x:1001:1001::/home/user1:/bin/bash
Which equates to the user ID (UID) & group ID (GID) of 1001 & 1001. If we were to list the home directory of this user1
, you'll see these same numbers there as well:
$ ls -lnd /home/user1
drwx------ 4 1001 1001 4096 Jul 12 00:05 /home/user1
NOTE: In the above command we're instructing ls
to display number's it defaults to display names otherwise:
$ ls -ld /home/user1
drwx------ 4 user1 user1 4096 Jul 12 00:05 /home/user1
What about SSH
When you ssh
to another server and you include the user to log in as:
$ ssh user1@server1.mydom.com
you're authenticating against that server's locally stored "details" about a given user. With SSH it's a bit more complicated since you'll typically be using a private/public key pair to log into a server via ssh
.
example key pair files
$ ls -l ~/.ssh/id_rsa*
-rw------- 1 root root 1675 Jul 21 00:50 /root/.ssh/id_rsa
-rw-r--r-- 1 root root 394 Jul 21 00:50 /root/.ssh/id_rsa.pub
In this scenario when you ssh
, you're sending "secrets" to the remote server, using your user's private portion of the SSH key pair, on the server is the public portion of the key pair, which the server can use to confirm that you are in fact who you say you are.
Here, for example is your private key portion:
my laptop
$ ssh-keygen -l -f ~/.ssh/id_rsa
2048 SHA256:HvNN38AS9H7tIHaaE+51lqv6bgL7AmyDdqHIZIyBimg root@centos7 (RSA)
If we look to a client where this user is able to ssh
into using a key pair:
remote server X
$ ssh-keygen -l -f ~user1/.ssh/authorized_keys
2048 SHA256:HvNN38AS9H7tIHaaE+51lqv6bgL7AmyDdqHIZIyBimg root@centos7 (RSA)
Notice the keys match.
example.com
host part would be different, no?ssh
can default it: e.g. if you are logged-in tothis
asalice
, thenssh that
(nouser@
) attempts to connect tothat
and log in asalice
there. (And the same forscp
andsftp
, which usessh
, and sometimes other things likersync
andgit
etc.)