How to install SSH on linux and start the SSH service? How to access the linux server remotely through SSH

1. First install ssh on the linux server

sudo apt-get update
sudo apt-get install openssh-server

2. Check the IP address of the current host

hint

If the Linux system is running in a docker container, you need to perform this step on the host . Directly access the port of the docker host (this port is mapped to port 22 of this container).
For example docker run --name lianxi -d -p 7000:22 -p 7001:5050 nginx
, query the IP address on the host and use the IP address and port 7000 to access the container.

method

ifconfigOrder

if appearsbash: ipconfig: command not found

It may be that the system does not come with ipconfig. In this case, network tools need to be installed.

yum install upgrade
yum install net-tools
sudo apt-get update
sudo apt-get install net-tools

Then you can check the IP address

3. Modify user information

In this step, you can directly set the password of the root account or add a new user.

Change root account password

Change user password

passwd username

If you do not set up other users at this time, then the root account will be logged in through SSH access. In the subsequent steps, you will need:

1.vim /etc/sshd/ssh_config

2. Press I to edit, PermitRootLoginand change the last no toyes

Add a new user

increase user

useradd username

Change user password

passwd username

4. Enable SSHD service

sudo service sshd start

There will be errors here sshd: unrecognized service, and the online solutions are invalid. I simply use them directly. sudo service ssh startNo other problems have been found yet. The specific reasons are unknown.

root@3287b8254440:/# sudo service ssh start
[ ok ] Starting OpenBSD Secure Shell server: sshd.

5. Verify whether the service has been started

service ssh status

root@3287b8254440:/# service ssh status
[ ok ] sshd is running.

ssh service has been started

2022.10.25 addition:
Enter ps -e |grep ssh–> Enter –> sshd, indicating that the ssh service has been started
. If it has not been started, enter “service ssh start” –> Enter –> the ssh service will start.

6. Modify configuration file

First enter the /etc directory. The /etc directory stores some configuration files, such as passwd and other configuration files. If you want to use ssh to log in remotely, you need to configure the configuration information in the /etc/ssh/sshd_config file. Use vim to edit it. In the command Enter in line mode vim /etc/ssh/sshd_config. After entering, press "i" to enter the editing state, find it in the file and modify it to: PasswordAuthentication yes , PermitRootLogin yestwo lines are enough

After the modification is completed, you need to restart the ssh service.

sudo service ssh restart
# service ssh start/stop/restart/status //启动/停止/重启/状态

7. Access via SSH

If it is a container, use the host IP + the host port corresponding to port 22 of the container to access

Otherwise, access through the IP and port 22 queried in step 2.

For example, using XShell software
Insert image description here

Guess you like

Origin blog.csdn.net/Zilong0128/article/details/120450470