I have two ubuntu computer shared the same wifi and desire to reach another from one's terminal.
First create an user on the second computer named "Second" and hostname "Algorithms"
Then tried to reach it from the other with SSH, but get error report as
$ ssh second@Algorithms
ssh: Could not resolve hostname algorithms: nodename nor servname provided, or not known
The solution I tried is based on networking - Reach other computer from the local - Ask Ubuntu
What's the problem with my experiment?
ssh
within a home Local Area Network (LAN)I assume there are two users each with their own computers running Ubuntu.
user1
usescomp1
anduser2
usescomp2
.The goal is for
user1
to remotely accesscomp2
usinguser2
's account incomp2
.Note: If
user1
has an account incomp2
anduser1
wishes to access her account incomp2
fromcomp1
then she she does not have to specify the username in the ssh command.ssh comp2.local
will work.Setup
ssh
server incomp2
For this you will need to physically go to
comp2
and login to it using an account with administration privileges. I assume that is,user2
account.Install the
ssh
serverDesktop Ubuntu does not come with any servers. To
ssh
intocomp2
you will first need to install theopenssh-server
. Open a terminal incomp2
by pressing Ctrl+Alt+T and enter the following lines one at a time:You will be asked for
user2
's password. When you type the password the cursor will not move and it will seem like nothing is happening. This is normal. Hit Enter after typing the password. Then follow the instructions.Once
openssh-server
is installed you will see a new options in Settings under sharing called "RemoteLogin" and it will be "On":The standard Ubuntu desktop does not come with any firewalls installed. If you have a firewall installed then make sure it allows connections to port 22 from within the LAN. The instructions will depend on the specific firewall software.
Test
ssh
locallyStill at the terminal of
comp2
test thatssh
is working. Enter the command:The
127.0.0.1
refers to the IP address of the computer you are using. In other words, you are trying to ssh fromcomp2
tocomp2
. If all goes well you will be asked if you are sure you want to connect and then for your password. Once you answeryes
for being sure and enter the password foruser2
you will see the terminal prompt change fromuser2@comp2$
to[email protected]$
. This shows that you have successfully sshed fromcomp2
to itself.Note: Since the same user (
user2
) is ssh-ing in this case, you don't need to specifyssh [email protected]
in the ssh command.ssh
fromcomp1
To
ssh
fromcomp1
tocomp2
you can either use the computer name (hostname) or its IP address. To find the IP address ofcomp2
use theifconfig
command in the terminal ofcomp2
. You will see an address like192.168.x.y
, wherex
can be0
or1
andy
can be any number between2
and255
.From a terminal in
comp1
enter either:or
Note: If you use the name of the computer then you must add
.local
at the end. If you use the local IP address, it may change from time to time if a fixed address is not assigned.Security concerns
1. Router setup
Make sure port 22 is not forwarded to any computers in the home router. This will prevent anyone from outside the home LAN use ssh to connect to the home computers.
The instructions are router specific and beyond the scope of this answer as it has nothing to do with Ubuntu.
If you do want set up port forwarding, see How to access home ssh server from outside via the Internet?
2. Enable public key based authentication and disable password based authentication
This is a more secure way to use ssh. It uses a private-public key pair. The private key remains in the trusted computer from which the ssh connection is made. In this case
comp1
. The public key goes tocomp2
. Once the keys are in place, you will disable password based authentication in the ssh server incomp2
. If you disable password based authentication without making sure the key based authentication is working, then ssh will not work, as there will be no way to authenticate the remote user.First generate the private-public key pair in the
user1@comp1
. This will need to be done at each user and each local computer from where you ssh to another computer. In a terminal enter:This generates the newer and more secure key than RSA. If you want RSA type keys, then enter:
The process will prompt you for a passphrase. You can hit Enter if you don't want one. If you do enter a passphrase, you will be asked for it every time you ssh from
comp1
tocomp2
. If you use a passphrase it should not be same as the password used for normal login.Next you will need to copy the public key from
comp1
tocomp2
. In the terminal incomp1
enter:You will be asked to enter the login password of
user2
incomp2
. If you have other computers in the home LAN you want to ssh to from theuser1@comp1
then you need not create a new key-pair. Copy the public key ofuser1@comp1
to the other user accounts in the other remote computers using the above command.Once the public key is successfully copied to the
user2
account ofcomp2
try to ssh again:Now you should be able to get into
comp2
withoutuser2
's password. At this point one can either use the password or the public key you generated to log in. You can test this by creating a new user (or with an existing second user) incomp1
, such asuser1a
. At this stageuser1@comp1
will be able to ssh touser2@comp2
without password using the public key. On the other handuser1a@comp1
will need to use the password ofuser2
to ssh touser2@comp2
.The next step is to disable the password based authentication. You may want to do this locally in a terminal of
comp2
. Use the following command to edit/etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config
Then make sure it contains the following lines and they are uncommented:
Note these lines may not be together. "Uncommented" means there is no
#
in front of each of these lines.Use Ctrl+O to save the changes and Ctrl+X to exit the editor.
Finally, restart the ssh server with the new settings by the following command:
Now
user1@comp1
will still be able to ssh touser2@comp2
without password using the public key. On the other handuser1a@comp1
will get permission denied to ssh touser2@comp2
.3. Install and configure a firewall
There are many firewall software, and some of them are hard to configure. I suggest you install the "uncomplicated firewall" called
ufw
by the command:To open the port 22 but only from within the home LAN use the command:
Note: replace
x
with either0
or1
based on your router setup.This firewall setting in
comp2
stops anyone from outside the home LAN use ssh to connect tocomp2
. However, it allows anyone (using any computer) within the home LAN try to ssh intocomp2
. Ifcomp2
is removed from home and taken somewhere else and connected to another "similar" network, say by WiFi, all the computers in that network will be allowed to access port 22 ofcomp2
by this firewall setting. For this reason I recommend password based authentication to be disabled and private-public key based authentication be used in all computers running the ssh server.Hope this helps
Maybe try to ssh via IP not hostname?
You can check ip using
ifconfig
Add a string to your
/etc/hosts
file like this:For example:
Save
/etc/hosts
and try connecting again.Anyway, as a quick solution, you can always connect directly via IP address: