SSH


ssh and github  check ssh and socks4/5

ssh passwordless with keys.

 or if you have multiple ssh with different passwords but you generate rsa keys for all of them, so you can connect to all with only one pass phrase.
$ ssh-keygen -t rsa -C "info@mshahriarinia.com" # generate your ssh keys
$ eval "$(ssh-agent)"
$ ssh-add
$ ssh-copy-id -i ~/.ssh/id_rsa_passless.pub morteza@sm321-01.cise.ufl.edu
$ sudo /sbin/service sshd restart   # you might have to restart sshd
# if ssh to localhost doesn't work, $ chmod 750 $HOME      link

Read here on ssh: http://nerderati.com/2011/03/simplify-your-life-with-an-ssh-config-file/ http://www.debianadmin.com/howto-use-ssh-local-and-remote-port-forwarding.html
$ alias dev='ssh fooey@dev.example.com -p 22000'
$ # To connect:
$ dev

ssh-copy-id for moving your public keys. It will save you quite a few folder/file permission headaches.

# contents of $HOME/.ssh/config

Host dev
    HostName dev.example.com
    Port 22000
    User fooey
This means that I can simply  $ ssh dev, and the options will be read from the configuration file.
Port forwarding: Say you want to block your ports and still want to connect to a remote server on some port, but you want to close outside connections.




Add custom ssh file to memory

$ ssh-agent /bin/bash
$ ssh-add ~/mykeys/id_rsa
  • OR create a file inside your ~/.ssh folder named config

    you can use some command like the following

    $ nano ~/.ssh/config
    

    the content of the file should have the location of your key based on each host name. for example:

    Host github.com
     IdentityFile ~/myPublicKeyFolder/myGitHubFile
    Host heroku.com
     IdentityFile ~/myPublicKeyFolder/myHerokuFile
    

    Then when git tries to acces each host it will follow the rules inside this config file based on the git host your trying to reach

  • chmod 600 ~/.ssh/config

e.g.

Host github-project1
    User git
    HostName github.com
    IdentityFile ~/.ssh/github.project1.key

Host github-org
    User git
    HostName github.com
    IdentityFile ~/.ssh/github.org.key

Host github.com
    User git
    IdentityFile ~/.ssh/github.key

Later on you can $ git@github.com:cegme/gatordsr.git   with no password required. 
In the config file you can change the default number of connection attempts, specify local environment variables to be passed to the remote server upon connection, and even the use of * and ? wildcards for matching hosts.

To install ssh server on a machine

yum -y install openssh-server openssh-clients    # sudo apt-get install openssh-server openssh-client


put ssh password in commandline:

 sshpass -p 'trecGuest' ssh trecGuest@sm321-01.cise.ufl.edu "cat /media/sde/s3.amaz.gpg" | gpg --output - --decrypt - | xz --decompress

sshpass -p "PASSWORD" scp -rp /home/morteza/zproject/gatordsr/code/src/main/java/ morteza@sm321-01.cise.ufl.edu:/home/morteza/zproject/gatordsr/code/src/main/; sshpass -p "PASSWORD" scp -rp /home/morteza/zproject/gatordsr/code/src/main/scala/ morteza@sm321-01.cise.ufl.edu:/home/morteza/zproject/gatordsr/code/src/main/

X11 (GUI) forwarding on ssh

ssh -Y user@machine #ssh and run graphical programs on the server even if it doesn't have X!!!!!!!!
ssh -X ,, ,,, ,, ,, ,, ,, ,, 
 

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash"
       # -t will let to execute arbitrary commands on remote machine

Copy files

$ scp *.txt s3gusest@10.220.55.15:/home/s3guest/
Transfer files to SSH

$cd /opt
$ sudo rsync -avPp msnia@lin113-01.cise.ufl.edu:/opt/* .
It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon.  It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied.  It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the  source files and the existing files in the destination.  Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks for files that have changed in size or in last-modified time.  Any changes in the other preserved attributes (as requested by options)
       are made on the destination file directly when the quick check indicates that the file’s data does not need to be updated.


================

ssh configuration file

 -- Execute on local machine
cp ~/downloads/skey.pem ~/.ssh/
$ chmod 400 ~/.ssh/skey.pem
$ chmod 700 ~/.ssh
$ nano ~/.ssh/config
# edit the file as shown below
$ cat ~/.ssh/config
Host awshost1
HostName ec2-54-218-35-71.us-west-2.compute.amazonaws.com
User ubuntu
IdentityFile "~/.ssh/skey.pem"

$ ssh awshost1

$ scp hello.txt awshost1:~/                  ========          scp -i skey.pem hello.txt ubuntu@ec2-54-218-73-84.us-west-2.compute.amazonaws.com:~/ 
scp -i ~/.ssh/id_rsa -P 2022 Downloads/elementary_journal.pdf  msnia@pcluster.cise.ufl.edu:~/c.pdf
$ scp awshost1:~/foo.txt  .


tmux, screen


Sharing Terminal Sessions With Tmux And Screen ---- Avoid: Dropped connection and stopping processes.
Basic sharing with screen 
$ screen -S foobar 
Create a new session for screen, where foobar is the name of your screen session:
$ screen -x foobar     attach to an already attached session
$ screen -r foobar   for resuming a detached session

Ctrl A D to detach

$ screen -ls

Ctrl a c - Creates a new screen session so that you can use more than one screen session at once.
Ctrl a n - Switches to the next screen session (if you use more than one).
Ctrl a p - Switches to the previous screen session (if you use more than one).

$ exit    to terminate the screen and it's processes
Basic sharing with tmux

tmux new -s foo1   
# new-session   start tmux where foobar is the session name

tmux attach -t foo1       # attach-session: attach to the shared session.


Ctrl B D
to detach

$ tmux ls     
# list-sessions



Ctrl B Q    
 to show pane numbers
Ctrl B
arrowkeys to switch panes


As screen is not maintained anymore you should look for modern alternatives like tmux. tmux is superior for many reasons, here are just some examples:
  • Windows can be moved between session and even linked to multiple sessions
  • Windows can be split horizontally and vertically into panes
  • Support for UTF-8 and 256 colour terminals
  • Sessions can be controlled from the shell without the need to enter a session

Sharing between two different accounts

 

 with screen

If you are logged in as two different users, there are three prerequisites to using screen. First, screen must be set SUID and it is necessary to remove group write access from /var/run/screen. The safety of using SUID in this context is something to consider. Then you must use screen's ACLs to grant permission to the second user.

sudo chmod u+s /usr/bin/screen
sudo chmod 755 /var/run/screen

In the first user's terminal, start screen as in the basic sharing above, where foobar is the name of the screen session. Then turn on multiuser mode and add user2 to the ACL, where user2 is the second account to be sharing the session.

screen -S foobar
^A:multiuser on
^A:acladd user2

The session can be made read-only for the second user by entering the following ACL change: ^A:aclchg user2 -w "#?"

Then in the other terminal, attach to the first user's session.

screen -x user1/foobar

It is also possible to put multiuser on and acladd user2 into .screenrc to have it take effect automatically upon starting screen. If the changes are not desired in all screen sessions, then a separate .screenrc configuration file can be specified by using the -c option when starting screen.
 

with tmux

For different users, you have to set the permissions on the tmux socket so that both users can read and write it. There is only one prerequiste, that there be a group in common between the two users. If such a group does not exist it will be necessary to create one.

 In the first terminal
$ tmux -S /tmp/shareds new -s shared       start tmux where shared is the session name and shareds is the name of the socket: 
$ chgrp joint /tmp/shareds 
Then chgrp the socket to a group that both users share in common. In this example, joint is the group that both users share. If there are other users in the group, then they also have access. So it might be recommended that the group have only the two members.

In the second terminal
$ tmux -S /tmp/shareds attach -t shared       attach using that socket and session.

That's it. The session can be made read-only for the second user, but only on a voluntary basis. The decision to work read-only is made when the second user attaches to the session.
tmux -S /tmp/shareds attach -t shared -r 




tmux vs screen shortcut keys 


The formatting here is simple enough to understand (I would hope). ^ means ctrl+, so ^x is ctrl+x. M- means meta (generally left-alt or escape)+, so M-x is left-alt+x

Action tmux screen
start a new session tmux OR
tmux new OR
tmux new-session
screen
re-attach a detached session tmux attach OR
tmux attach-session
screen -r
re-attach an attached session (detaching it from elsewhere) tmux attach -d OR
tmux attach-session -d
screen -dr
re-attach an attached session (keeping it attached elsewhere) tmux attach OR
tmux attach-session
screen -x
detach from currently attached session ^b d OR
^b :detach
^a ^d OR
^a :detach
rename-window to newname ^b , <newname> OR
^b :rename-window <newname>
^a A <newname>
list windows ^b w ^a w
list windows in chooseable menu
^a "
go to window # ^b # ^a #
go to last-active window ^b l ^a l
go to next window ^b n ^a n
go to previous window ^b p ^a p
see keybindings ^b ? ^a ?
list sessions ^b s OR
tmux ls OR
tmux list-sessions
screen -ls
toggle visual bell
^a ^g
create another shell ^b c ^a c
exit current shell ^d ^d
split pane horizontally ^b "
split pane vertically ^b %
switch to another pane ^b o
kill the current pane ^b x OR (logout/^D)
close other panes except the current one ^b !
swap location of panes ^b ^o
show time ^b t
show numeric values of panes^b q