10 Example of SSH Command in UNIX and Linux

SSH command stands for Secure Shell which allows user to securely login into remote machine. If you are working on network on UNIX and Linux machine, SSH command is your friend because with ssh in UNIX you can not easily navigate from one host to other. SSH Command or SSH client in UNIX not only allow you to login into remote host but also allows you to execute command on remote machine without going to into remote server. which is quite convenient while writing bash scripts or getting general information about server like uptime of remote server. This article shows examples of ssh command in UNIX and different usage of ssh in UNIX along with basic concept of ssh command.

SSH Command Examples

1) opening connection to remote host using SSH :

$ ssh username@hostname
2)If you are using same username than you can just type hostname to go from one remote host
to other, though it will ask password before you can access remote machine.

$ ssh hostname
3. In this example of SSH command we will see how to run UNIX command into remote host using ssh.
ssh username@hostname uptime
This is truly beneficial and increase productivity because you don't need to login into the server
to execute command.

4) Again if you are using same username for using multiple remote machine you can just omit username
while executing ssh command in UNIX and execute any Linux command in remote machine as showing in below example of ssh in UNIX.
$ ssh hostname uptime

5) SSH Command example with explicit port Number
by default ssh command in UNIX or ssh protocol uses port 22 to connect remote machine, but if it's different port than you can specify it with ssh command option "-p" as shown in below example of ssh command.
$ ssh -p newport username@remotehost
6) Copying all files from remote directory using scp.

$ scp yourusername@yourserver:/home/yourusername/ .

7) You can even pipe the output of one unix command to SSH in unix , as shown in below example of ssh in linux
where we are using tar alongwith ssh command to transfer one directory from one host to other.
$ tar -cvf - trades/equities | ssh username@host '(cd trade_backup;tar -xf -)'

8. Terminating ssh session in UNIX
terminating ssh session to a remote host is simple in UNIX just press Ctrl+D and your ssh session will be terminated and you will be back on your localhost.

9. SSH Installation and versions
OpenSSH is the most used free version of SSH command and its available for all flavor of Unix e.g.
Redhat Linux, IBM AIX , SuSE and even Cygwin on windows.

10 Example of SSH Command in UNIX and Linux


SSH Command line options

Though SSH Command in UNIX support number of parameters or command line options, we will only focus on some of the most useful options which we use frequently. you can get full list of ssh command line options anytime by executing "man ssh".
1. "-N" ssh command-line option is used to restrict the execution of the remote command.

2. "-p" is used to specify port to connect on the remote host. if it's different than default port of SSH
protocol(port 22).

3. "-v" is for verbose output.

4.  "-x" ssh command-line option is used to disable x11 forwarding while "-X" enables x11 forwarding.
5. numeric options of ssh command in UNIX e.g. "-1", "-2"  forces SSH to use protocol version 1 and 2 and "-4" and "-6" instruct ssh to use IPv4 and IPv6 addresses only.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.