Tech in T: depth + breadth‎ > ‎OS‎ > ‎Linux‎ > ‎


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 "" # generate your ssh keys
$ eval "$(ssh-agent)"
$ ssh-add
$ ssh-copy-id -i ~/.ssh/
$ 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:
$ alias dev='ssh -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
    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:

     IdentityFile ~/myPublicKeyFolder/myGitHubFile
     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


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

Host github-org
    User git
    IdentityFile ~/.ssh/

    User git
    IdentityFile ~/.ssh/github.key

Later on you can $   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 "cat /media/sde/s3.amaz.gpg" | gpg --output - --decrypt - | xz --decompress

sshpass -p "PASSWORD" scp -rp /home/morteza/zproject/gatordsr/code/src/main/java/; sshpass -p "PASSWORD" scp -rp /home/morteza/zproject/gatordsr/code/src/main/scala/

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 "cd /directory_wanted ; bash"
       # -t will let to execute arbitrary commands on remote machine

Copy files

$ scp *.txt s3gusest@
Transfer files to SSH

$cd /opt
$ sudo rsync -avPp* .
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
User ubuntu
IdentityFile "~/.ssh/skey.pem"

$ ssh awshost1

$ scp hello.txt awshost1:~/                  ========          scp -i skey.pem hello.txt 
scp -i ~/.ssh/id_rsa -P 2022 Downloads/elementary_journal.pdf
$ scp awshost1:~/foo.txt  .

tmux, screen

Sharing Terminal Sessions With Tmux And Screen ---- Avoid: Dropped connection and stopping processes. 

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

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 session_name   
# new-session   start tmux where foobar is the session name

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

Ctrl B D
to detach

tmux kill-session -t 

tmux ls     
# list-sessions

Ctrl B
arrowkeys to switch panes

Window commands
^b w    list windows in all sessions
^b ,     name current window
^b n    next window
^b p    previous window
^b c    create window

gem install tmuxinator
mkdir .bin
wget > .bin/
vim .bashrc
export EDITOR='vim'
source ~/.bin/tmuxinator.bash
. .bashrc

tmuxinator new session_name
mux start session_name    # start the tmux session or if exists attach to it

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 -s session_name

re-attach a detached session tmux attach -t session_name
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
^a ^d
rename-window to newname ^b , <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 numeric values of panes^b q

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

the default layout options are:
even-horizontal, even-vertical, main-horizontal, main-vertical, tiled

mux new sample
mux start sample                     mux start -p  ~/zProject/pluralytics/pluralytics.sandbox/mshahriarinia/.tmuxinator/pluralytics-tmuxinator-python.yml
mux stop sample

name: test
root: ~/zProject

pre_window: rbenv shell 2.0.0-p247
  - myeditor: vim
- stats: - ssh - tail -f /var/log/stats.log
- small_project: root: ~/zProject/archive panes: - htop - watch ls
- logs: layout: main-vertical panes: - logs: - ssh - cd /var/logs - tail -f development.log

You can also pass key-value pairs using the format key=value. These will be available in a hash named @settings.


$ tmuxinator start project workspace=~/workspace/todo
# ~/.tmuxinator/project.yml

name: project
root: ~/<%= @settings["workspace"] %>