[LINUX] Log in to the remote server with SSH

Outputs what IT inexperienced school students have learned: neutral_face: This is my first post. Thank you.

What is SSH

A protocol that securely communicates with a remote computer using encryption and authentication technology when you want to work in a remote server (remote server).

Benefits of SSH

・ You can operate safely while avoiding the risk of eavesdropping on communications. -File operations and configuration file editing can be performed on the server.

Login with SSH

You can log in using two methods: password authentication and public key authentication.

In order to use SSH, it is necessary to install software called SSH server remotely and SSH client locally. However, it is installed by default on Linux servers and Mac OS, so there is no need to install it.

$ ssh [Login user name]@[IP address]

When you log in for the first time, you will be asked if you really want to connect, so enter yes Next you will be asked to enter the password, so you can log in by entering the password

When logging in to a Linux machine (192.168.2.67) for the first time with ssh
 macmini: ~ user1 $ ssh [email protected]
The authenticity of host '192.168.2.67(192.168.2.67)' can't be established.
RSA key fingerprint is c3 : e3 : 8d :5e ea :58 : 0e :a9 :  e5 : 03 : 34 : 56 : b3 : ca : a3 : 60.
Are you sure you want to continue connecting (yes, no)?yes ← enter yes
Warning : Permanently added '192.168.2.67'(RSA) to the list of known hosts.
[email protected]'s passward:← Enter password
Last login: Sat Aug 23 18:34:30 2014 from 192.168.2.67 
[hinako@localhost ~]$

Private key and public key

It is not good for security that an unspecified number of people can enter the server Restrict who can create two files, *** private key *** and *** public key ***, on the server The public key will be sent with you when you start SSHing to the server. Store the private key in your local environment. The public key generates the cipher, and in the local environment, the cipher sent from the server is decrypted with the private key. In this way, the exchange of information between the server and the local environment is secure.

Login with public key

procedure

  1. Generate public and private keys (local environment)
  2. Install the public key on the server side (local environment → server)
  3. Check if you can log in with public key authentication
1. Generate public and private keys
~ $ cd .ssh # .Move to ssh directory
.ssh $ ssh-keygen -t rsa  #Create public and private keys

After entering, the following will be displayed, and enter the private key storage location and the password to be set for the private key.

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/hogehoge/.ssh/id_rsa): [Where to put the private key] 
Enter passphrase (empty for no passphrase): [password]
Enter same passphrase again: [password]

The public key is stored in *** ~ / .ssh / id_rsa.pub *** and the private key is stored in *** ~ / .ssh / id_rsa *** Enter the following command, and if the output result contains the following two, the creation is successful.

$ ls
=> [Key name]_key_rsa [Key name]_key_rsa.pub

Use the cat command to check the contents of the public key to see if the public and private keys have been created.

#Check the contents of the public key
$ cat [Public key]_key_rsa.pub

#Check the contents of the private key
$ cat [Private key]_key_rsa

When I run the command, a complex string is displayed on the terminal. This is the contents of the public and private keys you created earlier.

2. Install the public key on the server side

-Copy the public key file of the local environment to the remote server

Use the scp command to copy the public key remotely to the server *** scp command *** A command that allows you to specify the destination directory and send a file

$ scp ~/.ssh/id_rsa.pub [username]@ [IP address]:~/.ssh
$ ssh -i [The name of the downloaded key].pem ec2-user@[Elastic IP associated with the created EC2 instance]

-Add the contents of the public key file of the local environment to the public key file of the remote server.

Until now, in the local environment, the public key was saved in id_rsa.pub, but in the remote server, the public key is saved in ~ / .ssh / authorized_keys.

#Log in to the remote server
$ ssh [username]@ [IP address]

# .Move to ssh directory
$ cd ~/.ssh  

#Remote server public key file (~/.ssh/authorized_create keys)
touch .ssh/authorized_keys

#Add the contents of the public key
$ cat ~/id_rsa.pub >> authorized_keys

Finally, set permissions for the .ssh directory and the authorized_keys file. Set all read / write / execute permissions in the .ssh directory and read / write permissions in the authorized_keys file.

# .ssh directory and authorized_Set permissions for keys file
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys
3. Check if you can log in with public key authentication
#Log in to the remote server
$ ssh [username]@ [IP address]

After typing the command, the following display will be displayed. (1) Enter the password described in the #Enter password to set the public key and private key to the generated # private key. After that, if [User name] @ [IP address]% is displayed, login is successful: v:

Enter passphrase for key '~/.ssh/id_rsa':← Enter password
[username]@ [IP address]%← Successful login to SSH server

Recommended Posts

Log in to the remote server with SSH
screen and split screen with python and ssh login to remote server
I can't log in to the admin page with Django3
Log in to Raspberry PI with ssh without password (key authentication)
Log in to Yahoo Business with Selenium Python
Easily log in to AWS with multiple accounts
Use pygogo to get the log in json.
SSH login to the target server from Windows with a click of a shortcut
Send log data from the server to Splunk Cloud
Convert the image in .zip to PDF with Python
[Simple procedure] To log in to ssh without a password
Ssh to virtual environment with remote development of vscode
Describe the multi-stage ssh destination in the config, log in easily, and copy the file with scp
Log in to the fortigate (6.0) management screen from selenium-try to log out
[Postgresql] SSH connection to the external DB server from the client
How to manipulate the DOM in an iframe with Selenium
How to log in automatically like 1Password from the CLI
[Shell] How to get the remote default branch in Git
How to log in to AtCoder with Python and submit automatically
I tried to output the access log to the server using Node.js
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
Cython to try in the shortest
How to log in to Docker + NGINX
Programming to fight in the world ~ 5-2
I tried to describe the traffic in real time with WebSocket
Automatically acquire the operation log in the terminal when logging in to Linux
Output the contents of ~ .xlsx in the folder to HTML with Python
I tried to process the image in "sketch style" with OpenCV
Hit the New Relic API in Python to get the server status
Synchronizing with the server port forwarding ssh on localhost fails [Resolved]
I tried to process the image in "pencil style" with OpenCV
I tried to log in to twitter automatically with selenium (RPA, scraping)
A story that didn't work when I tried to log in with the Python requests module
I tried to build an environment where work in the Docker container on the remote server can be done directly from the local VS Code with SSH connection
Operate Paints Chainer with Selenium to automatically color images in the directory
Access the Docker Remote API with Requests
Load the remote Python SDK in IntelliJ
Try logging in to qiita with Python
Start the web server in the current directory
Behavior when returning in the with block
I tried to communicate with a remote server by Socket communication with Python.
Connect to VPN with your smartphone and turn off / on the server
How to set the server time to Japanese time
Output Python log to console with GAE
How to get the date and time difference in seconds with python
Display Python 3 in the browser with MAMP
How to work with BigQuery in Python
Log in to Slack using requests in Python
Edit the file of the SSH connection destination server on the server with VS Code
In Jupyter, add IPerl to the kernel.
Connect to GNU / Linux with Remote Desktop
Connect with mysql.connector with ssh tunnel in Python 3.7
What to do if the server doesn't start with python manage.py runserver
Various comments to write in the program
To work with timestamp stations in Python
Match the colorbar to the figure with matplotlib