0

I'm trying to access the etc/fstab by the following command and getting the error.

Bash command:

sudo echo "swapfile none swap sw 0 0" >> etc/fstab

Error:

-bash: etc/fstab: No such file or directory

Then when I trying to check the existence of etc/fstab by ls -l and getting it there. But why I got an error in the first entry point?

Btw, I'm using Ubuntu server on my Virtual machine.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Danil Popov
  • 3
  • 1
  • 3

1 Answers1

5

Two issues here

  1. The file path needs the leading / here, so it should be /etc/fstab
  2. You'll then find that despite your sudo command you get a "permission denied" error. This is answered at Redirecting stdout to a file you don't have write permission on
roaima
  • 107,089
  • 14
  • 139
  • 261
  • Thank you, but in the second point as you mentioned I get an error about permissions. I tried to add "tee" keyword but error caused again. – Danil Popov Nov 24 '22 at 07:52
  • permission denied :) – Danil Popov Nov 24 '22 at 07:59
  • @danilpopov You need to modify the `tee` command with `sudo`, not the `echo` command. – Shadur Nov 24 '22 at 08:00
  • @Shadur Yes I did it, removed `echo`, and added a `sudo tee` . But it didn't helped. – Danil Popov Nov 24 '22 at 08:04
  • Please show us! Edit your question to add the new command and its result – roaima Nov 24 '22 at 08:10
  • @Danil Point (2) above explains this. Your non-sudo shell does all redirections *first* (because the commands in the pipeline have to inherit the file description when they are started). The sudo comes too late. Read the reference, please. – Paul_Pedant Nov 24 '22 at 09:56
  • ... At this point I *really* urge the OP not to monkey around with `/etc/fstab` using scripts they don't understand, because odds of them breaking something critical approach 1. – Shadur Nov 24 '22 at 13:10