Thursday, August 04, 2011

Linux Command Line Quick Intro

hehe... Quick.

So a friend of mine, let’s call him Aaron, it’s a good name. Anyways... Aaron is fairly familiar with Linux, but isn't overly friendly with the command line. As he puts it, he likes "pretty things" and we all have to admit there isn't much pretty about the terminal. So I compiled my entire bash history, sorted and wrote/stole some explanations of how I use terminal and regularly used command line programs. Hopefully this helps somebody, maybe you. It helped me at least as an exercise in documentation.
So here goes:

/var/log/messages is your friend. This is where a lot of the daemons log to. I often use this in conjunction with tail and/or grep.

Tab completion is your friend. Start typing a command and then press tab once to complete it and tab twice to list all possible completions, works on directories, commands and aliases

#output the results of a command to a file, called piping. In this case it is ls
ls > output.txt
ls >> output.txt // this appends the output of ls to the file output.txt

#execute one command based on the output of the other, specifically standard out, this is apparently called concurrency. The following will list all running processes in full detail and find only the lines with “named” on it.
ps -ef | grep named

#execute one command after another
cd /root; ls

#find out what version of Linux you have
uname -a && cat /etc/*release

#move files from one host to another over ssh, must use full filepath from /
scp root@172.16.1.2:/root/.bashrc /root/.bashrc

# request new ip using dhcp
sudo dhclient

#restart networking has the same effect of requesting new ip.
/etc/init.d/network restart

# redhat based, ie redhat, fedora, centos, scientific
yum update
yum search
yum install
yum info
yum remove

#debian based, ie debian, ubuntu, mint
apt-get update
apt-cache search
apt-get install

#port scan remote host, not overly abusive
sudo nmap -T2 -v -A -PN

# Find files
# where /etc is the directory to search can also be . for the current directory and all sup
# directories and search by name of file in this case *.cpp
find /etc -type f -name “*.cpp” -print | sort -u

#find text in a file in filesystem
find . | xargs grep "text"

# un gzip and un-tar, works on *.tar.gz and also *.tgz
tar -zxvf file.tar.gz

#edit /user/donald/.bashrc to modify coloring of terminal and regularly used aliases
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\] $ '

#the syntax for PS1 varies this above works in Fedora 13
export SVN_EDITOR="/bin/nano"

alias ll='ls -l --color=auto --group-directories-first' // I use ll ALL the time

#and if I find myself constantly changing to one directory I’ll often set an alias of
alias cdproject=”cd /home/aaron/Documents

#install ssh server
apt-get install openssh-server

#install Webmin on Ubuntu
sudo su
echo "deb http://download.webmin.com/download/repository sarge contrib" >> /etc/apt/sources.list
echo "deb http://webmin.mirror.somersettechsolutions.co.uk/repository sarge contrib">> /etc/apt/sources.list
wget http://www.webmin.com/jcameron-key.asc
apt-key add jcameron-key.asc
apt-get update
apt-get install webmin

#install LAMP on Ubuntu
apt-get install mysql-server mysql-client
apt-get install apache2
apt-get install php5 libapache2-mod-php5
apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
/etc/init.d/apache2 restart
echo -e "" > /var/www/info.php

Command My Description
alias print out all aliases currently in use, these are typically stored in your home directory in a file called .bashrc
cat .viminfo concatenate the output of the foo.txt to stdout or in otherwords print out the file
chmod+x install_webmin.sh change the mode of the file to be executable to all owners, groups and world
chmod 740 www same except explicitly defining the mode, b
Owner Group World
R W X R W X R W X
4 2 1 4 2 1 4 2 1
7 4 0
RWX R none

chown root:wheel manage.py Change the owner to root and the group to wheel of the file manage.py
chown root:root * -R Change the owner and group to root of all files and folders in the current directory and do it to all subfolders and files recursively
clear clear all text from the current terminal
cp
copies file or directories from source to destination, if wanting to recursively do directories, be sure to include -R
dd if=/dev/cdrom of=disc6.iso; eject create an iso of the disc in the drive and store it as disc6 in the current directory and eject aftwards
df display current disc utilization stat. Add -a for all discs and -h for human readable sizes
diff tmp.txt tmp2.txt display the difference between the two files specified
dig soecs.ku.edu dig up some dns info about the given server or domain
echo $PS1 print the contents of the given variable
echo d68d6c2e04c998>vmware.txt replace all contents of the given filename with the content specified, if filename doesn't exist, attempt to create the file
fg resume the last bg process
find . -size >1G find all files in the current directory larger than 1GB
find . -user jlfugett -print find all files in the current directory whose owner is jlfugett
find / -name *.sh -print find all files and where the extension .sh
find / -name *ldap* -ctime -0.04 find all files and folders where ldap appears in the filename created within the last hour, 0.04 is approx 1/24
find /etc -name klogd -print find all files and folders in /etc where the name of the file begins with klogd
g++ -c main.cpp compile main.cpp
gksudo wireshark run wireshark graphically as root, assuming you have gksudo installed
grep g++ -R * find all instances of the phrase "g++" in all files and subfolders
grep try *.py find all istances of the phrrase "try" in all files with .py extension in the current directory, do not recurse into subdirectories
groups <user> show all groups a user is a member of
gunzip totalvalidatorbasic.tar.gz just unzip the file total…tar.gzz
gzip totalvalidatorbasic.tar gzip the .tar file
hardinfo ubuntu: brings up the hardware info gui applet thingy
history print out all previously used terminal commands
history | grep nestat find all entries of the command netstat in history
hostname print the hostname
ifconfig <interface> print the current configuration of the specified network interface. If none is specified, all active interfaces will be printed
ifdown <interface> take the specified interface down
ifup <interface> bring the specified interface up
ip addr print the current ip configuration of all interfaces
iptables -L -n Print all current chains and rules in iptables
kill 1477 end the process given by the specified process id (PID)
less /etc/apache2/httpd.conf print the given file to the screen in a scrollable format. This is often used concurrently with another command i.e. "ll -a | less"
ln -s <source> <destination> create a symlink from the source file to the destination. Analagous to .lnk files in windows
locate PyQt4 search the locate database for files
lsof list open files
lsof -i tcp:80 list open files in use by port 80
man <command> get the manual or man page for the command specified, note program man must be installed and the command must have the man page installed, luckily both of these are typically done by default
md5sum <file> display the md5sum of a file specified
mkdir /home/donald make a directory called donald in /home
mv <source> <destination> move the file <source> to <destination>. I often use this instead of rename, because the end result is the same and mv is a lot less letters than rename
mysql -h localhost mysql client: connect to host specified
nano test.txt Nano is a basic text editor, open the specified file, if no file is specified, opens an empty file that can be saved on exit
netstat netstat is very similar to its windows counterpart,it shows all tcp/udp connections in or out of the host.
netstat -tan | grep LISTEN show ALL TCP connections in NUMERIC form, but only display the ones in listen state. Alternatively, use netstat -tanl
nslookup mail.google.com analogous to nslookup in windows, the internet recommends dig over nslookup
ntpdate -b <time server> set the time on the host to that specified by the given time server
ping time.ku.edu ICMP echo to host specified. I typically set an alias in my .bashrc ping="ping -c 5" because this will perform a never ending ping
ps --help get help for the command ps, the --help is useful for many commands
ps -ef print ALL(-e) running processes in FULL(-f) detail
ps -ef | grep wireshark same as above, except only show the lines with "wireshark" in them
pwd Print Working Directory, show what directory you're in
reboot reboot the computer, must be root or sudo
rm -rf Documents Forcibly(-f) and recursively(-r) remove the folder Documents and all of its subfiles and subfolders
rmdir Documents remove the empty directory Documents
route print the routing table
scp <username@host>:<file to send> <file to receive> Secure Copy, transfer a file over ssh, file paths must be finitely defined. i.e. scp root@172.16.1.2:/root/.bashrc /root/.bashrc
service most often used to start, stop or restart a service. Typically executing scripts in /etc/init.d/
service --status-all print the up/down status of all services installed
service networking restart Ubuntu restart networking
shred removes a file and overwrites the data.
sl Choo Choo!
smbclient -L -U Administrator -W Home.local server1.home.local command line Samba client. In this context, used to list shares of the server "server1.home.local" with the username "Administrator" of the domain/workgroup "home.local"
source ~/.bashrc After editing you're .bashrc file, run this to update to the most current version
sudo ssh root@firewall.home.local -L 2222:172.16.1.2:22 Secure Shell: the -L requires root permissions, hence the sudo. In this scenario, I'm shelling into a remote router firewall.home.local. And from that firewall, I am accessing ssh on the remote server 172.16.1.2 and redirecting that to my local box on port 2222, thus creating a tunnel.
startx if X is installed at a terminal, start it.
stat <filename> print out statistics about a file, permissions, dates, owners, groups, etc
su <username> assume the role of another user, if username is not specified, become root
tail -f -n 25 /var/log/messages print just the tail of the file specified, in this case -f follows the file. And -n 25 specifies print out the last 25 lines
tar -zxvf file.tar.gz un gzip and un-tar, works on *.tar.gz and also *.tgz
telnet <host> <port> Telnet into the host and port specified, not that is a space seperating the two. Since telnet has been effectively replaced by ssh, this is most often used to determine if a given service is up such as smtp, vpn, or imap
top shows the top running processes
tracepath <hostname> analogous to traceroute in windows
uname -a print out the current kernel version
uname -a && cat /etc/*release print out the current kernel version and distibution details
users prints out the users currently logged in.
vim tmp2.txt VIM!
wget https://dl-ssl.google.com/linux/linux_signing_key.pub web get, requests and downloads the file specified
whereis sysinfo locate the binary source and manual page files for a command
who prints out the users currently logged in.
whoami prints out who you are logged in, props to Sam Flynn in Tron:Legacy for using this
whois waterone.org get whois info for the domain specified, I believe this queries the root servers
wireshark Packet sniffer, very useful if you specify narrow filters, overwhelming if you don't

No comments: