Difference between revisions of "Linux"

Jump to navigation Jump to search
4,257 bytes added ,  08:30, 26 September 2016
→‎System Info: Added NIC list
m (→‎Commands: Added a command)
(→‎System Info: Added NIC list)
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Commands ==
== Commands ==
=== System ===
{|class="vwikitable"
|-
! Syntax                                  !! Action / Description
|-
|<code> shutdown -r now </code>          || Restart / reboot system, use <code>-h</code> to shutdown/halt.  If command not available use <code>init</code>
|-
|<code> init <level> </code>              || Level 0 - Halt system, level 6 - Reboot system
|-
|<code> uname -a </code>                  || Running OS info
|-
|<code> arp -n </code>                    || IP -> MAC arp table
|-
|<code> ifconfig </code>                  || Interface config (Unix equiv of Windows' ipconfig)
|-
|<code> ps fx <nowiki>|</nowiki> grep <proc> </code>      || Find running process info
|-
|<code> ps -aux </code>                  || Running process info
|-
|<code> kill <pid> </code>                || Kill a process
|-
|<code> top </code>                      || Task Manager (ish)
|-
|<code> free -m </code>                  || Free memory
|-
|<code> nice <cmd> </code>                || Runs a command with reduced priority
|}


For use in the service console
=== File System and File Operations ===
 
{|class="vwikitable"
{|cellpadding="2" cellspacing="0" border="1"
|-
! Syntax                                  !! Action / Description
|-
|<code> ls -l </code>                    || List directory contents (<code>-l</code> long format) - see [[#Terminal Colours|Terminal Colours]]
|-
|<code> df -h </code>                    || Disk space summary
|-
|<code> df -i </code>                    || Disk inodes summary
|-
|<code> du -sh </code>                    || Directory space usage summary
|-
|<code> du -h --max-depth=1 </code>      || Directory space usage summary of subfolders
|-
|-
|<code> ls -l </code>                    || List directory contents (<code>-l</code> long format) - [http://communities.vmware.com/message/678302#678302 Colour codes]
|<code> <nowiki> du -ma / | sort -nr | head -n 20 </nowiki></code> || Find 20 largest files/directories
|-
|-
|<code> pwd </code>                      || Present working directory
|<code> pwd </code>                      || Present working directory
|-
|-
|<code> cp <src> <dest> </code>          || Copy a file
|<code> cp <src> <dest> </code>          || Copy a file (see also <code>[[#SCP|scp]]</code>)
|-
|-
|<code> ln -s <src> <dest> </code>        || Create a symbolic link to a file
|<code> ln -s <src> <dest> </code>        || Create a symbolic link to a file
Line 16: Line 54:
|-
|-
|<code> rm –d -r -f <folder> </code>      || Remove (delete) folder and contents '''without prompting'''
|<code> rm –d -r -f <folder> </code>      || Remove (delete) folder and contents '''without prompting'''
|-
|<code> find /path/ -iname "*FILENAME*" </code> || Find file with ''*filename*'' in ''path''
|-
|-
|<code> find <folder> -mtime +14 -exec rm {} \; </code>      || Remove (delete) files in folder older than 14 days
|<code> find <folder> -mtime +14 -exec rm {} \; </code>      || Remove (delete) files in folder older than 14 days
|-
|<code> find -iname 'file*' -mtime +7 -exec rm {} \; </code> || Remove all files matching ''file*'' in current folder older than 7 days
|-
|<code> find -iname 'file*' -exec rm {} \; -exec sleep 1 \; </code> || Remove all files matching ''file*'' in current folder, pausing for 1 sec between deletes
|-
|<code> chmod +x <file> </code>          || Add execute permission to a file
|-
|<code> lsof <folder> </code>            || List open files
|-
|<code> lsof +L1 </code>                  || List deleted files still held open (reported by <code>df</code> but not <code>du</code>
|-
|<code> tar czf file.tgz folder </code>  || [[Acronyms#T|Tar]] contents of folder (or file) to an archive
|-
|<code> mount --bind /var/tmp tmp </code>  || Create a <code>/tmp</code> mount to local <code>/var/tmp</code> directory
|}
=== File Contents (Inspection and Manipulation) ===
{|class="vwikitable"
|-
! Syntax                                  !! Action / Description
|-
|-
|<code> more <file> </code>              || Views a file (read only), <code>q</code> to exit, <code>+line</code> to start from line
|<code> more <file> </code>              || Views a file (read only), <code>q</code> to exit, <code>+line</code> to start from line
|-
|-
|<code> find /path/ -iname "*FILENAME*" </code> || Find file with ''*filename*'' in ''path''
|<code> less <file> </code>               || Views a file (read only), like <code> more </code>, but with increased functionality
|-
|-
|<code> watch -d "ls -lt *.vmdk ;date" </code> || Watch modifications occurring to files matching ''*.vmdk''
|<code> watch -d "ls -lt *.vmdk ;date" </code> || Watch modifications occurring to files matching ''*.vmdk''
Line 27: Line 87:
|<code> diff <file1> <file2> </code>      || Difference between two files
|<code> diff <file1> <file2> </code>      || Difference between two files
|-
|-
|<code> grep <find> <file> </code>        || Find all occurrences of ''find'' in ''file''
|<code> grep <find> <file> </code>        || Show all lines with occurrences of ''find'' in ''file''
|-
|<code> grep -A <n> -B <m> <find> <file> </code> || As above, but additionally show the ''n'' lines after and ''m'' lines before
|-
|-
|<code> tail <file> </code>              || Displays the last 10 lines of a file, <code>-f</code> follows any updates (eg to monitor a log file)
|<code> tail <file> </code>              || Displays the last 10 lines of a file, <code>-f</code> follows any updates (eg to monitor a log file)
|}
=== NTP / Date and Time ===
{|class="vwikitable"
|-
! Syntax                                  !! Action / Description
|-
|-
|<code> init <level> </code>             || Level 0 - Halt system, level 6 - Reboot system
|<code> date </code>                      || Show the local system date and time
|-
|<code> date -d <string> </code>         || Show/calculate the date and time represented by <code><string></code> (eg 'tomorrow', 'next week', 'thursday')
|-
|<code> ntpdate -p 1 -q -d ntp.domain.com </code> || Test/query an NTP server (replace <code>ntp.domain.com</code>)
|-
|<code> ntpdate -d ntp.domain.com </code> || Set system clock from an NTP server (replace <code>ntp.domain.com</code>)
|-
|<code> ntpq -p </code>                  || Show list of ntp daemon peers
|}
 
== System Info ==
{|class="vwikitable"
|-
! File / Command                            !! Description
|-
|<code> /etc/issue </code>                  || OS Name
|-
|<code> /proc/cpuinfo </code>              || CPU(s) information - model, spec, features, etc
|-
|<code> lspci | egrep -i 'network|ethernet'</code> || List NICs
|-
|}
 
== Terminal Colours ==
The colourisation of files and folders as seen through a terminal session (eg PuTTY) normally have the following meanings...
 
{|class="vwikitable"
|-
! Type                                                              !! Description
|-
|-
|<code> df -h </code>                    || Disk space summary
|style='background:black;color:white;font-family:courier;'| FILE  || Normal file
|-
|-
|<code> du -sh </code>                    || Directory space usage summary
|style='background:#FF5555;color:white;font-family:courier;'| MISSING || Missing file
|-
|-
|<code> chmod +x <file> </code>          || Add execute permission to a file
|style='background:black;color:#5555FF;font-family:courier;'| DIR    || Directory
|-
|-
|<code> uname -a </code>                  || Running OS info
|style='background:black;color:#55FF55;font-family:courier;'| EXEC    || Executable file
|-
|-
|<code> arp -n </code>                    || IP -> MAC arp table
|style='background:black;color:#55FFFF;font-family:courier;'| LINK    || Symbolic link
|-
|-
|<code> ifconfig </code>                  || Interface config (Unix equiv of Windows' ipconfig)
|style='background:#FF5555;color:white;font-family:courier;'| ORPHAN  || Orphaned symbolic link
|-
|-
|<code> ps fx <nowiki>|</nowiki> grep <proc> </code>      || Find running process info
|style='background:black;color:#FFFF55;font-family:courier;'| FIFO    || Named pipe
|-
|-
|<code> ps -aux </code>                  || Running process info
|style='background:black;color:#FF55FF;font-family:courier;'| SOCK    || Socket
|-
|-
|<code> kill <pid> </code>                || Kill a process
|style='background:black;color:#FFFF55;font-family:courier;'| BLK    || Block device driver
|-
|-
|<code> lsof <folder> </code>            || List open files
|style='background:black;color:#FFFF55;font-family:courier;'| CHR    || Character device driver
|-
|-
|<code> top </code>                      || Task Manager (ish)
|style='background:black;color:#FC391F;font-family:courier;'| ARC    || Archive / Compressed
|-
|-
|<code> free -m </code>                  || Free memory
|style='background:black;color:#FF55FF;font-family:courier;'| MEDIA  || Media files (image, video etc)
|}
|}
These colours are derived from the [http://www.ecma-international.org/publications/standards/Ecma-048.htm ISO 6429] standard for coded character sets.  The above info is derived from the following sources...
* http://communities.vmware.com/message/678302#678302
* http://www.wikipedia.org/wiki/ANSI_escape_code
== SCP ==
In order to be able to copy files from one machine to the other...
* <code> scp user@server:/remote/server/path/file /local/server/path/ </code>
In order to be able to copy files within a script (and not get prompted for a user password, the public key of the machine running the script needs to be authorised on the remote server.
# On the local machine, be logged in as the same user as will be running the script
# Create public key on local machine (just hit return to accept default options)
#* <code> ssh-keygen -t rsa </code>
# Copy the public key for local machine user to the remote machine
#* EG <code> scp /root/.ssh/id_rsa.pub server:/home/user/ </code>
# On the remote machine, check if the following file already exists
#* <code> /root/.ssh/authorized_keys </code>
# If so, append the contents of the copied key to the file
# If not, copy the file
Alternatively...
# On the local machine, be logged in as the same user as will be running the script
# Create public key on local machine (just hit return to accept default options)
#* <code> ssh-keygen -t rsa </code>
# Copy key to remote machine
#* <code>cat /root/.ssh/id_rsa.pub | ssh user@server "cat >> ~/.ssh/authorized_keys"</code>


== VI Editor ==
== VI Editor ==
 
VI is a bit of a pain to use, go slowly, and use [Esc] regularly to get out of the current mode after each operation.
VI is a bit of a pain to use, go slowly, and use [Esc] regularly to get out of the current mode after each operation  


Crib sheet for VI mode commands...
Crib sheet for VI mode commands...
 
{|class="vwikitable"
{|cellpadding="2" cellspacing="0" border="1"
! Command                              !! Purpose
|-
|-
|<code> i </code>                      || Insert characters
|<code> i </code>                      || Insert characters
Line 92: Line 214:
|<code>:undo </code>                    || Undo last action
|<code>:undo </code>                    || Undo last action
|}
|}
== VIM Editor ==
VIm = Vi Improved, the commands are the same, but theres extra features
* [http://www.vim.org/docs.php VIM website]
* [http://vimdoc.sourceforge.net/htmldoc/usr_toc.html VIM on line manual]
=== Installation ===
#  Login to the ESX console using your user account and create a new directory (within <code> /home/user </code>
#* <code> mkdir vim </code>
# Logon to the VCS machine (UKLONVCP1) and copy across the installation files, stored under the Software_Reposity directory (substitute user and ESXserver appropriately).
#* <code> pscp C:\Software_Repository\vim-7.1.tar.bz2 user@ESXserver:/home/user/vim  </code>
# Back on the ESX console become root, change into the directory where vim is
# Extract the package
#* <code>  bunzip2 -c vim-7.1.tar.bz2 | tar -xf - </code>
# Move into the vim71 folder
# Run configure
#* <code> ./configure </code>
# Compile the package
#* <code> make </code>
# Install the package
#* <code> make install </code>
=== Useful Commands ===
{|cellpadding="2" cellspacing="0" border="1"
|-
|<code> :syntax enable </code>                    || Turns on syntax formatting
|-
|<code> vim one.sh two.sh three.sh </code>        || Opens up three files for editing
|-
|<code> :wnext </code>                            || Writes changes to existing file and moves to next file(omit w not to write)
|-
|<code> :wprevious </code>                        || Writes changes to existing file and moves to previous file (omit w not to write)
|-
|<code> v </code>                                || Toggle visual mode (so you can select text), use y/p to yank/paste selected text
|-
|[http://vimdoc.sourceforge.net/htmldoc/usr_07.html#07.5 VIM manual section 7.5] || Copy text between files
|}


== Unix User accounts ==
== Unix User accounts ==
* '''Create Account''' - Use <code> useradd <user> </code> to add a new user account with the default policy settings
* '''Create Account''' - Use <code> useradd <user> </code> to add a new user account with the default policy settings
* '''Modify Password''' - Use <code> passwd <user> </code> to change password for ''user''.  To breach minimum complexity rules you must be logged in at the console (in the DC not remotely)
* '''Modify Password''' - Use <code> passwd <user> </code> to change password for ''user''.  To breach minimum complexity rules you must be logged in at the console (in the DC not remotely)
* '''Modify Expiry''' - To disable the password expiry policy for a account that already exists use <code> chage -M 99999 <user> </code>, to disable for all future account (ie modify the default policy) use <code> esxcfg-auth --passmaxdays=99999 </code>
* '''Modify Expiry''' - To disable the password expiry policy for a account that already exists use <code> chage -M 99999 <user> </code>, to disable for all future account (ie modify the default policy) use <code> esxcfg-auth --passmaxdays=99999 </code>
* '''List users''' - <code>cat /etc/passwd</code>
* '''List groups''' - <code>cat /etc/group</code>
* '''Show group membership''' - <code>id <user> </code>
* '''Add user to group''' - <code>usermod -a -G <group> <user></code> - user must already exist
[[Category:Linux]]

Navigation menu