Oct 26, 2013

10 Great Command Line Tricks

There are a great number of command line tricks. There are a few that are easy and good for less experienced Linux users too. These commands are worth remembering and memorizing.

Commnad line tricks, Linux, Bash, Data, Reference, Remote control, Reference, MPlayer, Bandwidth, SSH, Kiddies


As cited on tuxradar.com, here are 10 that such command line tricks -

1. Making your own Bash wormholes - The mkfifo command makes a pipe for sharing data, connecting two running utilities with a kind of command line wormhole. The data that is sent into one end will instantly appear at the other end. The 'fifo' component of the command makes reference to the nature of the pipe - the data that's first comes in and out. You can create the pipe by typing mkfifo, followed by the name that you want to call it. Once you have created the pipe, you only need to route data into it. For eg
mkfifo fifo_pipe
tail -f fifo_pipe

2. Remote control MPlayer – Mplayer can be controlled from a console, a shell script or even over the network. It tells the program to accept commands from the stdin stream in place of keystrokes. You can combine this with the -input option, and commands are read from a file, or a FIFO. For example, in one terminal you can put:
mkfifo ~/mplayer-control
mplayer -slave -input file=/home/user/mplayercontrol
filetoplay
In another terminal-
echo "pause" >~/mplayer-control

The command will pause the currently running MPlayer and issue a command that will resume playback.

3. Sharing files in an easy manner - File sharing with Samba or NFS is easy once it is set up on both computers. What about file transfer to another computer on the network without the need for setting up software? If the file size is small it can be emailed. In the event of the computers being in the same room and USB devices being used on both, you can use a USB flash drive. The other option is Woof. This is a Python script that can run on any Linux (or similar) computer. You can download the script from the homepage at www.home.unix-ag.org/ simon/woof.html and make it run. You can then share the file by using this command.
./woof /path/to/myfile

4. Locate lost files – If you save a file or a download and are unable to locate it then you can use -
find ~ -type f -mtime 0

This command will display all files in your home directory that have been modified or created today.

5. Bandwidth hogs - A solution to this is a handy script known as Wonder Shaper that makes use of tc (traffic control) command to limit overall bandwidth use to slightly below the maximum on offer. You can get it from http://lartc.org/wondershaper, put the wshaper script somewhere in your path - /usr/ local/bin is a good choice -and edit the start of the script to suit your system. Set DOWNLINK and UPLINK to just below your maximum bandwidth (in kilobits/s) and run it. You should now find that heavy uploads, like putting photos on Flickr, no longer drag your modem to its knees.

6. Fixing broken passwords with chroot - The chroot or (change root) command is for setting up a working environment within a directory. The root in the name refers to the root directory, not the root user (or superuser). Chroot installs a 'jailed' system within the specified directory which has no access to the rest of the system. For fixing a broken password, for example, you can boot from a live CD, mount your disk's root filesystem at /mnt/tmp and do this:
sudo -i
mount --bind /dev /mnt/tmp/dev
mount -t proc none /mnt/tmp/proc
chroot /mnt/bin/bash

7. Password-free SSH – By using SSH, connecting to a remote computer is convenient with certain disadvantages. You can type the password every time you connect. This can be annoying in an interactive shell but not acceptable with a script as you need the password in the script. The other way is by cracking the password. You can set up a SSH to work with no passwords at all. You need to first set up a pair of keys for SSH, using ssh-keygen like this to generate RSA keys (change the argument to dsa for DSA keys). ssh-keygen -t rsa

This leads to creation of two files in ~/.ssh, id_rsa (or id_dsa) with your private key and id_rsa. pub with your public key. You can now copy the public key to the remote computer and add it to the authorised keys list with - cat id_rsa.pub >>~/.ssh/authorized_keys

You can log out of the SSH session and restart. You need to repeat this for each user and each remote computer. For securing this further you need to add - PasswordAuthentication no
to /etc/ssh/sshd_config. This makes SSH to refuse all connection without a key, making password-cracking impossible.

8. Blocking script kiddies - There are many ways to avoid this. The best one is to close port 22 on your router for not allowing anyone to get in. Another option is running a program like Fail2ban (http://fail2ban.sourceforge. net) or DenyHosts (www.denyhosts.net). The third option is attempting to crack SSH by assuming it runs on the standard port 22; changing that to a random, high-numbered port and the crack attempts disappear. Edit /etc/ssh/sshd_config and change the Listen directive to something like this:
Listen 31337
and restart sshd. The only problem with this is the inconvenience of having to add this port number to the ssh command each time you log in but you can use an alias in order to take care of it.
alias myssh ssh -p 31337

9. Reclaiming disk space – By filling a partition to 100 per cent can lead to unpleasant effects on your system. If services and other programs cannot write to their log files, data cannot be saved in /var. To prevent this, the ext2 and ext3 filesystems reserve 5% of their capacity for only root processes to use. Tune2fs is used for tuning different parameters of an ext2 (or ext3) file system. You can use it to change the volume label or the number of mounts between forced execution of fsck and a host of other, more esoteric settings, but the options we are interested in here are -m and -r.
tune2fs -m 2 /dev/sda1

This leads to a reduction in the reserved area to 2 per cent of the filesystem. This may be great if you have a really large / or /var filesystem. In case you are using a 500GB drive or larger, this is the best option.

This line of code:
tune2fs -r 0 /dev/sda1

This sets the filesystem with no reserved blocks, a good setting for /home that doesn't require a reserved area for the superuser.

10. Making packages - When building from source with the use of standard autotools method of ./configure && make && make install, install CheckInstall first. You can get this from www.asic-linux. com.mx/~izto/checkinstall. Even though it is there in your distro's repositories. You can Run this in place of make install and, instead of installing the new files directly to your filesystem, it builds a package and then installs that. CheckInstall functions with Deb, RPM and Slackware packages. You can specify the type in a config file by - ./configure && make && checkinstall

0 comments:

Post a Comment