Wednesday, April 24, 2024
HomeJava10 Instance of SSH command in Linux and UNIX

10 Instance of SSH command in Linux and UNIX


Hey guys, you probably have been working in Linux machine then there may be nice
likelihood that you’ve come throughout SSH and SCP instructions that are important to
change between completely different hosts in a Linux community. As a Java developer, I want
to work on greater than 30 to 40 servers which comprises improvement server, UAT
servers, manufacturing servers in addition to server from completely different areas like
Asia, Center East, UK, and the USA. It is troublesome to work between server if
you do not go from one host to different or copy knowledge between them like
information and listing and that is the place this SSH instructions comes useful. The SSH is
one of many important Linux instructions which lets you log in to distant host
utilizing safe socket protocol. By default, the SSH program listens on port
22..

You want an SSH consumer like Putty to login to the distant host however you should utilize
any consumer. Putty is like default as a result of wherever I’ve labored, Putty is
at all times there. Although, I’ve additionally used purchasers like WinSCP which is used to
switch information between Home windows and Linux hosts. 

With the intention to hook up with distant host, you want a username and password as nicely
as entry to that host, which is management utilizing completely different mechanism. If the
host lets you join utilizing public key then you’ll be able to merely generate a
private and non-private key and provides the general public key to the one that has entry to
that host and he can add that into SSH config. As soon as that’s finished you’ll be able to login
to the distant host utilizing SSH. 

However SSH is rather more than that and even after working in Linux for shut to twenty
years, I’m nonetheless discovering issues which I do not know and folks all additionally very
inventive with respect to utilizing SSH. Certainly one of my colleague was so good at
creating bash script utilizing SSH {that a} regular launch which took hours 
might be accomplished utilizing minutes. 

10 methods to make use of SSH command in Linux and UNIX

Right here is listing of important SSH command and the way you should utilize them in Linux to
login to distant host utilizing username and password or through the use of your public and
personal key. 

1. Connecting to a distant host utilizing a password and present consumer

This command will hook up with the distant host by logging in as a present
consumer

$ ssh dev1234pgcm
The authenticity of host 'dev1234pgcm (10.62.33.22)' can't be established.
RSA key fingerprint is c1:7a:a9:6b:ef:4c:c0:6d:6c:2b:1f:b4:2b:80:eb:54.
Are you certain you wish to proceed connecting (sure/no)? sure
Warning: Completely added 'dev1234pgcm, (10.62.33.22)' 
(RSA) to the listing of recognized hosts.
pw
Permission denied, please attempt once more.

When you enter the right password, it is possible for you to to login to the host,
offered you could have entry to that host. If you do not have entry then you’ll be able to
not log in to the host.

2. Connecting to a distant host utilizing a selected consumer and password

If you happen to do not present username then SSH by default makes use of the present consumer who’s
executing the command, however if you wish to use any particular consumer you’ll be able to
present the username as proven beneath:

$ ssh user1@host1.com

Now it can ask password for user1 and when you enter the right password,
you’ll be able to log in to the required host i.e. host1

3. Connecting to a distant host utilizing a non-public key (with out password)

SSH additionally permits a consumer to connect with a distant consumer utilizing RSA personal key, in
this case, you need not kind the password. That is the popular solution to
do ssh/SCP from scripts. You may
use the ssh -i choice to offer
the identification file containing the consumer’s personal key. The
-i stands for identification file as
proven beneath:

$ ssh -i /dwelling/user1/.ssh/id_rsa user1@host1.com

To ensure that this to work, the general public key of user1 also needs to exist within the
authorized_keys within the host1.com
and public key of host1.com ought to exist within the
known_hosts on a consumer machine.
That is additionally the way in which you configure passwordless login utilizing SSH.

4. Executing a command on a distant host

You may execute a command on a distant host with out bodily logging into it
utilizing SSH command. That is very helpful if you wish to execute a specific
command on a number of hosts utilizing a script e.g. deploying a package deal to N quantity
of hosts or getting the present model of your utility from all of your
manufacturing hosts. 

Right here is how one can execute a command on a distant host utilizing SSH in Linux:

$ ssh user1@remotehost.com "ls -lrt /dwelling/user1/app"

This can print the output of  ‘ls -lrt /dwelling/user1/app’
from the remotehost.com and login as user1. This fashion you will get variations of
your utility from a number of hosts with out bodily login into them.

If you happen to do not wish to login with one other consumer, you may also do that to execute
a command as a present consumer within the distant host:

$ ssh host1.com uptime

This can print the uptime from host1.com which tells from how lengthy host1 is
up.

5. Connecting to a selected port

By default, SSH protocol connects to port 22 as a result of that is the place the SSH
program listens for connections, however whereas putting in SSH, you’ll be able to change this
port (although I do not advise that). If you need to log in to a number which is
listening on SSH connection on a distinct port then 22 then you should utilize the
following command to login:

$ ssh remoteuser@remotehost -p 9066

6.  ssh-keygen instance 

You may take away all keys associated to a number through the use of
ssh-keygen -R command. That is
helpful when the general public key of a number modified as a result of re-installing SSH, or
transferring to a distinct IP handle or OS re-installation.

$ ssh-keygen -R remotehost.com

This can take away entries for host1.com from known_hosts file within the consumer
machine.

7. Connecting to a distant host utilizing IP handle

If you happen to do not bear in mind hostname however have IP handle then additionally you’ll be able to join
to the distant host as proven beneath:

$ ssh 14.43.13.21

This can attempt to login the present consumer to 13.43.13.21 host. It is advisable enter
the password of present consumer to proceed additional.

8. Copying a tar file to a distant host utilizing SSH

You may as well use ssh with tar to maneuver a listing of information between two
machines as an alternative choice to scp:

$ tar -cvf - photos/canines | ssh consumer@remotehost '(cd new_images; tar -xf -)'

9. Producing a key pair (private and non-private key, RSA or DSA)

If you do not have a pair or private and non-private key then you’ll be able to generate utilizing
ssh-keygen -t command as proven
beneath:

$ ssh-keygen -t dsa
Producing public/personal dsa key pair.
Enter file in which to avoid wasting the important thing (/dwelling/user1/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter identical passphrase once more:
Your identification has been saved in /dwelling/user1/.ssh/id_dsa.
Your public key has been saved in /dwelling/user1/.ssh/id_dsa.pub.
The important thing fingerprint is:
13:58:30:56:72:e7:bd:14:86:9f:42:aa:82:3d:f8:e5 user1@host1.com

You need to use any encryption algorithm like DSA or RSA. You may as well present
particular file title in order for you. Simply bear in mind to maintain the passphrase empty if
you wish to do password-less login, in any other case even if you happen to use personal key SSH
will ask for this passphrase.

How SSH works in Linux

10. Switching to a specific location

By default if you log in to a number, if you happen to do not present any path then you definately
will find yourself on the dwelling listing. However, you’ll be able to present the
cd command

to change to the listing you need if you login to the distant host:

$ ssh -t xxx.xxx.xxx.xxx "cd /destination_directory ; bash --login"

11. Ending your SSH session

You may both kind exit, logout or simply press Ctrl + D to terminate your SSH
session and are available again to the unique host the place you sshed to distant host.

That is all about  important examples of SSH command in Linux. It is
one of the crucial helpful Linux instructions for each Software program Builders, System
administrator, IT assist skilled or anybody who must effectively work
on a community, the place you need to login into a number of host as a part of your work.

Different Linux and UNIX Tutorials for Newbies and Skilled

  • The best way to get an IP handle from the hostname and vice-versa in Linux (command)
  • 10 examples of date command in Linux (examples)
  • 10 examples of the xargs command in Linux (examples)
  • 10 examples of tar command in UNIX (examples)
  • 5 Free Programs to be taught Linux for Newbies (Free programs)
  • 10 examples of Vim in UNIX (examples)
  • 5 examples of type command in Linux (examples)
  • The best way to create, replace and delete tender hyperlink in UNIX (command)
  • 5 examples of kill command in Linux (examples)
  • 6 Free Programs to be taught Bash scripting in-depth (free programs)
  • 10 examples of chmod command in UNIX (examples)
  • 10 examples of lsof command in Linux (examples)
  • 10 examples of curl command in Linux (examples)
  • 10 Books each Linux Energy consumer ought to learn (books)
  • 10 examples of minimize command in Linux (examples)
  • 10 Greatest Linux Programs for Programmers and Builders (Programs)

Thanks for studying this text up to now. If you happen to like this Linux SSH
tutorial and examples, then please share it with your folks and colleagues.
You probably have any questions or suggestions, then please drop a word.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments