Table of Contents
INSTALL A PACKAGE
sudo dpkg -i DEB_PACKAGE
REMOVE A PACKAGE
sudo dpkg -r PACKAGE_NAME
Install Chrome
sudo apt-get install libxss1 libappindicator1 libindicator7 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo dpkg -i google-chrome*.deb
BASH: difference between executing and sourcing
Source:\\http://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-scrip/176788#176788
Consider following script pid.sh:
#!/bin/sh echo $$
(the special variable $$ expands to the PID of the current running shell process)
First print the PID of the current shell:
$ echo $$ 25009
Source the script:
$ source pid.sh 25009
Execute the script, note the PID:
$ ./pid.sh 25011
Source again:
$ source pid.sh 25009
Execute again:
$ ./pid.sh 25013
You can see that sourcing the script runs in the same process while executing the script creates a new process everytime. That new process is the new shell which was created for the execution of the script. Sourcing the script does not create a new shell and thus the PID stays the same.
Summary
Both sourcing and executing the script will run the commands in the script line by line, as if you typed those commands by hand line by line.
The differences are:
- When you execute the script you are opening a new shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed.
- When you source the script you are typing the commands in your current shell. Any changes to the environment will take effect and stay in your current shell.
Use source if you want the script to change the environment in your currently running shell. Use execute otherwise.
para ejecutar archivo BASH con exits sin salir del terminal
#! test.sh echo hola exit
a) cambiar exit por return y ejecutar con source o . (punto) b) ejecutar con sh
sh test.sh
Use of ports
lsof -i :3000
Scheduled shutdown
notifying all users prior to shutdown as in:
$ sudo shutdown -h 10:00 "Shutting down for scheduled maintenance."
Shutdown
shutdown -h halt poweroff
Reboot
shutdown -r reboot
Locating applications
Folders: /bin, /usr/bin,/sbin,/usr/sbin, /opt.
$ which diff $ whereis diff
Finding files
locate & find
Searching for files and directories named “gcc”:
$ find /usr -name gcc
Searching only for directories named “gcc”:
$ find /usr -type d -name gcc
Searching only for regular files named “test1”:
$ find /usr -type f -name test1
Finding based on time:
$ find / -ctime 3
Finding based on size
$ find / -size +10M
Find files and copy
$ find ./uploads -iname "taller*" -type f -exec cp -p {} /home/alfredo/projects/shk/app/assets/images/fondos/ \;
wildcards
? Matches any single character * Matches any string of characters [set] Matches any character in the set of characters, for example [adf] will match any occurrence of “a”, “d”, or “f” [!set] Matches any character not in the set of characters
Find and execute a command
To find and remove all files that end with .swp:
$ find -name "*.swp" -exec rm {} ’;’
Filesystem
tree -d
To see mounted fs permissions
mount
To see static file system information
cat /etc/fstab
Disk Free space
df -Th
Network File System NFS
On the server
NFS service
sudo service nfs start
Text file with shared folders and permissions
# /etc/exports /projects *.example.com(rw)
Soft restart
exportfs -av
On the client
Permanent: In file /etc/fstab
servername:/projects /mnt/nfs/projects nfs defaults 0 0
One time:
mount servername:/projects /mnt/nfs/projects
Kernel data
/proc
/proc/cpuinfo /proc/interrupts /proc/meminfo /proc/mounts /proc/partitions /proc/version
Viewing Files
You can use the following utilities to view files:
Command Usage
cat Used for viewing files that are not very long; it does not provide any scroll-back.
tac Used to look at a file backwards, starting with the last line.
less Used to view larger files because it is a paging program; it pauses at each screenful of text, provides scroll-back capabilities, and lets you search and navigate within the file. Note: Use / to search for a pattern in the forward direction and ? for a pattern in the backward direction.
tail Used to print the last 10 lines of a file by default. You can change the number of lines by doing -n 15 or just -15 if you wanted to look at the last 15 lines instead of the default.
head The opposite of tail; by default it prints the first 10 lines of a file.
ECHO
echo string > newfile The specified string is placed in a new file. echo string >> existingfile The specified string is appended to the end of an already existing file. echo $variable The contents of the specified environment variable are displayed.
CAT
cat file1 file2 Concatenate multiple files and display the output; i.e., the entire content of the first file is followed by that of the second file. cat file1 file2 > newfile Combine multiple files and save the output into a new file. cat file >> existingfile Append a file to the end of an existing file. cat > file Any subsequent lines typed will go into the file until CTRL-D is typed. cat >> file Any subsequent lines are appended to the file until CTRL-D is typed.
Symlinks
$ ln -s file1 file4 $ ls -li file1 file4
Folder History stack
To push current folder to the stack
pushd .
To list the stach
dirs
To change to the last pushed folder
popd
Standard file streams and descriptors
stdin ⇒ descriptor 0 (usu keyboard) stdout ⇒ descriptor 1 (usu screen) stderr ⇒ descriptor 2 (screen or file)
$ do_something > output-file
stdout goes to 'output-file' file, stderr goes to screen (by default)
$ do_something 2> error-file
stdout goes to screen (by default) stderr (file descriptor 2) goes to 'error-file' file
$ do_something >& all-output-file $ do_something > all-output-file 2>&1
stdout and stderr goes to 'all-output-file' file
Directories and its contents
Essential programs
/bin
Non-essential binaries
/usr/bin
System administration binaries
/sbin
Device nodes
/dev
Variable files
/var System log files: /var/log Packages and database files: /var/lib Print queues: /var/spool Temp files: /var/tmp FTP service: /var/ftp HTTP web service: /var/www
System configuration files, global settings for all users
/etc resolv.conf: where to go on the network to obtain host name to IP address mappings (DNS) passwd, shadow, group: for managing user accounts rc2.d: (System run level scripts) contains links to scripts for entering and leaving run level 2
Boot
/boot vmlinuz: the compressed Linux kernel, required for booting initramfs: the initial ram filesystem, required for booting, sometimes called initrd, not initramfs config: the kernel configuration file, only used for debugging and bookkeeping System.map: kernel symbol table, only used for debugging grub: grand unified bootloader
Libraries
/lib /lib64
Removable devices (CS, USB, DVD; …)
/media
Other
/opt Optional application software packages. /sys Virtual pseudo-filesystem giving information about the system and the hardware. Can be used to alter system parameters and for debugging purposes. /srv Site-specific data served up by the system. Seldom used. /tmp Temporary files; on some distributions erased across a reboot and/or may actually be a ramdisk in memory. /usr Multi-user applications, utilities and data.
Comparing files
diff <file1> <file2> -c listing of differences that include 3 lines of context before and after the lines differing in content -r recursively compare subdirectories as well as the current directory -i Ignore the case of letters -w Ignore differences in spaces and tabs (white space)
diff3 MY-FILE COMMON-FILE YOUR-FILE
Patches
Create the patch (the diferences only)
diff -Nur originalfile newfile > patchfile
Apply a patch to directory (see man patch)
patch -p1 < patchfile
Apply a patch to a file
patch originalfile patchfile
File to detect file type
Windows → by file extension
Linux → by file contents =⇒ file utility
Backup
rsync -r project1 archive-machine:projects/project1
Options -r recursive -dry-run do-no execute
FILE COMPRESSION
gzip bzip2 xz zip
tar zcvf file.tar.gz * -> join and copmress files in current folder and subfolders and copmress it with gzip tar xf file.tar.gz -> decompress and restores file.tar.gz in current folder
compress into a gzip file
tar -zcvfp filename.tar.gz files
z uses gzipc creates new filev verbosef specifies where to put the new compression (filename.tar.gz)p preserves permissions
uncompress in current folder
tar -xvpzf filename.tar
uncompress in folder path_to_folder
tar -xvpzf filename.tar -C path_to_folder
list contents
tar -ztvf filename.tar.gz
Copy disk
dd
Copia archivos remotos a local
Copiar backups automáticos de Nairobi
scp -r alfredo@nairobi.pres.in:/home/alfredo/backups/database/postgresql/* /home/alfredo/backups/remote/
Copiar fotos de uploads:
scp -r alfredo@nairobi.pres.in:/home/alfredo/rails_apps/shk/shared/public/uploads /home/alfredo/projects/shk/public
Command history
CTRL+R reverse search !! last command !n n-th last command !$ last parameter in the last command !string last command starting with string
Keyboard Shortcuts
TASK CTRL-L Clears the screen CTRL-D Exits the current shell CTRL-Z Puts the current process into suspended background CTRL-C Kills the current process CTRL-H Works the same as backspace CTRL-A Goes to the beginning of the line CTRL-W Deletes the word before the cursor CTRL-U Deletes from beginning of line to cursor position CTRL-E Goes to the end of the line Tab Auto-completes files, directories, and binaries
Alias
$ alias --> list of alias
~/.bashrc
alias <name>='<command>'
File ownership
chown
Group ownership
chgrp
File permissions
The permission in the command line is displayed as: _rwxrwxrwx 1 owner:group
- rwx rwx rwx 1 owner:group d = directorio owner group others hardlinks to the file
chmod <permissions> <file>
Permissions:
u/g/o +- r/w/x numérico binario
Ejemplo:
chmod 755 file1 --> -rwxr-xr-x
Si file1 tiene -rw-rw-r–
chmod uo+x,g-w file1 --> -rwxr--r-x
Networking
Usage:
ipcalc [options] <ADDRESS>[[/]<NETMASK>] [NETMASK]
ipcalc takes an IP address and netmask and calculates the resulting broadcast, network, Cisco wildcard mask, and host range. By giving a second netmask, you can design sub- and supernetworks. It is also intended to be a teaching tool and presents the results as easy-to-understand binary values.
Host name
hostname
IP address and other informaiton
host <name> nslookup <name>
Network adapters
ifconfig
Network configuration files
Debian
cat /etc/network/interfaces /etc/init.d/networking start
Fedora
cat /etc/sysconfig/network cat /etc/sysconfig/network-scripts/ifcfg-eth0
Network tools and commands
ip addr show ip route show ping <hostname> route -n route add/del -net address traceroute <hostname>
ethtool Queries network interfaces and can also set various parameters such as the speed. netstat Displays all active connections and routing tables. Useful for monitoring performance and troubleshooting. nmap Scans open ports on a network. Important for security analysis tcpdump Dumps network traffic for analysis. iptraf Monitors network traffic in text mode.
Source: http://bencane.com/2013/02/25/10-nmap-commands-every-sysadmin-should-know/
nmap -sP 192.168.1.0/24 ==> discovers IP addresses nmap 192.168.1.0/24 ==> scan for open ports in the whole net nmap -O 192.168.1.50 ==> Identify the Operating System of a host (requires root) nmap -sL 192.168.1.0/24 ==> Identify Hostnames nmap -sS -sU -PN 192.168.1.50 ==> TCP Syn and UDP Scan (requires root) nmap -T4 -F 192.168.1.50 ==> Fast scan for open ports (100 most common) nmap -T4 -A 192.168.1.0/24 ==> Agressive scan for open ports nmap -v ... ==> verbose command
password-less SSH session
ssh-keygen ssh-copy-id remote-host