Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands by 0disse0 from sorted by
Terminal - Commands by 0disse0 - 56 results
setserial -q /dev/ttyS0
2012-02-08 20:44:09
User: 0disse0
2

I actually planned to do this for quite a long time, but since I haven't had any suitable client hardware, I procrastinated this.

Now, the old laptop I've got from my dad, features an RS-232 port. So, now that I had technically a client, that I could test my RS-232 connection with, I ordered a null modem cable.

There is no RS-232 outlet on my desktop computer directly on the mainboard, but theres a connector on the mainbord, where a RS-232 outlet can be attached to. The outlet will then cover up a PCI slot.

# Activating RS-232

Ok, once all cables were in place, I tried to access both RS-232 ports by reading from them directly. They're usually ttyS0 or ttyS1, depending what COM-Port it is.

From the file /proc/tty/driver/serial, information about the serial setup can be obtained.

A setserial -q /dev/ttyS0 might be usefull as well. Usually, the UART Type is 16550A, on a standard PC. Mine wasn't working though.

At leas not right from the start, when I tried to read the interface with cat /dev/ttyS0 I got the following error:

# cat /dev/ttyS0

cat: /dev/ttyS0: Input/output error

Obviously, the driver couldn't activate the hardware. Reason was, it was deactivated in BIOS. After activating RS-232 there, it worked well.

As a last action, I added myself to the uucp group, so I have user permission to the serial lines. It is not necessary for the terminal setup, but a good idea to do so, just for future projects, maybe...

# Setting up a terminal

Once the Serial line is configured and working properly, it's time to let a terminal run on that port. This is what I added to my /etc/inittab :

s0:2345:respawn:/sbin/agetty -L 38400 ttyS0

I added it quite on the top of that file, right below the 'si' statement, mingetty cannot be used for serial connections, it cannot be run in a console, too. I tried it for testing purposes, but the cosole - along with your login program - will log you out, as soon as you log in over your serial line.

'-L' means this is a local line, with no carrier signal. 38400 is the standard speed of a Linux console, it might be a bit high, I was told, but it works well. I tested that with some higher values as well (115200) and it worked too, I guess it tepends on things like cable length, etc. Last parameter, is the serial tty to listen on. The terminal type can be specified as an additional parameter at the end of the parameter list, vt102, for instance. This is sometimes required, depending on the client.

After finishing editing /etc/inittab, an init q will make the system re-read /etc/inittab and apply changes. The agetty should now be listening on ttyS0.

#Setting up a client

It's time to establish a connection and test the serial line. I use a laptop, that has an RS-232 port, so some preliminary setup is required. I tried minicom as terminal initially, but it turned out, not to be the best client. It initializes the modem, this lasts quite long, and it doesn't convey ANSI colors. So the better option is cu, it's part of the UUCP-Package.

Oh, and the serial port of that computer, has to be accessible as well, of course.

Once everything was set up, I established the connection:

cu -l ttyS0 -38400 --nostop

Pretty self explanatory, I think. The --nostop option disables XON/XOFF handling.

# root access over ttyS0

In order to become root over the serial terminal, the tty needs to be added to /etc/securetty

I appended ttyS0 to the end of the file. It is now possible, to gain root access over the serial terminal. The agetty process needs to be restarted to apply changes.

# Accessing GRUB over ttyS0

To make bootloader access possible over ttyS0, some changes to /boot/grub/menu.lst need to be done. (GRUB is the bootloader I use, I suppose LiLo has similar capabilities.)

Those are the lines, I appended to the top of my menu.lst :

serial --unit=0 --speed=38400 --word=8 --parity=no --stop=1

terminal --timeout=3 serial console

The serial command initiates the serial terminal option, --unit=0 defines our first serial connector, I my case, it's the only one I have on my machine. I used the standard Linux-Console speed, as well as the "8N1" connection strategy.

terminal defines the terminal priorities, first terminal (serial) is the standard one, the last one is the secondary terminal (console).

--timeout=3 enables a delay on both consoles, with a prompt for a keystroke. Depending on which terminal, the key is pressed, this terminal, will be used. If no key is pressed after the timeout, the standard console (in my case serial) will be used.

# Relaying Kernel output on boot

The Kernel accepts multiple console options, of which the last one, is the standard console, and the one that will be used in Single User mode. These are my Kernel options:

title Fedora Core (2.6.20-1.2316.fc5)

root (hd0,0)

kernel /vmlinuz-2.6.20-1.2316.fc5 ro root=/dev/VolGroup00/LogVol00 rhgb quiet vga=795 console=tty0 console=ttyS0,38400

initrd /initrd-2.6.20-1.2316.fc5.img

console=tty0 is the standard console, located on the machine, i.e. monitor and keyboard.

ssh -f -N -R 8888:localhost:22 user@somedomain.org
2012-02-08 20:24:38
User: 0disse0
Functions: ssh
Tags: ssh
5

this command from the source server and this follow in the destination server:

ssh user@localhost -p 8888

useradd -m -p $(perl -e'print crypt("passwordscelta", "stigghiola")') user
2012-02-06 19:53:01
User: 0disse0
Functions: perl useradd
0

The crypt function takes a password, key, as a string, and a salt character array which is described below, and returns a printable ASCII string which starts with another salt. It is believed that, given the output of the function, the best way to find a key that will produce that output is to guess values of key until the original value of key is found.

from http://en.wikipedia.org/wiki/Crypt_(Unix)

sudo hal-get-property --udi /org/freedesktop/Hal/devices/computer --key 'system.formfactor'
sudo hal-get-property --udi /org/freedesktop/Hal/devices/computer --key 'system.hardware.product'
netsh firewall add programmaautorizzato C:\nltest.exe mltest enable
2012-02-06 19:33:57
User: 0disse0
1

To allow a program ("programmaautorizzato" in example) to listen through the firewall

taskkill /F /FI "USERNAME eq Cicciopalla"
2012-02-05 12:00:52
User: 0disse0
-5

The taskkill command has the option of the curve, for a complete list just type: taskkill /? As we know, but to give a practical example, suppose you want to act only on the user's processes Cicciopalla use this command

DISPLAY=":0.0" import -window root screenshot.png
2012-02-05 11:52:33
User: 0disse0
Tags: display
0

But if you use a screen saver, you must first disable your screen saver

kill $(ps ax | grep screensaver | grep -v grep | awk '{ print $1 }')

VBoxManage modifyvm "vm-name" --vrdp on --vrdpport 3389 --vrdpauthtype external
2012-02-03 18:36:04
User: 0disse0
-1

--vrdp on enables VirtualBox RDP server for the VM

--vrdpport 3389 ndicates the TCP port that the server will accept RDP connections direct to the VM (for each VM is assigned a different port)

--vrdpauthtype external RDP console gives access to the VM Host physical users via authentication

In fact, in the 3.1.x version of VirtualBox, the external value for the parameter --vrdpauthtype allows access via RDP only to the user who started the VM.

The workaround is to add the user that runs the VM to shadow group, using the command

sudo svn co svn+ ciccio_diverso://root@192.160.150.151/svn-repo/progettino
2012-02-03 18:16:02
User: 0disse0
Functions: sudo
Tags: subversion
0

first need to Edit the configuration file

/home/cicciobomba/.subversion

and under the [tunnels] add this line

ciccio_diverso = /usr/bin/ssh -p 12345

svn diff --diff-cmd='meld' -r 100:BASE FILE
find /protocollo/paflow -type f -mtime +5 | xargs tar -cvf /var/dump-protocollo/`date '+%d%m%Y'_archive.tar`
2012-02-03 16:24:08
User: 0disse0
Functions: find tar xargs
1

Finally, we can make the file "unchangeable"

sudo chattr +i

tasksel list-tasks
2012-02-03 16:13:17
User: 0disse0
Tags: Ubuntu install
1

The command tasksel allows the choice of packages from the command line to get predefined configurations for specific services (usually this option is offered during installation).

TOTAL_RAM=`free | head -n 2 | tail -n 1 | awk '{ print $2 }'`; PROC_RSS=`ps axo rss,comm | grep [h]ttpd | awk '{ TOTAL += $1 } END { print TOTAL }'`; PROC_PCT=`echo "scale=4; ( $PROC_RSS/$TOTAL_RAM ) * 100" | bc`; echo "RAM Used by HTTP: $PROC_PCT%"
mysqldump --opt --where="true LIMIT 5000" dbinproduzione > miodbditest.sql
2012-02-02 11:50:35
User: 0disse0
Tags: mysqldump
1

How to extract data from one table:

mysqldump --opt --where="true LIMIT 5000" dbinproduzione tabella > miodbditest_tabella.sql

VBoxManage closemedium dvd "/sicuramente/mipaghi/tutto.iso
VBoxManage storageattach "volpedimongibello" --storagectl "fighetto" --port 1 --device 0 --type dvddrive --medium "/tanto/mipaghi/tutto.iso
2012-01-31 14:42:54
User: 0disse0
-1

was inspired by http://www.commandlinefu.com/commands/view/8936/boot-from-a-block-device-without-giving-root-privilege-to-virtual-box

volpedimongibello= virtual machine name

fighetto= controller name

tutto.iso= DVD iso image

How to remove the DVD:

VBoxManage storageattach "volpedimongibello" --storagectl "fighetto" --port 1 --device 0 --type dvddrive --medium none

stsadm -o addalternatedomain -url http://paperino.paperopoli.com -urlzone Internet -incomingurl http://quiquoqua.paperopoli.com
2011-07-09 11:32:45
User: 0disse0
-2

How to add an "alternate access mapping" from the command line and avoid passing on Friday evening following the interpretation of the all-too-terse "log" of SharePoint?

ntfsfix /dev/hda1
2011-07-02 17:49:48
User: 0disse0
Tags: ntfs
-4

This command marks it as "dirty" NTFS partition, forcing Windows to do the scandisk during boot. It is convenient if you work hard to NTFS partitions under Linux

ntfsresize --size X[k,M.G] /dev/hda1
2011-07-02 17:47:05
User: 0disse0
Tags: ntfs
-2

With this command you can resize an NTFS partition by specifying the new size (X) in Kbytes, Mbytes or Gbytes. If you plan to do this it is advisable to precede --no-action parameter to size

see more:

http://ubuntuforums.org/showthread.php?t=1244058

and

http://en.wikipedia.org/wiki/Ntfsresize

mkntfs /dev/hda1
2011-07-02 17:43:16
User: 0disse0
Tags: windows ntfs
-3

With this command you can create an empty NTFS partition. The command is useful if, for example, we want to format a previous installation of Windows and reinstall before you want to restore some files on the partition.

ntfsclone
2011-07-02 17:37:19
User: 0disse0
Tags: windows ntfs
-3

Although not frequently used, it is possible to clone an NTFS partition to an image file and, where necessary, restore the image to another partition. This command is useful, for example, if we want to keep a backup copy of our Windows installation, or in a corporate environment to install or repair a Windows of one or more computers. For the command syntax is refer to the documentation (man ntfsclone)

VBoxManage modifyvm "vm-name" --memory 256 --acpi on --ioapic off --pae on --hwvirtex on --nestedpaging on
2011-06-16 18:14:47
User: 0disse0
1

where

- memory 256 assign 256 Mb RAM

- acpi on enable ACPI (mandatory if you use Winfog 2000

- ioapic off disable the IO APIC. Not useful if you use one CPU (on virtual machine or a 32 bit operative system). As ACPI, this switch is mandatory for Winbug 2000

- pae on enable the Phisical Address Extension how to use more than 4Gb of RAM on x86 CPU

- hwvirtex on enables hardware virtualization extensions for microprocessors that have this feature (which should be also enabled in the BIOS of the motherboard)

- nestedpaging on allows part of the processes of memory management hardware are made directly

net rpc -I indirizzoip -U nomeutente%password servizio {stop|start} nomedelservizio
watch vmstat -sSM