Jun 142011
 

sudoPenso che sudo sia diventato un comando ampiamente utilizzato con Ubuntu, dove non si ha nemmeno una password di root, prima probabilmente è stato utilizzato solo in alcuni centri informatici per limitare l’accesso ad alcuni comandi.

sudo permette ad un utente autorizzato di eseguire un comando come super utente o un altro utente, come specificato nel file sudoers.

In questo articolo vi mostrerò alcuni usi un po meno comuni di questo comando, per una descrizione generale del comando consiglio di leggere la pagina di wikipedia dedicata


1)

 sudo !!

si voleva dare un comando come root ma avete dimenticato di usare sudo? Non preoccupatevi con questo comando si usa la history in combinazione con sudo, l’argomento !! ha lo stesso significato che !-1, quindi questo comando esegue l’ultimo comando dato come root.

Ovviamente questo potrebbe essere utilizzato anche come

 sudo !-2

se si dà un altro comando dopo quello che si desidera eseguire come root, ma attenzione al comando che si manda in esecuzione come root .

2)

 sudo -i

è possibile utilizzare questo comando per avere una shell interattiva come root.
Per avere una shell con un altro utente utilizzare il formato

 sudo -u username -i

L’opzione -i ( simula login iniziale) consente di eseguire la shell specificata nella voce passwd dell’utente target come una shell di login. Ciò significa che file di risorse specifici del login come .profile o .login verranno letti dalla shell. Se un comando viene specificato, viene passato alla shell per l’esecuzione. In caso contrario, una shell interattiva viene eseguita.

3) Sudo, redirezione dell’output, grazie a petur.eu:

Siamo di fronte a un problema quando si cerca di reindirizzare con sudo, in quanto la seconda parte del comando non viene eseguito con privilegi di root.

sudo comando > fileoutput
<>—-root——<>—user—<>

La soluzione è quella di utilizzare;

‘sudo tee’ instead of the ‘>’ operator,
‘sudo tee -a’ instead of the ‘>>’ operator.

sudo comando | sudo tee outputfile
<>—–root—–<>——-root——<>

4)

 :w !sudo tee %

Questo comando può essere utilizzato nel vostro vi/vim per salvare un file che avete aperto e modificato come utente, ma che non potete salvare con il vostro username. In questo modo ci si risparmia il solito salvare il file in /tmp, diventare root, spostare il file da /tmp alla posizione reale.



Popular Posts:

flattr this!

  11 Responses to “4 Trucchi con sudo”

  1. “I think that sudo is become a wide used command with Ubuntu, where you don’t even have a root password, before that probably it was used only in some data-centers to restrict access to some commands.”

    Seriously?!?

    Sudo has been around in BSD since 1980. That predates the first Linux kernel by 11 years so Ubuntu was not even a thought when sudo was becoming “wide used”.

    A Breif History of Sudo

    Since Ubuntu was not even publicly released until 2004 it predates Ubuntu by 24 years!

    A Breif History of Ubuntu

    Just because *YOU* are new to these commands and *YOU* are using them more often now does not mean that they are just now becoming more “wide used”.

    Please check your facts before you blindly type things like “sudo is become a wide used command with Ubuntu”. Ubuntu, is a good distribution *BECAUSE* of widely used GNU commands like sudo and others not the other way around.

    • Hello David,

      I used Sudo on AIX before Ubuntu even existed, so i know that it’s a old and solid command that is part of Unix/BSD standard.
      I meant that the use of sudo in the desktop version of GNU/Linux was not so widespread before ubuntu, is this better?

      Peace :)

      • Sorry if I came across a bit gruff in my comment, I just get a bit overly critical I guess when I see all the credit that keeps going towards Ubuntu. No offense, I appreciate Ubuntu for it’s marketing and drive to make Windows users more comfortable with Linux. It’s just that as a Long time Linux user (pre 2000) I know that many things that Ubuntu takes credit for were and are achieved by larger Linux Distributions like Redhat, Suse, and Debian (among many others).

        For instance sudo was very widly used in Redhat/Alliance days (1999) in data centers long before Ubuntu was even released. Ubuntu does a nice job of promoting the use of Linux as a whole but I am not sure I agree with the statement that sudo was not as widely used before Ubuntu and that Ubuntu is directly related to any growth in it’s usage.

        That said, agree or not, you have shown some nice usages of sudo here and I have another that can come in handy…

        If you want to ssh to a server and run a command on the other server as root you cannot simply use the command:

        ssh someserver sudo whoami

        It will fail with the error “sudo: sorry, you must have a tty to run sudo”

        However you can tell ssh to allocate a tty for it’s session using the “-t” flag as follows:

        ssh -t someserver sudo whoami
  2. Very informative and useful article on the use of sudo. I learned quite a few things from it that I plan to use in my Ubuntu installation. Keep up the good work!

  3. Nice!

  4. sudo bash -c ‘command > outputfile’

  5. Hi All,
    I have a problem here. I have logged in linux with my id, then sudo’d (sudo su – xyz) with my password into an account permissible only to view the log prints.
    Now, I need to edit some of the exploded files in the domain from my app, but it has permission other than root (abc).
    I did try :w !sudo tee % after I edited my read-only file (for my sudo’d account xyz) but it is prompting me to enter the sudo password for xyz. I entered properly but still it is not taking.

    Any workaround to get this working? Any help/leads are appreciated.

    Thanks!!

    • If you have the password of xyz, why you use (sudo su – xyz ?)

      If the account xyz can become root (i don’t know the setup of your machine), :w !sudo tee % should work.
      Or just do “sudo -i” become root and edit the files, or change the permissions/ownership of the files, if this is possible.

  6. sudo sudo su –

 Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>