The syntax ssh command is
ssh [-l username] hostname | user@remote-hostname [command]
Let see the examples of ssh command.
SSH Command Examples:
1. Logging to a remote server
You can login to a remote server from the local host as shown below:
localhost:[~]> ssh -l username remote-server
username@remote-server password:
remote-server:[~]>
Alternatively you can use the below ssh command for connecting to remote host:
localhost:[~]> ssh username@remote-server
username@remote-server password:
remote-server:[~]>
Note: If you are logging for the first time, then it will prints a message that host key not found and you can give yes to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter the password.
2. Logging out from remote server
Simply enter the exit command on the terminal to close the connection. This is shown below:
remote-server:[~]>exit
logout
Connection to remote-server closed.
localhost:[~]>
3. Running remote commands from local host
Sometimes it is necessary to run the unix commands on the remote server from the local host. An example is shown below:
localhost:[~]> ssh user@remote-host "ls test"
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt
The ssh command connects to the remote host, runs the ls command, prints the output on the local host terminal and exits the connection from remote host.
localhost:[~]> ssh user@remote-host
user@remotehost password:
remotehost:[~]> cd test
remotehost:[~/test]> ls
online-backup.dat
oracle-storage.bat
unix-dedicated-server.txt
4. Version of the SSH command
We can find the version of SSH installed on the unix system using the -V option to the ssh. This is shown below:
> ssh -V
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2018
5. Debugging the SSH Client
When we are not able to connect to the remote host, it is good to debug and find the exact error messages that causing the issue. Use the -v option for debugging the ssh client.
ssh -v user@remote-host
OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2018
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to remote-host [172.22.200.140] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/identity type -1
debug1: identity file /home/user/.ssh/id_rsa type -1
debug1: identity file /home/user/.ssh/id_dsa type 2
debug1: loaded 3 keys
..........
..........
6. Copying files between remote host and local host.
We can use the scp command to copy the files securely between the local host and remote host using the ssh authentication.
To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.
scp filename user@remote-host:/var/tmp/
To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the below scp command.
scp user@remote-host:/usr/local/bin/add.sh.
0 comments:
Post a Comment