ssh – My Random Tips https://www.myrandomtips.com Useful Tips to Save Time Sun, 25 Aug 2019 23:50:13 +0000 en hourly 1 https://wordpress.org/?v=6.8.1 https://www.myrandomtips.com/wp-content/uploads/2017/10/cropped-light-bulb-icon-42x42.png ssh – My Random Tips https://www.myrandomtips.com 32 32 VIM Cheat Sheet https://www.myrandomtips.com/linux/vim-cheat-sheet/ https://www.myrandomtips.com/linux/vim-cheat-sheet/#respond Sun, 25 Aug 2019 01:05:02 +0000 https://www.myrandomtips.com/?p=746 This guide will teach you the basics of VIM and how to get going quickly, and offer you a VIM cheat sheet and other useful resources on where you can learn more. While VIM can appear hard, it is a really powerful text editor and worth the effort as will save you time in editing large amounts of… Read More »

The post VIM Cheat Sheet appeared first on My Random Tips.

]]>
VIM Cheat Sheet

This guide will teach you the basics of VIM and how to get going quickly, and offer you a VIM cheat sheet and other useful resources on where you can learn more.

While VIM can appear hard, it is a really powerful text editor and worth the effort as will save you time in editing large amounts of text.

You will find VIM or vi on most UNIX systems and with Apple OS X. 

How to Run VIM / VI

You can run VIM / VI from the command line or open a file here is the commands:

  1. vi   # This will open vi text editor
  2. vim  # This will open vim text edditor
  3. vi newfile # type a new file name and it will make a blank file ready to save
  4. vim newfile # type a new file name and it will make a blank file ready to save
  5. vi existingfile # type a file name in the current folder to open the text file to edit.
  6. vm  existingfile # type a file name in the current folder to open the text file to edit.

How to quit VIM / VI

esc then : then  q then enter # Will quit file, you may need to add a ! to force close without saving changes.

How to save and quit VIM / VI

esc then : then  wq then enter # This will quit and save changes.

How to enter text into VIM / VI

i # This will turn to input mode you can use the arrow keys and type in text as required.

How to find text in VIM / VI

esc then / then text then enter # This will find word that matches your search use n to go to next result or N to go back.

How to undo and redo in VIM / VI

esc then u # in command mode hit u to undo any change you may keep using u to go back as far as you need.

esc then hold Ctrl + r # in command mode hit Ctrl + u to keep redoing actions you may keep using u to go forward as far as you need.

How to copy and paste lines in VIM / VI

esc then go to line then yy # in command hit yy this will yank the whole line into memory

p # at the desired location to paste hit p to paste the copied line.

Tip: you put a number before yy to cope more line e.g 3yy will copy 3 lines, then p will paste the 3 lines.

Video on using these commands and more

How to learn VIM / VI in a Chrome Game

Goto https://vim-adventures.com/ here you can play a game to teach vim commands, it will take you through the keys by moving a character through an island teaching you all the basics.  

The first level or so is free but will cost $25 for for 6 months access, not worth it IMHO but at lest you can get a start, then move onto the free vimtutor below.

How to learn VIM / VI in Unix

Type vimtutor in unix and hit enter – Yes, vimtutor is literally the name of a program included on your unix system that runs you through a tutorial for vim, it is interactive and fun. 

VIM VI Cheat Sheet

Here is a small vim cheat sheet for the info above you can print out.

Summary

Using VI or VIM can be fun once you handle the basics then the sky’s the limit as there is so much to learn and enhance your day to day work.

For other Linux / Unix related tutorials on my website please click here.

Also please have a look at my YouTube Channel and subscribe for video tutorials as they are released, and if you have any advice for others, requests or questions please leave a comment below.

The post VIM Cheat Sheet appeared first on My Random Tips.

]]>
https://www.myrandomtips.com/linux/vim-cheat-sheet/feed/ 0
create ssh key https://www.myrandomtips.com/linux/create-ssh-key/ https://www.myrandomtips.com/linux/create-ssh-key/#respond Thu, 06 Sep 2018 06:46:34 +0000 https://www.myrandomtips.com/?p=472 In Linux and most other Linux distributions you can use ssh key pairs to easily remote into other systems and run scripts remotely. This is extremely useful when managing multiple Linux hosts and if you wish to gather reports from one central source. What are SSH Key Pairs? SSH key pairs are two cryptographically secure keys that can be… Read More »

The post create ssh key appeared first on My Random Tips.

]]>
How to create ssh key pairs in Linux

In Linux and most other Linux distributions you can use ssh key pairs to easily remote into other systems and run scripts remotely. This is extremely useful when managing multiple Linux hosts and if you wish to gather reports from one central source.

What are SSH Key Pairs?

SSH key pairs are two cryptographically secure keys that can be used to authenticate a client to an SSH server. Each key pair consists of a public key and a private key.

The private key is retained by the client and should be kept absolutely secret. Any compromise of the private key will allow the attacker to log into servers that are configured with the associated public key without additional authentication. As an additional precaution, the key can be encrypted on disk with a passphrase.

The associated public key can be shared freely without any negative consequences. The public key can be used to encrypt messages that only the private key can decrypt. This property is employed as a way of authenticating using the key pair.

The public key is uploaded to a remote server that you want to be able to log into with SSH. The key is added to a special file within the user account you will be logging into called ~/.ssh/authorized_keys.

When a client attempts to authenticate using SSH keys, the server can test the client on whether they are in possession of the private key. If the client can prove that it owns the private key, a shell session is spawned or the requested command is executed.

These 3-steps are how to create ssh key pairs in Linux:

  1. Create your ssh key pairs on the client server you wish to connect from.
  2. Copy your public key onto the host you wish to connect to.
  3. Load your ssh key on the client server your connecting from.

An overview of the flow is shown in this diagram:

The diagram shows a laptop connecting to a server, but it could just as easily be one server connecting to another server.

  • private key (id_rsa) – this is the master key that sits on your client your connecting from.
  • public key (id_rsa.pub) – like a door lock sits on the servers you want to connect to

Detailed Steps on how to create ssh keys in Lunix

This contains details steps on how to create your ssh key pairs and load them onto your servers.

Setting up SSH Keys (public and private key)  

Just a quick summary on how to use the ssh-agent so we can use ssh keys which are password protected.

[CLIENT “Main client your connecting from, holds the private key (id_rsa)”]
Under your local account, create your SSH key pair , by default it will create under your home directory a .ssh directory (hidden folder)

First run ssh-keygen

localuser@client:remotehost# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/localuser /.ssh/id_rsa):
/home/localuser /.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/localuser /.ssh/id_rsa.
Your public key has been saved in /home/localuser /.ssh/id_rsa.pub.
The key fingerprint is:
cc:1e:55:ad:44:fe:51:3d:c6:7a:5b:e4:9a:e2:fa:f6  localuser@remotehost

Within this directory after running the command ssh-keygen , you will find your SSH key pair

localuser@remotehost# ls -al .ssh
total 20
drwx------ 2 wz7052 techsup 4096 Oct 21 13:27 .
drwx------ 3 wz7052 techsup 4096 Oct 21 13:41 ..
-rw------- 1 wz7052 techsup 1743 Oct 21 13:36 id_rsa   <-- private key
-rw-r--r-- 1 wz7052 techsup  425 Oct 21 13:36 id_rsa.pub  <-- public key

[SERVER “Server you want to connect to, holds the public key (id_rsa.pub)”]

We use a useful copy feature to push the keys onto the server we want to connect to, but we need to create the hidden .ssh folder in our home directory.

Create a .ssh directory under your home directory , make sure the permission are set to 700:

cd ~
mkdir .ssh
chmod 700 .ssh

Repeat for each server you want to connect to.

[CLIENT “Main client your connecting from”]

Logon as yourself.

Copy the id_rsa.pub (the public key to the host you want to connect to)

ssh-copy-id -i .ssh/id_rsa.pub user@remotehost

You will be prompted for your password on the remote host, and then it will push the key to your ~/.ssh directory and set up everything – saves a LOT of hassles instead of manually copying the file to the client!

Making it all work

[CLIENT “Main client your connecting from”]

Passphrase Log onto server

So now it is all configured and pushed onto each client you wish to connect to, you logon as yourself on the client.

ssh to the server you want to connect to:

ssh server
since ssh is smart it will see you have a public key present and ask for your passphrase:
Enter passphrase for key ‘/home/localuser/.ssh/id_rsa’:
Last login: Tue Oct 21 13:43:12 2014 from 172.22.1.138

Passphrase auto Log onto server

Now we have this key loaded on many hosts and we don’t want to put in the password all the time.

To get around this , preload the the ssh-agent on the primary host and load the private key.

It will ask for the key password but as long as you run commands from this ssh session it will automatically provide the passwords to the remote hosts.

Let me show you

Load ssh-agent shell

localuser@client# ssh-agent /bin/bash
Add your private key to the agent
[localuser@client ~]$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/localuser/.ssh/id_rsa:
Identity added: /home/localuser/.ssh/id_rsa (/home/localuser/.ssh/id_rsa)

Now you should not need the password again when connecting to the remote host:

[localuser@client~]$ ssh server
Last login: Tue Oct 21 14:17:07 2014 from 171.22.12.138
[localuser@server~]$

You see it used the password kept by the ssh-agent.

Running remote commands

Now you can use key pair to run commands on the other side.

ssh -q 10.11.0.92 hostname
server

I have written a script that loops a bunch of servers IPs defined in hostlist.txt to run a simple hostname command in this script:

cat hostlist.txt
server1
server2
server3
server4
server5

Script to gather hostname list and ensure that all the remote systems are connecting from the defined list:

cat hostname.sh
ssh-agent /bin/bas
ssh-add ~/.ssh/id_rsa
for host in `cat hostlist.txt`; do ssh -q $host hostname >> ssh-testrun ; done
wc -l hostlist.txt ; wc -l ssh-testrun

Run the script and you should see all 5 servers defined in hostlist.txt match the connect output in ssh-testrun:

./hostname.sh
5 hostlist.txt
5 ssh-testrun

Summary

You can see that creating ssh keys can be a powerful tool to easily connect to remote systems and also run remote health checks and gather data.

The post create ssh key appeared first on My Random Tips.

]]>
https://www.myrandomtips.com/linux/create-ssh-key/feed/ 0