Using a variable's value as password for scp, ssh etc. instead of prompting for user input every time

前端 未结 10 1465
独厮守ぢ
独厮守ぢ 2020-12-03 03:38

AFAIK, the commands ssh or scp do not have/take a password parameter. Otherwise I could keep the password in a shell variable and probably get rid

相关标签:
10条回答
  • 2020-12-03 04:19

    Yes, you want pubkey authentication.

    0 讨论(0)
  • 2020-12-03 04:19

    ssh, ssh-keygen, ssh-agent, ssh-add and a correct configuration in /etc/ssh_config on the remote systems are necessary ingredients for securing access to remote systems.

    First, a private/public keypair needs to be generated with ssh-keygen. The result of the keygen process are two files: the public key and the private key.

    The public key file, usually stored in ~/.ssh/id_dsa.pub (or ~/.ssh/id_rsa.pub, for RSA encryptions) needs to be copied to each remote system that will be granting remote access to the user.

    The private key file should remain on the originating system, or on a portable USB ("thumb") drive that is referenced from the sourcing system.

    When generating the key pair, a passphrase is used to protect it from usage by non-authenticated users. When establishing an ssh session for the first time, the private key can only be unlocked with the passphrase. Once unlocked, it is possible for the originating system to remember the unlocked private key with ssh-agent. Some systems (e.g., Mac OS X) will automatically start up ssh-agent as part of the login process, and then do an automatic ssh-add -k that unlocks your private ssh keys using a passphrase previously stored in the keychain file.

    Connections to remote systems can be direct, or proxied through ssh gateways. In the former case, the remote system only needs to have the public key corresponding to the available unlocked private keys. In the case of using a gateway, the intermediate system must have the public key as well as the eventual target system. In addition, the original ssh command needs to enable agent forwarding, either by configuration in ~/.ssh/config or by command option -A.

    For example, to login to remote system "app1" through an ssh gateway system called "gw", the following can be done:

    ssh -At gw ssh -A app1
    

    or the following stanzas placed in the ~/.ssh/config file:

    Host app1
    ForwardAgent = yes
    ProxyCommand = ssh -At gw nc %h %p 2>/dev/null
    

    which runs "net cat" (aka nc) on the ssh gateway as a network pipe.

    The above setup will allow very simple ssh commands, even through ssh gateways:

    ssh app1
    

    Sometimes, even more important than terminal sessions are scp and rsync commands for moving files around securely. For example, I use something like this to synchronize my personal environment to a remote system:

    rsync -vaut ~/.env* ~/.bash* app1:
    

    Without the config file and nc proxy command, the rsync would get a little more complicated:

    rsync -vaut -e 'ssh -A gw' app1:
    

    None of this will work correctly unless the remote systems' /etc/ssh_config is configured correctly. One such configuration is to remove "root" access via ssh, which improve tracking and accountability when several staff can perform root functions.

    In unattended batch scripts, a special ssh key-pair needs to be generated for the non-root userid under which the scripts are run. Just as with ssh session management, the batch user ssh key-pair needs to be deployed similarly, with the public key copied to the remote systems, and the private key residing on the source system.

    The private key can be locked with a passphrase or unlocked, as desired by the system managers and/or developers. The way to use the special batch ssh key, even in a script running under root, is to use the "ssh -i ~/.ssh/id_dsa" command options with all remote access commands. For example, to copy a file within a script using the special "batch" user access:

    rsync -vaut -e 'ssh -i ~batch/.ssh/id_dsa -A gw' $sourcefiles batch@app2:/Sites/www/
    

    This causes rsync to use a special ssh command as the remote access shell. The special-case ssh command uses the "batch" user's DSA private key as its identity. The rsync command's target remote system will be accessed using the "batch" user.

    0 讨论(0)
  • 2020-12-03 04:22

    You can Using expect to pass a password to ssh do this or as said already use public key authentication instead if that's a viable option.

    0 讨论(0)
  • 2020-12-03 04:24

    Indeed, you'll definitely want to look into setting up ssh keys, over saving a password in a bash script. If the key is passwordless, then no user input will be required to ssh/scp. You just set it up to use the key on both ends and voila, secured communication.

    However, I'll get downvoted to hell if I don't say this. Many consider passwordless ssh keys to be a Bad Idea(TM). If anybody gets their hands on the keys, the have full access. This means that you are relying on other security measures such as file permissions to keep your password safe.

    Also, look into ssh-agent. It allows you to set it up so that you have a password protected ssh-key, but you only need to type it in once and it will manage the password for the key for you and use it when necessary. On my linux box at home, I have ssh-agent set up to run in my .xinitrc file so that it prompts me once and then starts X. YMMV.

    UPDATE:
    With regards to your requirements, password protected public key authentication + ssh-agent still seems to fit. Only the developers privy to the SSH/FTP password could start up ssh-agent, type in the password and ssh-agent would manage the passwords for the public keys for the rest of the session, never requiring interaction again.

    Of course, how it stores it is another matter entirely. IANASE, but for more information on security concerns of using ssh-agent, I found symantec's article to be pretty informative: http://www.symantec.com/connect/articles/ssh-and-ssh-agent

    "The ssh-agent creates a unix domain socket, and then listens for connections from /usr/bin/ssh on this socket. It relies on simple unix permissions to prevent access to this socket, which means that any keys you put into your agent are available to anyone who can connect to this socket. [ie. root]" ...

    "however, [..] they are only usable while the agent is running -- root could use your agent to authenticate to your accounts on other systems, but it doesn't provide direct access to the keys themselves. This means that the keys can't be taken off the machine and used from other locations indefinitely."

    Hopefully you're not in a situation where you're trying to use an untrusted root's system.

    0 讨论(0)
  • 2020-12-03 04:26

    Ugh. I hit the man pages hard for this. Here's what I got:

    Use this code near the beginning of the script to silently get the ssh password:

    read -p "Password: " -s SSHPASS # *MUST* be SSHPASS
    export SSHPASS
    

    And then use sshpass for ssh like so:

    sshpass -e ssh username@hostname
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-03 04:31

    Today, the only way I was able to do this in a bash script via crontab was like that:

    eval $(keychain --eval --agents ssh id_rsa id_dsa id_ed25519)
    source $HOME/.keychain/$HOSTNAME-sh
    

    This is with the ssh agent already running and to achieve that it was needed the passphrase.

    0 讨论(0)
提交回复
热议问题