Login with Linux FTP username and password

23

4

What's the command for logging in with FTP all with one line?

ftp username:password@my.domain.com

says:

Password required for username:password

Daniel Course

Posted 2012-03-03T16:26:38.370

Reputation:

2Use man ftp to find out, or maybe ftp --help. Don't forget that ftp may mean different utilities.... – Basile Starynkevitch – 2012-03-03T16:29:58.507

2ftp ftp://username:password@my.domain.com – None – 2012-03-03T16:35:39.580

1You should also remember that the commandline of a given process is visible to all the other users on the system. Therefore, giving your password as a part of the commandline may be a serious security issue. – None – 2012-03-03T19:59:53.983

Answers

9

ftp -nv yourftpserver.com

then user your_username or user anonymous


I posted this answer since ftp ftp://username:password@my.domain.com did not work for me.

Usage: { ftp | pftp } [-46pinegvtd] [hostname]
   -4: use IPv4 addresses only
   -6: use IPv6, nothing else
   -p: enable passive mode (default for pftp)
   -i: turn off prompting during mget
   -n: inhibit auto-login
   -e: disable readline support, if present
   -g: disable filename globbing
   -v: verbose mode
   -t: enable packet tracing [nonfunctional]
   -d: enable debugging

user391339

Posted 2012-03-03T16:26:38.370

Reputation: 593

2This isn't that useful because it requires interactive usage. I might as well just type 'ftp user@yourserver'. – smaudet – 2016-08-05T08:41:59.010

3this does not answer the question – phil294 – 2019-02-18T16:07:09.410

6

ftp ftp://username:password@my.domain.com

You could quite easily have used ftp --help though.

Jack

Posted 2012-03-03T16:26:38.370

Reputation: 250

27The command shown produces: "Name or service not known". ftp -help produces nothing like with ftp:, // nor username:password@. – CW Holeman II – 2014-12-11T16:54:13.587

4What version of Linux are you using? I also get "Name or service not known" when I try the above syntax. I'm using CentOS 6. – Tim Ludwinski – 2015-02-06T17:34:37.540

7I'm also getting Name or Service not known – Kevin Johnson – 2015-07-02T18:11:20.773

6doesn't work for me, fyi on a ubuntu remote server – user391339 – 2016-05-28T21:26:01.660

2Didn't work for me either. – smaudet – 2016-08-05T08:41:00.300

2This command does not work. – Rick – 2016-12-06T13:36:22.833

7I also found ftp -help unhelpful and the format you suggest doesn't work for me on ubuntu 16. – Henry – 2017-04-11T21:58:39.090

actually the command is "ftp -help", at least on Linux. Or "man ftp". – 0x4a6f4672 – 2012-07-26T08:30:07.960

4

The best option is to use a .netrc along with something like gpg for security purposes.

I've written a general purpose script for this, which I may upload later, but it boils down to:

gpg -c .netrc

or optionally with a passphrase on the commandline and an output destination:

gpg --passphrase <secretphrase> -o .netrc.gpg -c .netrc

Not shown here, but you could additionally use asymmetric keys (if you have them setup) with gpg to make this even more secure.

Then when you are ready to login

gpg .netrc.gpg
# or
gpg --passphrase <secretphrase> -o .netrc .netrc.gpg
ftp yourservername
rm .netrc

An example .netrc:

machine google.com
login <username>
password <secretpassword>

I actually keep a local hash and the original copy of these files on a different computer than the one I that I use the .netrc files on, and verify the hash of the .netrc and the script that I run, but that is above and beyond the OP's original question.

smaudet

Posted 2012-03-03T16:26:38.370

Reputation: 161

This is the best answer, although it can be improved: (1) user credentials within a single command line will be stored in shell history => security issues. (2) .netrc works also without gpg => security issues. Also check that the .netrc file has correct permissions: chmod 600 .netrc (3) a shell function as wrapper around the decrypt, ftp call, and removal of decrypted .netrc would be helpful. Thank you for your great answer! – math – 2019-05-29T06:57:40.987

So instead of the FTP password the user has to type the GPG passphrase? Also, if an attacker has write access to your personal files there are tons of ways of revealing your credentials as soon as you're using them. In that scenario GPG only really helps against an attacker with read-only access. Which is really rare, I guess? – Konrad – 2019-11-08T16:17:50.647

@Konrad So I'm less sure about the local files attach, but, no, and no, I think?

passphrase is only for people who want that, it is a bit silly to trade a password for a password, however you can map many separate passwords to one password.

Asymmetric you don't have to worry about any passphrase. Then you just use the first command, not the second (well you do, but just once).

Your concerns about local are not warranted, gpg takes care of its own local file permissions. And if you are rooted it doesn't matter... – smaudet – 2020-02-29T14:55:33.503

1

You can try

my_ftp() {
  ftp -i -n <<EOF
    open $HOST
    user "$USER" "$PASS"
    $@
EOF
}

which you then can call with my_ftp $'ls subfolder\nanothercommand'

This solution is not interactive but the best I could figure out

edit: You are probably best off to just use curl instead.

phil294

Posted 2012-03-03T16:26:38.370

Reputation: 123

1

Use netrc. It is better than giving the password away on the command line.

Nemo

Posted 2012-03-03T16:26:38.370

Reputation: 39

This answer does not deserve a negative score (though it is lacking an example of how to do it). – Peter Mortensen – 2016-05-28T11:28:25.660

I agree - however nobody ever changed this (and I didn't downvote it). – smaudet – 2016-08-05T08:40:16.267