orac

Orac is thebaldgeeks online memory.
command line tips and tricks

Stuff that I don’t use often enough to remember. (And stuff that I do remember, but might be handy for someone starting out in Linux. i.e pretty much all the stuff Dan taught me).

Some helpful web sites for ubuntu server (ie, no gui);  http://linuxservertutorials.blogspot.com/

sudo !! to redo the last command that you forgot to do as sudo.

What processes are listening on what ports?
sudo lsof -i
sudo netstat -lptu
sudo netstat -tulpn

Open a port on ufw.
sudo ufw allow 22


putty

You got to have it. Simple as that.  http://www.chiark.greenend.org.uk/~sgtatham/putty/
If you have it, you got to have it tabbed.  http://en.poderosa.org/
Simple as that.
Its nice to set it up a little ‘before’ you use it;  http://dag.wieers.com/blog/content/improving-putty-settings-on-windows
If you have already set up some connections. You have to load them, change the settings, then hit save. Then you can open the connection and see how it looks.

config file for your SSH connection is located here; /etc/ssh/sshd_config


tmux

For when you need to run a process and leave it running in Putty.
apt-get install tmux (probably don’t need to do that, should be already installed).
tmux
To disconnect, ctrl+b then d
tmux attach
name each session using Ctrl-B and $. You can get a list of the currently running sessions using tmux list-sessions.

Another way to have a running task in Putty;

nohup long-running-command &

If you want to “background” some already running tasks, then your best bet is to ctrl+z then run bg then a quick disown should keep the process running after you log out.

History

history Show the last commands you have used.
!xxx where xxx is the history number. This will re-run that command. (pretty handy for long commands).
Hit ctrl+r at the command line, it will allow you to do a search of the history commands. Sometimes this is quicker than looking back though the history and then doing the !xxx trick.
history |grep command will display a list of all the times you used that command in your history. Pretty handy. Almost better than the above which only gets the last one (which may not be the right one).
!!:$ will get the previous argument and pass it to the current command.
eg, lets say you do ‘more /var/etc/httpd.conf’ then if you want to nano that, do ‘nano !!:$’


cli apps

sudo apt-get install ncdu Visual command line app to find out whats taking up space on your hard drive. Awesome.

sudo apt-get install lm-sensors
sudo sensors-detect Answer every question with ‘yes’, when it asks if you want to auto insert the config to /etc/modules say yes.
sudo /etc/init.d/module-init-tools start
Get the CPU and HDD temps with the command sensors
(Note, the 4 coretemps are the four cores on the CPU. Yes, the little Acers are quad cores…).

sudo apt-get install htop
htop This gives a terminal version of processor load, memory usage and top process. A lot like ‘top’ only better looking? Use ctrl-c to exit (Or just open another putty session and let it run).

sudo apt-get install siege
siege -c50 -t1M www.opto22.com This will stress test a web server (www.opto22.com) with 50 connected users for 1 minute.

sudo apt-get install ntop
sudo ntop To monitor network traffic, open a browser and open to the web servers address:3000 (you might have to do a port forward for this to work).

sudo apt-get install iftop
sudo iftop This will open a terminal display of the network interface traffic. ctrl-c to exit.
sudo iftop -i eth0 -f “dst port 8443” shows only data for port 8443

sudo apt-get install nethogs
sudo nethogs This will open a terminal display of the network interface traffic. ctrl-c to exit. (It shows different and very useful connection information from iftop).

sudo apt-get install iptraf Pretty interesting ncurses based IP LAN monitor.

sudo apt-get install whowatch Not much point on a home machine, but shows whos logged in and what they are doing.

sudo apt-get install apachetop Shows the load on apache.

sudo apt-get install iotop who is writing to the hard drive.

sudo apt-get install bmon graphical network monitor, pretty simple, but rather handy.

sudo apt-get install mytop shows what/who is connected to your mysql database.

fuser ; look into this one. for example fuser 80/tcp and fuser -vm /dev/sda1

sudo apt-get install procinfo a lot like atop, but very good info

sudo apt-get install vnstat then sudo vnstat -u -i eth0 then, after a while, sudo vnstat Takes a while to gather the data. Nice because it does not sniff the interface direct. Not-nice because it takes 24 hours to get data. Nice because it will get monthly data with no hit on performance. See more from its web page;  http://humdi.net/vnstat/


find

sudo find / -name FILE.name find the file from the root directory. (Case sensitive)
sudo find / -iname File.Name case insensitive.
sudo find / name ‘*.txt’ note the quotes around the wild card.
find ~/Images/Screenshots -size +500k -iname ‘*.jpg’ find all jpgs in the users home directory larger than 500k.
sudo find . -perm /777 will find every file from root that has their permissions set for 777.
sudo find /var/www -perm /777 will find every file in the www directory that has their permissions set for 777.
find /home -user joe find every file in the home path owned by the user joe.
find $HOME -mtime 0 find every file in the users home directory that has been modified in the past 24hours.
sudo find / -mmin -10 find every file that has been changed in the last 10 minutes.


grep

grep html index.html will display each line with the text ‘html’ in that file.
grep html /var/www/* will display each line of each file in the path.
grep ‘beno.id.au’ -F -r /var/www will display every file and the matching text in that file.
grep -i “text” nameoffile will search for the word ‘the’ in the file.


unzip untar gzip

tar tvf archive_name.tar
unzip test.zip
tar -xzf rebol.tar.gz
gunzip rebol.gz


Assorted

tail -f then the file name. This will give you a ‘live’ feed of the end of the file, great for apache logs. whereis firefox Path to the file name
which firefox path to the executable file

dpkg -L jetty Find all of the owned files and directories of a package.

diff -w file1.txt file2.txt Will show the differences and ignore white spaces (pretty handy).
colordiff file1.txt file2.txt Will show the differences in pretty colors.
diff -rq folder-1 folder-2 Will show the diff between two folders

cd – toggles between the last two directory’s. HUGE TIME SAVER”’

Change users home directory;
sudo nano /etc/passwd
For example change from /home/beno to /var/www
When beno logs in, he will be in the web root as his home directory (permissions is a whole other issue).

free -m show how much ram is in and free in megs’
df -h show how much disk space is around in megs’

ps -ef | grep firefox to find out the process ID of firefox
kill -9 1234 to kill the PID 1234

rm -r dirname will delete all files in dirname and all files and directories under it!!!
sudo find . -name “*.xml” -delete or find /home/dev/unsecured -name “*” -delete  will delete all .xml files or all files in a directory (If there are a lot of them ‘rm *.xml’ may fail)
cp file1 file2 will copy file1 to file2.
mv file1 file2 will rename file1 to file2.
sudo mv * /var/www/someotherdir/ move everything from where you are to the new directory.
xcopy c:\opto22 p:\optobackup\ /D /S /E /V /Y /C Just checking if your paying attention.

mkdir /home/beno/oldhdd make a directory.
rmdir remove a directory (must be empty, if not empty, use the rm command).
mount /dev/sda1 /home/beno/oldhdd mount the drive into the directory.

watch ls -la watch will do the command specified every two seconds (default). -d will show the diff on the command

env will show a lot of the system parameters.

lshw More than you want to know about your hardware.
————————————————————————-
scp -r -v /var/www/* beno@1.1.1.2:/var/www/. scp to copy files from server to server. In this example copy all the web files and directories from the first listed server to the remote server, and print out status as you do it (probably will need the base directory to exist on the first server (ie, the destination)).
scp -P 2202 -r Sandbox/*  host@host.net:/var/www/tomcatroot/ will copy that whole directory over.

From cmd prompt in Windows.
Download pscp.exe from putty page.
cd to where the file you want to send from windows to Lunix is (optional, but I find it easier).
pscp -P 2222 file.name.to.send username@thebaldgeek.net:.

————————————————————————

time +%s will show the time in unix epoch (seconds since 1/1/1970)

w3m beno.id.au w3m is the web browser that comes installed with Ubuntu server.

sudo netstat -nlp to list what ports are open and listing for connections on the server (note, ::: means its listening to everyone).

dmesg | grep -i sata | grep ‘link up’ Will grep the list to see what sata devices are up. Used to check what sata speed your drives are hooked to.


GNOME

To reset your desktop back to defaults;
rm -rf .gnome .gnome2 .gconf .gconfd .metacity In a nut shell, this just deletes all the stuff that you have messed with when it comes to gnome. By deleting it, next time you log in via the GUI, it just builds it all from scratch. Any settings you changed in hardware, display, x-server, network etc will not be undone.
Quick version of the above, rm -rf ~/.*


Passwords

To change your own password, passwd
To change a different users password, sudo passwd mranderson


ls (directory listings)

ls -lh directory listing in human readable form.
ls -la directory listing showing all files (even hidden).
ls -ltr directory listing showing files in order based on last modified time.
add alias lt=’ls -Alhtr’ in $homedir/.bashrc from then on, you can just do a ‘lt’ from the command line and get a directory listing by time.


shutdown / reboot

shutdown -h now Halt all processes nicely and shut the pc down now.
shutdown -h +10 Halt all processes nicely and shut the pc down in 10 minutes time.
shutdown -r now Reboot the PC now.
reboot -h now Reboot the PC now.


Crontab

00 11,16 * * * /home/beno/incremental-backup do a backup every day at 11 and 4pm.
00 09-18 * * * /home/beno/check-db-status do a job every day at the top of the hour between 9am and 6pm.
* * * * * /home/beno/scanner do a job every minute. Note, this is as fast as you can do something with crontab.
*/10 * * * * /home/beno/job do a job every 10 minutes.
@reboot /home/beno/startjob do a job at reboot. (Pretty handy).

crontab -l Show the crontab jobs for the current logged in user. [BR]] crontab -e edit the crontab jobs for the current logged in user.

Note, all paths used in crontab must be absolute!


chmod/chown

chmod –reference=file1 file2 will set file2 permissions the same as file 1.
chmod -R 755 directory-name/ will change all files in all directories from there down!

Following are the symbolic representation of three different roles:

u is for user,
g is for group,
and o is for others.

Following are the symbolic representation of three different permissions:

r is for read permission,
w is for write permission,
x is for execute permission.

chmod u+x filename add execute permission just for the user.
chmod u+r,g+x filename add user read and group execute .

chown beno:www-data filename.txt change the owner to beno and the group to www-data on the file. Use -R to go deep.


Versions

ifconfig -a show the network configuration.
uname -a to show kernel name etc.
whatis ifconfig show what a command is about.
lsb_release -a show what version of Ubuntu you have installed.
cat /etc/os-release better way to show what version of Ubuntu you have.
uname -m show 32 or 64 bit (its in the -a result as well, just easier to read like this).
dpkg –print-architecture This is a much better way to show if you have 64 or 32 bit. (But only works on Debian)
getconf LONG_BIT This another way to show if you have 64 or 32 bit.
echo $PATH show whats in your path. (Not a bad idea to copy and store this somewhere).
more /proc/cpuinfo
cat /proc/cpuinfo
lscpu


apt get

sudo apt-get install package Install the package
sudo apt-get -f install package Force the package to install even if it previously complained about unmet dependencies.
sudo apt-cache search package Look for the string ‘package’ (hint, use a specific name or grep to narrow the results, eg apt-cache search filename | grep -w filename).
sudo apt-cache show package Show any info on the package, version, description etc.
sudo apt-get update Run this command after changing /etc/apt/sources.list
sudo apt-get upgrade Do an upgrade on all packages that have outstanding upgrades.
sudo apt-get dist-upgrade Go from 9.04 to 10.04 for example.
sudo apt-get autoclean removes all .deb packages from your drive (could give you back a chunk of space), check ‘/var/cache/apt/archives’ before and after to see the difference.
sudo apt-get clean removes ALL packages. The command du -sh /var/cache/apt/archives will tell you how much space those packages are taking up.
sudo dpkg-reconfigure package_name reconfigure the package.
sudo apt-get remove package uninstall the package.
sudo apt-get purge package will remove the package and any config files (very handy for a bloke like Ben).

sudo tasksel nice way to install groups of packages, ie, if you want to make a LAMP server from a stock install.


samba share

First, back up your current samba config file; sudo cp /etc/samba/smb.conf-sample /etc/samba/smb.conf.orignal
Next, chuck something like this at the end for each directory you want to share with your windows box.
[web]

comment = web stuff
path = /var/www
public = yes
writable = yes

Then, restart samba sudo service smbd restart

Adding authenticated users is useful, other wise you need chmod 777 all the files.
 http://www.howtogeek.com/howto/ubuntu/create-a-samba-user-on-ubuntu/


internet speed test

(Bit like a command line version of the flash speedtest.net)

curl -o /dev/null  http://speedtest.wdc01.softlayer.com/downloads/test500.zip
sudo iftop run this in one terminal while running the above in another for all the data you need.


Ping sweep from command line

for i in {1..254}; do ping -c 1 -W 1 10.1.1.$i | grep ‘from’; done
Just change the 10.1.1. to your address range.


Find all SNMP devices on network from command line

nmap -sU -p161 –script snmp-brute –script-args snmplist=community.lst 192.168.1.0/24
Just change the 192.168 blah blah to your address range.


Hard drive read / write speed test

Write speed.
dd if=/dev/zero of=test.tmp oflag=direct bs=500K count=1024
You want to see something like 14+ MB/S (this is what the Pi gets)

Read speed.
dd if=test.tmp of=/dev/null iflag=direct bs=500K count=1024
Here you want to see something north of 32 MB/s


Benchmark test

To install UnixBench, run the following commands:

sudo apt-get install build-essential libx11-dev libgl1-mesa-dev libxext-dev
wget http://byte-unixbench.googlecode.com/files/UnixBench5.1.3.tgz
tar xvfz UnixBench5.1.3.tgz
cd UnixBench
./Run

Unixbench test descriptions.


Find out your real world (WWW) IP address from command line

curl icanhazip.com

Networking Tools

To find out all open (listening) ports on the box:

netstat peanut

Starwars in command shell

telnet towel.blinkenlights.nl


Autostart on boot

If you want to start a program on boot, but don’t want to use @reboot in crontab, it’s a two step process. Create a .sh file and then run that at boot.
Make the file chrome.sh (and chmod +x it)
#!/bin/bash
(sleep 30 && chromium-browser http://127.0.0.1:8080) &

Then put this in ~/.config/autostart as chrome.desktop
[Desktop Entry]
Name=Chrome
Exec=/opt/groov/chrome.sh
Type=Application

Leave a comment