The evil Trojan stole my account on Habr, after which some stupid cartoons began to be published under my account. Unfortunately, I only found out about this when the UFO turned me into read-only, and the rating went negative. It does not matter: the reason to finally write a post that has long been going to.
A web developer needs a console, but not so much that if he leaves all his business to start reading fat books on Linux. That is why I studied the console tricks from time to time and, judging by my staff, many do the same. I will reveal a couple of convenient secrets, without which I can no longer live.
1) Use ssh keys, Luke!
I discovered the keys for a long time, although I regularly meet people for whom they are new. The ssh keys allow you to set up a connection once and no longer store passwords to all sites in a notebook.
$ ssh-keygen -t dsa
We agree on the standard location of the key: /home/user/data/.ssh/id_dsa,
Enter (or do not enter) passphrase. It is better to enter: the system will remember your password from login to logout, that is, you do not need to enter this password all the time. But security will increase by an order of magnitude.
After that we will get two files: ~ / .ssh / id_dsa and ~ / .ssh / id_dsa.pub.
The first is a private (private) key - it is better to copy it to a USB flash drive and hide it in reserve. The second is the public key, and we will inform all of our servers about it.
The easiest way to transfer the key to the server is to execute this command in your console:
$ ssh user@hostname "umask 077; cat >> .ssh/authorized_keys" < ~/.ssh/id_dsa.pub
and last time enter the password to the ssh remote computer
')
2) Use ssh configs, Luke!
Everything works fine, but you need to enter long logins and hostnames every time. Need to optimize!
Edit the file ~ / .ssh / config, add:
Host host
User user
Hostname hostname
We check the rights to the file ~ / .ssh / config, if they are allowed to write to someone other than us, we change to others:
$ chmod 644 ~/.ssh/config
Suppose the computer to which we want to connect is located on the nat. We need to log in via SSH to one server, then from there move to the correct computer. If you need to do this many times a day, it will get very, very fast.
We register a new rule in the config:
Host computer.hostname
Hostname 192.168.1.10
User user
ProxyCommand ssh hostname nc %h %p
That's all! Now we can write ssh computer.hostname, the user will be automatically substituted, the connection will be established directly with the desired computer. The main thing is not to forget him, too, put your public key.
In addition, I will describe two useful directives
LocalForward localhost:8080 192.168.10.10:80 # , SSH.
Port 8022 # SSH , .
3) Power in autocompletions
Enter four letters host each time? This is tiring! As a rule, auto completion of the ssh-config files parsit, just start writing the hostname and pressing the tab so that the host name will be added automatically. If this does not happen, you need a bash to teach it.
Add a line
complete -W "$(echo `cat ~/.ssh/config | grep -iE '^(Host|HostName) ' | awk '{print $2}'`)" ssh
to ~ / .bash_profile
There you can add the following code:
function __mysql_list_all_opts {
local i IFS=$'\n'
mysql --help|egrep '^ -'|awk '{print $1 "\n" $2}'|egrep '^-'|sed s/,$//|sort
}
__mysql_all_opts=
function __mysql_compute_all_opts {
: ${__mysql_all_opts:=$(__mysql_list_all_opts)}
}
function _mysql_complete {
local cur prev opts
COMPREPLY=()
cur=`_get_cword`
prev=${COMP_WORDS[COMP_CWORD-1]}
case $prev in
*)
if [[ "$cur" == -* ]]; then
__mysql_compute_all_opts
opts=${__mysql_all_opts}
else
opts=$(mysql -uroot -s -e 'show databases')
fi
;;
esac
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
COMPREPLY=( $(compgen -W "$opts" -- $cur) )
}
complete -F _mysql_complete mysql
Similarly, you can register and mysqldump
After we open a new bash console, our console will complement the name of the remote computer and the name of the local database!
If you have a password to connect to the database, you need to take the next step.
4) Do not enter the password for the console muscle
Each time you start the console client mysql or mysqldump you need to remember to give him a login and password. To avoid this, it is enough to create the file ~ / .my.cnf once and for all with the following contents:
[client]
user = 'root'
password = 'password'
[mysql]
pager = less -iMSx4 -FX
Section mysqld add optional. It will allow you not to suffer the optimal selection of the limit when working with the database from the command line. If the output is longer than the number of lines on the screen - the output will automatically be sent to the less command. By which you can conveniently move vertically and horizontally, and even make a search!
5) Results:
To get a database dump from a remote server, you had to perform a series of commands earlier. In the worst case (example is based on real events):
localhost $ ssh -P 8022 user@hostname #
hostname $ ssh user2@computer #
computer $ mysqldump -u root -p password long_database_name > ~/filename.sql
computer $ exit
hostname $ scp user2@computer:~/filename.sql ~/filename.sql
hostname $ ssh user2@computer
computer $ rm ~/filename.sql
computer $ exit
hostname $ exit
localhost $ scp -P 8022 user@hostname:~/filename.sql ~/filename.sql
localhost $ ssh -P 8022 user@hostname
hostname $ rm ~/filename.sql
hostname $ exit
localhost $ cat ~/filename.sql | mysql -u root -p password long_database_name
localhost $ rm ~/filename.sql # ,
Now, instead of all this horror enough to execute
one command:
$ ssh computer mysqldump long_database_name | mysql long_database_name
In reality, and even less, because before each command, you can press the tab:
ssh com [tab] mysqldu [tab] lon [tab] | mys [tab] lon [tab]
There is no desire to send the file in unpacked form? It does not matter, we will pack on the fly from the other side in zip, and with this one - unpack.
$ ssh computer 'mysqldump long_database_name | gzip' | gunzip | mysql longdatabase_name
As additional bonuses, it was possible to download files directly from a remote computer for a while, without re-saving them.
$ scp computer:~/test.txt ~/
If the topic of the community seems interesting - I will continue.
I can tell you about how to configure iTerm under a poppy so that it would be extremely convenient to work with ssh
About how the basics of bash scripting can save a lot of time when working with the command line
About the advantages of the screen command and how to conveniently configure it
And also about the forgotten grandfather of z-modem and how he can help a modern developer in everyday life.