使用ssh远程链接你的linux Virtual Machine

This blog is forwarded by my personal blog TaylorLiang’s Blog. You are very welcome to visit and leave some comments on my blog.

Linux/Unix are built to be used in multi-user environments where several users are logged in to the same machine at the same time. To make work more efficient, we offen connect with our linux machine remotely. However, linux inside your virtual machine can not be connected by ssh by defauct. Some settings need to be done manually by yourself. Here is how I get connection with my virtual machine (virtual box) using ssh.

Get ip address

Use command ifconfig, you will get the results as follow:

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::6c6a:c992:612d:208b  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:6c:50:b0  txqueuelen 1000  (Ethernet)
        RX packets 960  bytes 780475 (780.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 518  bytes 51549 (51.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 92  bytes 7513 (7.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 92  bytes 7513 (7.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As you can see above, ‘10.0.2.15’ is your VM’s ip address. If you cannot get your ip address using ifconfig, please install ifconfig first using command sudo apt-get ifconfig.

Make sure you hava access to the internet. Use ping to test internet access.

Settings on virtual box

Machine -> Settings -> Network->Port Forwarding, new a port forwarding rule, set host ip to 127.0.0.1, host port 22222, guest ip 10.0.2.15, guest port 22, then click OK on the button right. here is my virtual box setting screen shot.
virtual box setting

Install openssh-server

Before installing openssh-server, you can also update your apt-get using command sudo apt-get update.
Then install openssh-server with sudo apt-get install openssh-server

Allow port 22 in ufw

sudo ufw allow 22
Check if port 22 has been used for sshd by command sudo ufw verbose. If so, the status should be ‘active’

Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       Anywhere                  
22 (v6)                    ALLOW       Anywhere (v6) 

If you get ‘Status: inactive’, try to enable your ufw by typing sudo ufw enable.

Connect with ssh

Here comes to the final goal. Use command ssh -p 2222 [email protected]. Please notice that taylor is my username of my linux machine, you should replace by our own username.

The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
ECDSA key fingerprint is SHA256:KhLmGgmnjhncCiI2ZpRC/jXG2ygMajTyMr2e8DMicxE.
Are you sure you want to continue connecting (yes/no)? yes

For the first-time connection, you will be asked if you want to continue connecting or not, of course we should say ‘yes’. Then you will be asked for password, just type in the password, and then you are good to go.

After finishing your job on your linux machine, you can type exit to logout.

References
StackExchange - Trying to SSH to local VM Ubuntu with Putty
StackExchange - why Ubuntu Firewall not showing verbose?
StackExchange - Why am I getting a “port 22: Connection refused” error?

猜你喜欢

转载自blog.csdn.net/weixin_41002844/article/details/86592911