Linux: Difference between revisions
Jump to navigation
Jump to search
(Added "Terminal Colours") |
(→System Info: Added NIC list) |
||
(15 intermediate revisions by the same user not shown) | |||
Line 24: | Line 24: | ||
|- | |- | ||
|<code> free -m </code> || Free memory | |<code> free -m </code> || Free memory | ||
|- | |||
|<code> nice <cmd> </code> || Runs a command with reduced priority | |||
|} | |} | ||
Line 31: | Line 33: | ||
! Syntax !! Action / Description | ! Syntax !! Action / Description | ||
|- | |- | ||
|<code> ls -l </code> || List directory contents (<code>-l</code> long format) - see [[#Terminal Colours|Terminal Colours] | |<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 -h </code> || Disk space summary | ||
Line 38: | Line 40: | ||
|- | |- | ||
|<code> du -sh </code> || Directory space usage summary | |<code> du -sh </code> || Directory space usage summary | ||
|- | |||
|<code> du -h --max-depth=1 </code> || Directory space usage summary of subfolders | |||
|- | |- | ||
|<code> <nowiki> du -ma / | sort -nr | head -n 20 </nowiki></code> || Find 20 largest files/directories | |<code> <nowiki> du -ma / | sort -nr | head -n 20 </nowiki></code> || Find 20 largest files/directories | ||
Line 54: | Line 58: | ||
|- | |- | ||
|<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> chmod +x <file> </code> || Add execute permission to a file | ||
Line 59: | Line 67: | ||
|<code> lsof <folder> </code> || List open files | |<code> lsof <folder> </code> || List open files | ||
|- | |- | ||
|<code> tar czf file.tgz folder </code> || Tar contents of folder to | |<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 | |||
|} | |} | ||
Line 75: | 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> || | |<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> 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 | |||
|- | |||
|} | |} | ||
Line 130: | Line 173: | ||
# If so, append the contents of the copied key to the file | # If so, append the contents of the copied key to the file | ||
# If not, copy 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 == | ||
Line 170: | Line 220: | ||
* '''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> | ||
[[Category: | * '''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]] |
Latest revision as of 08:30, 26 September 2016
Commands
System
Syntax | Action / Description |
---|---|
shutdown -r now |
Restart / reboot system, use -h to shutdown/halt. If command not available use init
|
init <level> |
Level 0 - Halt system, level 6 - Reboot system |
uname -a |
Running OS info |
arp -n |
IP -> MAC arp table |
ifconfig |
Interface config (Unix equiv of Windows' ipconfig) |
ps fx | grep <proc> |
Find running process info |
ps -aux |
Running process info |
kill <pid> |
Kill a process |
top |
Task Manager (ish) |
free -m |
Free memory |
nice <cmd> |
Runs a command with reduced priority |
File System and File Operations
Syntax | Action / Description |
---|---|
ls -l |
List directory contents (-l long format) - see Terminal Colours
|
df -h |
Disk space summary |
df -i |
Disk inodes summary |
du -sh |
Directory space usage summary |
du -h --max-depth=1 |
Directory space usage summary of subfolders |
du -ma / | sort -nr | head -n 20 |
Find 20 largest files/directories |
pwd |
Present working directory |
cp <src> <dest> |
Copy a file (see also scp )
|
ln -s <src> <dest> |
Create a symbolic link to a file |
rm –i <file> |
Remove (delete) file (-i prompts before remove)
|
rm –d -r -f <folder> |
Remove (delete) folder and contents without prompting |
find /path/ -iname "*FILENAME*" |
Find file with *filename* in path |
find <folder> -mtime +14 -exec rm {} \; |
Remove (delete) files in folder older than 14 days |
find -iname 'file*' -mtime +7 -exec rm {} \; |
Remove all files matching file* in current folder older than 7 days |
find -iname 'file*' -exec rm {} \; -exec sleep 1 \; |
Remove all files matching file* in current folder, pausing for 1 sec between deletes |
chmod +x <file> |
Add execute permission to a file |
lsof <folder> |
List open files |
lsof +L1 |
List deleted files still held open (reported by df but not du
|
tar czf file.tgz folder |
Tar contents of folder (or file) to an archive |
mount --bind /var/tmp tmp |
Create a /tmp mount to local /var/tmp directory
|
File Contents (Inspection and Manipulation)
Syntax | Action / Description |
---|---|
more <file> |
Views a file (read only), q to exit, +line to start from line
|
less <file> |
Views a file (read only), like more , but with increased functionality
|
watch -d "ls -lt *.vmdk ;date" |
Watch modifications occurring to files matching *.vmdk |
diff <file1> <file2> |
Difference between two files |
grep <find> <file> |
Show all lines with occurrences of find in file |
grep -A <n> -B <m> <find> <file> |
As above, but additionally show the n lines after and m lines before |
tail <file> |
Displays the last 10 lines of a file, -f follows any updates (eg to monitor a log file)
|
NTP / Date and Time
Syntax | Action / Description |
---|---|
date |
Show the local system date and time |
date -d <string> |
Show/calculate the date and time represented by <string> (eg 'tomorrow', 'next week', 'thursday')
|
ntpdate -p 1 -q -d ntp.domain.com |
Test/query an NTP server (replace ntp.domain.com )
|
ntpdate -d ntp.domain.com |
Set system clock from an NTP server (replace ntp.domain.com )
|
ntpq -p |
Show list of ntp daemon peers |
System Info
File / Command | Description |
---|---|
/etc/issue |
OS Name |
/proc/cpuinfo |
CPU(s) information - model, spec, features, etc |
egrep -i 'network|ethernet' | List NICs |
Terminal Colours
The colourisation of files and folders as seen through a terminal session (eg PuTTY) normally have the following meanings...
Type | Description |
---|---|
FILE | Normal file |
MISSING | Missing file |
DIR | Directory |
EXEC | Executable file |
LINK | Symbolic link |
ORPHAN | Orphaned symbolic link |
FIFO | Named pipe |
SOCK | Socket |
BLK | Block device driver |
CHR | Character device driver |
ARC | Archive / Compressed |
MEDIA | Media files (image, video etc) |
These colours are derived from the ISO 6429 standard for coded character sets. The above info is derived from the following sources...
SCP
In order to be able to copy files from one machine to the other...
scp user@server:/remote/server/path/file /local/server/path/
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)
ssh-keygen -t rsa
- Copy the public key for local machine user to the remote machine
- EG
scp /root/.ssh/id_rsa.pub server:/home/user/
- EG
- On the remote machine, check if the following file already exists
/root/.ssh/authorized_keys
- 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)
ssh-keygen -t rsa
- Copy key to remote machine
cat /root/.ssh/id_rsa.pub | ssh user@server "cat >> ~/.ssh/authorized_keys"
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.
Crib sheet for VI mode commands...
Command | Purpose |
---|---|
i |
Insert characters |
x |
Delete character |
r |
Replace character |
cw |
Overwrite word (use any delimiter) |
o |
Insert line |
dd |
Delete current line |
<x>yy |
Yank (copy) next x lines (inclusive of current line, default x is 1)
|
p |
Paste yanked lines |
/<string> |
Search for next occurrence of string |
:g/<find>/s//<replace>/g |
Find and replace |
:wq |
Write (save) and Quit |
:q! |
Quit (don't save) |
:undo |
Undo last action |
Unix User accounts
- Create Account - Use
useradd <user>
to add a new user account with the default policy settings - Modify Password - Use
passwd <user>
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
chage -M 99999 <user>
, to disable for all future account (ie modify the default policy) useesxcfg-auth --passmaxdays=99999
- List users -
cat /etc/passwd
- List groups -
cat /etc/group
- Show group membership -
id <user>
- Add user to group -
usermod -a -G <group> <user>
- user must already exist