You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:(){ :|:& };: <spanclass="cmt"># bash fork bomb. Will kill your machine</span>
tail +2 file > file2 <spanclass="cmt"># remove the first line from file</span>
```
</pre>
I use this little trick to change the file extension for many files at once. For example from .cxx to .cpp. Test it first without the <code>| sh</code> at the end. You can also do this with the command <code>rename</code> if installed. Or with bash builtins.
<pre># ls *.cxx | awk -F. '{print "mv "$0" "$1".cpp"}' | sh
tokhi
revised
this gist Aug 17, 2015.
1 changed file
with
5 additions
and
2 deletions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:(){ :|:& };:<span class="cmt"># bash fork bomb. Will kill your machine</span>
tail +2 file > file2 <span class="cmt"># remove the first line from file</span>
</pre>
```
I use this little trick to change the file extension for many files at once. For example from .cxx to .cpp. Test it first without the <code>| sh</code> at the end. You can also do this with the command <code>rename</code> if installed. Or with bash builtins.
<pre># ls *.cxx | awk -F. '{print "mv "$0" "$1".cpp"}' | sh
# ls *.c | sed "s/.*/cp &&.$(date "+%Y%m%d")/" | sh <spanclass="cmt"># e.g. copy *.c to *.c.20080401</span>
tokhi
renamed
this gist Aug 17, 2015.
1 changed file
with
0 additions
and
0 deletions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This document is a collection of Unix/Linux/BSD commands and tasks which are useful for IT work or for advanced users. This is a practical guide with concise explanations, however the reader is supposed to know what s/he is doing.<br /><br />
</div>
<divclass="footerfirst">
##Unix Toolbox revision 14.4
##Unix Toolbox revision 14.4
<divclass="footerfirst">
The latest version of this document can be found at <ahref="http://cb.vu/unixtoolbox.xhtml">http://cb.vu/unixtoolbox.xhtml</a>. Replace .xhtml on the link with <ahref="http://cb.vu/unixtoolbox.pdf">.pdf</a> for the PDF version and with <ahref="http://cb.vu/unixtoolbox.book.pdf">.book.pdf</a> for the booklet version. On a duplex printer the booklet will create a small book ready to bind. <spanclass="web">This XHTML page can be converted into a nice PDF document with a CSS3 compliant application (see the <aclass="xref"href="#bourneexample">script example</a>).</span> See also the <ahref="http://cb.vu/unixtoolbox">about page</a>.<br />
Error reports and comments are most welcome - <ahref="mailto:c\at\cb.vu">[email protected]</a> Colin Barschel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -5,6 +5,7 @@ This document is a collection of Unix/Linux/BSD commands and tasks which are use
</div>
<divclass="footerfirst">
##Unix Toolbox revision 14.4
The latest version of this document can be found at <ahref="http://cb.vu/unixtoolbox.xhtml">http://cb.vu/unixtoolbox.xhtml</a>. Replace .xhtml on the link with <ahref="http://cb.vu/unixtoolbox.pdf">.pdf</a> for the PDF version and with <ahref="http://cb.vu/unixtoolbox.book.pdf">.book.pdf</a> for the booklet version. On a duplex printer the booklet will create a small book ready to bind. <spanclass="web">This XHTML page can be converted into a nice PDF document with a CSS3 compliant application (see the <aclass="xref"href="#bourneexample">script example</a>).</span> See also the <ahref="http://cb.vu/unixtoolbox">about page</a>.<br />
Error reports and comments are most welcome - <ahref="mailto:c\at\cb.vu">[email protected]</a> Colin Barschel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This document is a collection of Unix/Linux/BSD commands and tasks which are useful for IT work or for advanced users. This is a practical guide with concise explanations, however the reader is supposed to know what s/he is doing.<br /><br />
</div>
<divclass="footerfirst">
Unix Toolbox revision 14.4<br />
##Unix Toolbox revision 14.4
The latest version of this document can be found at <ahref="http://cb.vu/unixtoolbox.xhtml">http://cb.vu/unixtoolbox.xhtml</a>. Replace .xhtml on the link with <ahref="http://cb.vu/unixtoolbox.pdf">.pdf</a> for the PDF version and with <ahref="http://cb.vu/unixtoolbox.book.pdf">.book.pdf</a> for the booklet version. On a duplex printer the booklet will create a small book ready to bind. <spanclass="web">This XHTML page can be converted into a nice PDF document with a CSS3 compliant application (see the <aclass="xref"href="#bourneexample">script example</a>).</span> See also the <ahref="http://cb.vu/unixtoolbox">about page</a>.<br />
Error reports and comments are most welcome - <ahref="mailto:c\at\cb.vu">[email protected]</a> Colin Barschel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This document is a collection of Unix/Linux/BSD commands and tasks which are useful for IT work or for advanced users. This is a practical guide with concise explanations, however the reader is supposed to know what s/he is doing.<br /><br />
</div>
tokhi
revised
this gist Aug 17, 2015.
1 changed file
with
2647 additions
and
2 deletions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre># uname -a <spanclass="cmt"># Get the kernel version (and BSD version)</span>
# lsb_release -a <spanclass="cmt"># Full release info of any LSB distribution</span>
# cat /etc/SuSE-release <spanclass="cmt"># Get SuSE version</span>
# cat /etc/debian_version <spanclass="cmt"># Get Debian version</span>
</pre>
Use /etc/<code>DISTR</code>-release with <code>DISTR=</code> lsb (Ubuntu), redhat, gentoo, mandrake, sun (Solaris), and so on. See also <code>/etc/issue</code>.
<pre># uptime <spanclass="cmt"># Show how long the system has been running + load</span>
Encrypted passwords are stored in /etc/shadow for Linux and Solaris and /etc/master.passwd on FreeBSD. If the master.passwd is modified manually (say to delete a password), run <code># pwd_mkdb -p master.passwd</code> to rebuild the database.<br /><br />
To temporarily prevent logins system wide (for all users but root) use nologin. The message in nologin will be displayed (might not work with ssh pre-shared keys).
<pre># echo "Sorry no login now" > /etc/nologin <spanclass="cmt"># (Linux)</span>
# echo "Sorry no login now" > /var/run/nologin <spanclass="cmt"># (FreeBSD)</span>
</pre>
<h2id="limits">Limits</h2>
Some application require higher limits on open files and sockets (like a proxy
web server, database). The default limits are usually too low.
<h3>Linux</h3>
<h4>Per shell/script</h4>
The shell limits are governed by <code>ulimit</code>. The status is checked
with <code>ulimit -a</code>. For example to change the open files limit from
1024 to 10240 do:
<pre># ulimit -n 10240 <spanclass="cmt"># This is only valid within the shell</span>
</pre>
The <code>ulimit</code> command can be used in a script to change the limits for the script only.
<h4>Per user/process</h4>
Login users and applications can be configured in <code>/etc/security/limits.conf</code>. For example:
<pre># cat /etc/security/limits.conf
* hard nproc 250 <spanclass="cmt"># Limit user processes</span>
asterisk hard nofile 409600 <spanclass="cmt"># Limit application open files</span>
</pre>
<h4>System wide</h4>
Kernel limits are set with sysctl. Permanent limits are set in <code>/etc/sysctl.conf</code>.
<pre># sysctl -a <spanclass="cmt"># View all system limits</span>
# sysctl fs.file-max <spanclass="cmt"># View max open files limit</span>
# sysctl fs.file-max=102400 <spanclass="cmt"># Change max open files limit</span>
# echo "1024 50000" > /proc/sys/net/ipv4/ip_local_port_range <spanclass="cmt"># port range</span>
# cat /etc/sysctl.conf
fs.file-max=102400 <spanclass="cmt"># Permanent entry in sysctl.conf</span>
# cat /proc/sys/fs/file-nr <spanclass="cmt"># How many file descriptors are in use</span>
</pre>
<h3>FreeBSD</h3>
<h4>Per shell/script</h4>
Use the command <code>limits</code> in csh or tcsh or as in Linux, use <code>ulimit</code> in an sh or bash shell.
<h4>Per user/process</h4>
The default limits on login are set in <code>/etc/login.conf</code>. An unlimited value is still limited by the system maximal value.
<h4>System wide</h4>
Kernel limits are also set with sysctl. Permanent limits are set in <code>/etc/sysctl.conf</code> or <code>/boot/loader.conf</code>. The syntax is the same as Linux but the keys are different.
<pre># sysctl -a <spanclass="cmt"># View all system limits</span>
# sysctl kern.maxfiles=XXXX <spanclass="cmt"># maximum number of file descriptors</span>
kern.ipc.nmbclusters=32768 <spanclass="cmt"># Permanent entry in /etc/sysctl.conf</span>
kern.maxfiles=65536 <spanclass="cmt"># Typical values for Squid</span>
kern.maxfilesperproc=32768
kern.ipc.somaxconn=8192 <spanclass="cmt"># TCP queue. Better for apache/sendmail</span>
# sysctl kern.openfiles <spanclass="cmt"># How many file descriptors are in use</span>
# sysctl kern.ipc.numopensockets <spanclass="cmt"># How many open sockets are in use</span>
# sysctl net.inet.ip.portrange.last=50000 <spanclass="cmt"># Default is 1024-5000</span>
See The <ahref="http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/configtuning-kernel-limits.html">FreeBSD handbook Chapter 11</a><spanclass="fn">http://www.freebsd.org/handbook/configtuning-kernel-limits.html</span> for details. And also <ahref="http://serverfault.com/questions/64356/freebsd-performance-tuning-sysctls-loader-conf-kernel">FreeBSD performance tuning</a><spanclass="fn">http://serverfault.com/questions/64356/freebsd-performance-tuning-sysctls-loader-conf-kernel</span>
<h3>Solaris</h3>
The following values in <code>/etc/system</code> will increase the maximum file descriptors per proc:
<pre>set rlim_fd_max = 4096 <spanclass="cmt"># Hard limit on file descriptors for a single proc</span>
set rlim_fd_cur = 1024 <spanclass="cmt"># Soft limit on file descriptors for a single proc</span>
</pre>
<h2id="runlevels">Runlevels</h2>
<h3>Linux</h3>
Once booted, the kernel starts <code>init</code> which then starts <code>rc</code> which starts all scripts belonging to a runlevel. The scripts are stored in /etc/init.d and are linked into /etc/rc.d/rcN.d with N the runlevel number.<br />
The default runlevel is configured in /etc/inittab. It is usually 3 or 5:
<pre># grep default: /etc/inittab
id:3:initdefault:
</pre>
The actual runlevel can be changed with <code>init</code>. For example to go from 3 to 5:
Use <code>chkconfig</code> to configure the programs that will be started at boot in a runlevel.
<pre># chkconfig --list <spanclass="cmt"># List all init scripts</span>
# chkconfig --list sshd <spanclass="cmt"># Report the status of sshd</span>
# chkconfig sshd --level 35 on <spanclass="cmt"># Configure sshd for levels 3 and 5</span>
# chkconfig sshd off <spanclass="cmt"># Disable sshd for all runlevels</span>
</pre>
Debian and Debian based distributions like Ubuntu or Knoppix use the command <code>update-rc.d</code> to manage the runlevels scripts. Default is to start in 2,3,4 and 5 and shutdown in 0,1 and 6.
<pre># update-rc.d sshd defaults <spanclass="cmt"># Activate sshd with the default runlevels</span>
# update-rc.d -f sshd remove <spanclass="cmt"># Disable sshd for all runlevels</span>
# shutdown -h now (or # poweroff) <spanclass="cmt"># Shutdown and halt the system</span>
</pre>
<h3>FreeBSD</h3>
The BSD boot approach is different from the SysV, there are no runlevels. The final boot state (single user, with or without X) is configured in <code>/etc/ttys</code>. All OS scripts are located in <code>/etc/rc.d/</code> and in <code>/usr/local/etc/rc.d/</code> for third-party applications. The activation of the service is configured in <code>/etc/rc.conf</code> and <code>/etc/rc.conf.local</code>. The default behavior is configured in <code>/etc/defaults/rc.conf</code>. The scripts responds at least to start|stop|status.
<pre># /etc/rc.d/sshd status
sshd is running as pid 552.
# shutdown now <spanclass="cmt"># Go into single-user mode</span>
# exit <spanclass="cmt"># Go back to multi-user mode</span>
# shutdown -p now <spanclass="cmt"># Shutdown and halt the system</span>
# shutdown -r now <spanclass="cmt"># Reboot</span>
</pre>
The process <code>init</code> can also be used to reach one of the following states level. For example <code># init 6</code> for reboot.
<li>0 Halt and turn the power off (signal <code>USR2</code>)</li>
<li>1 Go to single-user mode (signal <code>TERM</code>)</li>
<li>6 Reboot the machine (signal <code>INT</code>)</li>
<li>c Block further logins (signal <code>TSTP</code>)</li>
<li>q Rescan the ttys(5) file (signal <code>HUP</code>)</li>
</ul>
<h3>Windows</h3>
Start and stop a service with either the <code>service name</code> or <code>"service description"</code> (shown in the Services Control Panel) as follows:
<pre>net stop WSearch
net start WSearch <spanclass="cmt"># start search service</span>
net stop "Windows Search"
net start "Windows Search" <spanclass="cmt"># same as above using descr.</span>
</pre>
<h2id="resetpasswd">Reset root password</h2>
<h3>Linux method 1</h3>
At the boot loader (lilo or grub), enter the following boot option:
<pre>init=/bin/sh</pre>
The kernel will mount the root partition and <code>init</code> will start the bourne shell
instead of <code>rc</code> and then a runlevel. Use the command <code>passwd</code> at the prompt to change the password and then reboot. Forget the single user mode as you need the password for that.<br />
If, after booting, the root partition is mounted read only, remount it rw:
<pre># mount -o remount,rw /
# passwd <spanclass="cmt"># or delete the root password (/etc/shadow)</span>
# sync; mount -o remount,ro / <spanclass="cmt"># sync before to remount read only</span>
# reboot
</pre>
<h3>FreeBSD method 1</h3>
On FreeBSD, boot in single user mode, remount / rw and use passwd. You can select the single user mode on the boot menu (option 4) which is displayed for 10 seconds at startup. The single user mode will give you a root shell on the / partition.
<pre># mount -u /; mount -a <spanclass="cmt"># will mount / rw</span>
# passwd
# reboot
</pre>
<h3>Unixes and FreeBSD and Linux method 2</h3>
Other Unixes might not let you go away with the simple init trick. The solution is to mount the root partition from an other OS (like a rescue CD) and change the password on the disk.
<ul>
<li>Boot a live CD or installation CD into a rescue mode which will give you a shell.</li>
<li>Find the root partition with fdisk e.g. fdisk /dev/sda</li>
<li>Mount it and use chroot:</li>
</ul>
<pre># mount -o rw /dev/ad4s3a /mnt
# chroot /mnt <spanclass="cmt"># chroot into /mnt</span>
# passwd
# reboot
</pre>
<h2id="kernelmodules">Kernel modules</h2>
<h3>Linux</h3>
<pre># lsmod <spanclass="cmt"># List all modules loaded in the kernel</span>
# modprobe isdn <spanclass="cmt"># To load a module (here isdn)</span>
</pre>
<h3>FreeBSD</h3>
<pre># kldstat <spanclass="cmt"># List all modules loaded in the kernel</span>
# kldload crypto <spanclass="cmt"># To load a module (here crypto)</span>
</pre>
<h2id="compilekernel">Compile Kernel</h2>
<h3>Linux</h3>
<pre># cd /usr/src/linux
# make mrproper <spanclass="cmt"># Clean everything, including config files</span>
# make oldconfig <spanclass="cmt"># Reuse the old .config if existent</span>
# make menuconfig <spanclass="cmt"># or xconfig (Qt) or gconfig (GTK)</span>
# make <spanclass="cmt"># Create a compressed kernel image</span>
# make modules <spanclass="cmt"># Compile the modules</span>
# make modules_install <spanclass="cmt"># Install the modules</span>
# make install <spanclass="cmt"># Install the kernel</span>
# reboot
</pre>
<h3>FreeBSD</h3>
Optionally update the source tree (in <code>/usr/src</code>) with csup (as of FreeBSD 6.2 or later):
To modify and rebuild the kernel, copy the generic configuration file to a new name and edit it as needed (you can also edit the file <code>GENERIC</code> directly). To restart the build after an interruption, add the option <code>NO_CLEAN=YES</code> to the make command to avoid cleaning the objects already build.
<pre># cd /usr/src/sys/i386/conf/
# cp GENERIC MYKERNEL
# cd /usr/src
# make buildkernel KERNCONF=MYKERNEL
# make installkernel KERNCONF=MYKERNEL
</pre>
To rebuild the full OS:
<pre># make buildworld <spanclass="cmt"># Build the full OS but not the kernel</span>
# make buildkernel <spanclass="cmt"># Use KERNCONF as above if appropriate</span>
# make installkernel
# reboot
# mergemaster -p <spanclass="cmt"># Compares only files known to be essential</span>
# make installworld
# mergemaster -i -U <spanclass="cmt"># Update all configurations and other files</span>
# reboot
</pre>
For small changes in the source you can use NO_CLEAN=yes to avoid rebuilding the whole tree.
<pre># make buildworld NO_CLEAN=yes <spanclass="cmt"># Don't delete the old objects</span>
# make buildkernel KERNCONF=MYKERNEL NO_CLEAN=yes
</pre>
<h2id="grub">Repair grub</h2>
So you broke grub? Boot from a live cd, [find your linux partition under <code>/dev</code> and use <code>fdisk</code> to find the linux partion] mount the linux partition, add /proc and /dev and use <code>grub-install /dev/xyz</code>. Suppose linux lies on <code>/dev/sda6</code>:
<pre># mount /dev/sda6 /mnt <spanclass="cmt"># mount the linux partition on /mnt</span>
# mount --bind /proc /mnt/proc <spanclass="cmt"># mount the proc subsystem into /mnt</span>
# mount --bind /dev /mnt/dev <spanclass="cmt"># mount the devices into /mnt</span>
# chroot /mnt <spanclass="cmt"># change root to the linux partition</span>
# grub-install /dev/sda <spanclass="cmt"># reinstall grub with your old settings</span>
</pre>
<h2id="sysmisc">Misc</h2>
Disable OSX virtual memory (repeat with <code>load</code> to re-enable). Faster system, but a little risky.