Script to Update Host File
I have the following command that correctly creates the /etc/hosts file that I want to build. However, I cannot write it per the 2nd form due to permissions, of course. So I tried variations of the 3rd form, but it still errors on permissions. Can someone give me a suggestion to try?
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt > /etc/hosts
cat hosts.tmp ; echo -n "myremotehost " ; echo mypassword | sudo -S cat ip.txt > /etc/hosts
scripts
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have the following command that correctly creates the /etc/hosts file that I want to build. However, I cannot write it per the 2nd form due to permissions, of course. So I tried variations of the 3rd form, but it still errors on permissions. Can someone give me a suggestion to try?
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt > /etc/hosts
cat hosts.tmp ; echo -n "myremotehost " ; echo mypassword | sudo -S cat ip.txt > /etc/hosts
scripts
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
I suspect you might be trying to re-invent the wheel here: gaenserich.github.io/hostsblock -- anyway, if that's not the case, you can perhaps get answers to your question(s) in the script(s) there ;-)
– Sadi
Sep 26 '13 at 12:43
add a comment |
I have the following command that correctly creates the /etc/hosts file that I want to build. However, I cannot write it per the 2nd form due to permissions, of course. So I tried variations of the 3rd form, but it still errors on permissions. Can someone give me a suggestion to try?
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt > /etc/hosts
cat hosts.tmp ; echo -n "myremotehost " ; echo mypassword | sudo -S cat ip.txt > /etc/hosts
scripts
I have the following command that correctly creates the /etc/hosts file that I want to build. However, I cannot write it per the 2nd form due to permissions, of course. So I tried variations of the 3rd form, but it still errors on permissions. Can someone give me a suggestion to try?
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt
cat hosts.tmp ; echo -n "myremotehost " ; cat ip.txt > /etc/hosts
cat hosts.tmp ; echo -n "myremotehost " ; echo mypassword | sudo -S cat ip.txt > /etc/hosts
scripts
scripts
asked Sep 26 '13 at 12:14
EdgeCaseEdgeCase
2861616
2861616
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 13 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
I suspect you might be trying to re-invent the wheel here: gaenserich.github.io/hostsblock -- anyway, if that's not the case, you can perhaps get answers to your question(s) in the script(s) there ;-)
– Sadi
Sep 26 '13 at 12:43
add a comment |
I suspect you might be trying to re-invent the wheel here: gaenserich.github.io/hostsblock -- anyway, if that's not the case, you can perhaps get answers to your question(s) in the script(s) there ;-)
– Sadi
Sep 26 '13 at 12:43
I suspect you might be trying to re-invent the wheel here: gaenserich.github.io/hostsblock -- anyway, if that's not the case, you can perhaps get answers to your question(s) in the script(s) there ;-)
– Sadi
Sep 26 '13 at 12:43
I suspect you might be trying to re-invent the wheel here: gaenserich.github.io/hostsblock -- anyway, if that's not the case, you can perhaps get answers to your question(s) in the script(s) there ;-)
– Sadi
Sep 26 '13 at 12:43
add a comment |
2 Answers
2
active
oldest
votes
This was hard for me to find out! (Relative newbie to Linux.)
In terminal, type sudo -i
, then log in with your user password.
You will be in the root directory, with God-like powers... :>)
I also found bash filename.sh
will start the script.
Forgive me, I feel a great sense of accomplishment having
written a script (per suggestions in another post) which
killed all ads in Youtube!
add a comment |
I would put the instructions to modify your /etc/hosts
file in a shell script, say modify-hosts.sh
, then execute
sudo modify-hosts.sh
If you insist on running the script without having to provide a password, then add a NOPASSWD
entry specifically for this script and your username to /etc/sudoers
.
As for the contents of the file, you could do:
cp hosts.tmp > /etc/hosts
echo "myremotehost $(cat /path/to/ip.txt)" >> /etc/hosts
More conveniently, if myhostname
already has an entry in /etc/hosts
you can edit it in place:
sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
And that statement you can even sudo
on one line
sudo sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
without running into redirection issues.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f350299%2fscript-to-update-host-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This was hard for me to find out! (Relative newbie to Linux.)
In terminal, type sudo -i
, then log in with your user password.
You will be in the root directory, with God-like powers... :>)
I also found bash filename.sh
will start the script.
Forgive me, I feel a great sense of accomplishment having
written a script (per suggestions in another post) which
killed all ads in Youtube!
add a comment |
This was hard for me to find out! (Relative newbie to Linux.)
In terminal, type sudo -i
, then log in with your user password.
You will be in the root directory, with God-like powers... :>)
I also found bash filename.sh
will start the script.
Forgive me, I feel a great sense of accomplishment having
written a script (per suggestions in another post) which
killed all ads in Youtube!
add a comment |
This was hard for me to find out! (Relative newbie to Linux.)
In terminal, type sudo -i
, then log in with your user password.
You will be in the root directory, with God-like powers... :>)
I also found bash filename.sh
will start the script.
Forgive me, I feel a great sense of accomplishment having
written a script (per suggestions in another post) which
killed all ads in Youtube!
This was hard for me to find out! (Relative newbie to Linux.)
In terminal, type sudo -i
, then log in with your user password.
You will be in the root directory, with God-like powers... :>)
I also found bash filename.sh
will start the script.
Forgive me, I feel a great sense of accomplishment having
written a script (per suggestions in another post) which
killed all ads in Youtube!
edited Oct 13 '15 at 9:45
hg8
9,839125591
9,839125591
answered Jun 1 '15 at 4:17
B. FifeB. Fife
247
247
add a comment |
add a comment |
I would put the instructions to modify your /etc/hosts
file in a shell script, say modify-hosts.sh
, then execute
sudo modify-hosts.sh
If you insist on running the script without having to provide a password, then add a NOPASSWD
entry specifically for this script and your username to /etc/sudoers
.
As for the contents of the file, you could do:
cp hosts.tmp > /etc/hosts
echo "myremotehost $(cat /path/to/ip.txt)" >> /etc/hosts
More conveniently, if myhostname
already has an entry in /etc/hosts
you can edit it in place:
sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
And that statement you can even sudo
on one line
sudo sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
without running into redirection issues.
add a comment |
I would put the instructions to modify your /etc/hosts
file in a shell script, say modify-hosts.sh
, then execute
sudo modify-hosts.sh
If you insist on running the script without having to provide a password, then add a NOPASSWD
entry specifically for this script and your username to /etc/sudoers
.
As for the contents of the file, you could do:
cp hosts.tmp > /etc/hosts
echo "myremotehost $(cat /path/to/ip.txt)" >> /etc/hosts
More conveniently, if myhostname
already has an entry in /etc/hosts
you can edit it in place:
sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
And that statement you can even sudo
on one line
sudo sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
without running into redirection issues.
add a comment |
I would put the instructions to modify your /etc/hosts
file in a shell script, say modify-hosts.sh
, then execute
sudo modify-hosts.sh
If you insist on running the script without having to provide a password, then add a NOPASSWD
entry specifically for this script and your username to /etc/sudoers
.
As for the contents of the file, you could do:
cp hosts.tmp > /etc/hosts
echo "myremotehost $(cat /path/to/ip.txt)" >> /etc/hosts
More conveniently, if myhostname
already has an entry in /etc/hosts
you can edit it in place:
sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
And that statement you can even sudo
on one line
sudo sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
without running into redirection issues.
I would put the instructions to modify your /etc/hosts
file in a shell script, say modify-hosts.sh
, then execute
sudo modify-hosts.sh
If you insist on running the script without having to provide a password, then add a NOPASSWD
entry specifically for this script and your username to /etc/sudoers
.
As for the contents of the file, you could do:
cp hosts.tmp > /etc/hosts
echo "myremotehost $(cat /path/to/ip.txt)" >> /etc/hosts
More conveniently, if myhostname
already has an entry in /etc/hosts
you can edit it in place:
sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
And that statement you can even sudo
on one line
sudo sed -i -e 's/myhostname .*/myhostname '$(cat ip.txt)'/' /etc/hosts
without running into redirection issues.
edited Sep 26 '13 at 12:54
answered Sep 26 '13 at 12:41
zwetszwets
8,29922340
8,29922340
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f350299%2fscript-to-update-host-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I suspect you might be trying to re-invent the wheel here: gaenserich.github.io/hostsblock -- anyway, if that's not the case, you can perhaps get answers to your question(s) in the script(s) there ;-)
– Sadi
Sep 26 '13 at 12:43