728x90

source : https://unsplash.com/ko/%EC%82%AC%EC%A7%84/4Mw7nkQDByk

SSH 

Secure Shell (SSH) is a protocol that provides secure communication between two computers. It allows you to connect to a remote server securely and remotely execute commands on it. SSH is widely used by system administrators and developers to manage servers, transfer files, and execute remote commands.

In this article, we'll cover some of the basic SSH shell commands you can use to interact with a remote server.

Connect to a remote server

ssh username@remote_server_ip

Replace username with the username of the remote server and remote_server_ip with the IP address of the remote server. If the remote server uses a different port than the default SSH port 22, you can specify it using the -p option:

ssh -p port_number username@remote_server_ip

Connect to the remote server using the SSH key file :

ssh -i /path/to/private_key_file username@remote_server

SSH Config

Creating an SSH Config File

The SSH configuration file is located in your home directory under the .ssh directory. If the file does not exist, you can create it by typing the following command in your terminal:

touch ~/.ssh/config

SSH Config Options

The SSH configuration file allows you to set default options for connecting to remote servers. Here are some of the most commonly used options:

 

Host
The Host option allows you to define a nickname for the remote server. This can be any name you choose, and it can be used as a shortcut when connecting to the server. For example:

Host myserver
    HostName 192.168.1.100
    User username
    IdentityFile ~/.ssh/myprivatekey

In this example, we define myserver as the nickname for the remote server at IP address 192.168.1.100. We also specify the username and private key file to use when connecting to the server.

 

Port
The Port option allows you to specify the port number for the SSH connection. By default, SSH uses port 22, but you can specify a different port number if needed. For example:

Host myserver
    HostName 192.168.1.100
    User username
    IdentityFile ~/.ssh/myprivatekey
    Port 2222

In this example, we specify port number 2222 for the SSH connection.

 

User 
The User option allows you to specify the username for the SSH connection. If you don't specify a username, SSH will use your current username by default. For example:

Host myserver
    HostName 192.168.1.100
    User myusername
    IdentityFile ~/.ssh/myprivatekey

In this example, we specify the username as myusername.

 

IdentityFile
The IdentityFile option allows you to specify the path to your private key file. This is necessary if you have multiple private key files or if your private key file has a non-standard name. For example:

Host myserver
    HostName 192.168.1.100
    User username
    IdentityFile ~/.ssh/myprivatekey

In this example, we specify the path to the private key file as ~/.ssh/myprivatekey.

 

Connecting to a Remote Server Using SSH Config

Once you have created your SSH configuration file, you can use it to connect to remote servers. To connect to a remote server using the configuration file, simply type the following command in your terminal:

ssh myserver

In this example, myserver is the nickname we defined in the configuration file. This will initiate an SSH connection to the remote server using the default options specified in the configuration file.


SSH Tunneling

SSH port forwarding is a powerful feature that allows you to securely access remote resources as if they were local. There are two types of port forwarding: local port forwarding and remote port forwarding. In this article, we'll cover how to use both types of port forwarding.

Local Port Forwarding

Local port forwarding allows you to forward a local port to a remote destination. This is useful when you need to access a service running on a remote server that is not accessible from your local network. Here's an example of how to use local port forwarding to access a remote MySQL database:

ssh -L 3306:localhost:3306 user@remote-serve

In this example, we're forwarding local port 3306 to port 3306 on the remote server. Once the SSH connection is established, you can connect to the MySQL database as if it were running locally on your machine:

mysql -h localhost -u dbuser -p

Remote Port Forwarding

Remote port forwarding allows you to forward a remote port to a local destination. This is useful when you need to expose a service running on your local machine to the internet through a remote server. Here's an example of how to use remote port forwarding to expose a local web server to the internet:

ssh -R 8080:localhost:80 user@remote-server

In this example, we're forwarding port 8080 on the remote server to port 80 on our local machine. Once the SSH connection is established, anyone can access our local web server by visiting http://remote-server:8080 in their web browser.

 

728x90

'Programming > Shell & Command' 카테고리의 다른 글

[nslookup] domain ip로 찾기  (0) 2023.03.10
mysql processlist logging  (0) 2023.03.03
[Shell] scp  (0) 2023.02.22
git windows에서 파일 권한 변경하기  (0) 2023.02.16
[Makefile] Makefile Tutorial  (0) 2022.03.31
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기
반응형